-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
506 lines (414 loc) · 11.1 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
var isBrowser = require('is-browser')
, each = isBrowser ? require('each') : require('each-component')
, type = isBrowser ? require('type') : require('component-type')
, inherit = isBrowser ? require('inherit') : require('inherit-component')
, Uri = isBrowser ? require('json-schema-uri') : require('json-schema-uri-component')
, has = Object.hasOwnProperty
var Refs = require('./refs')
, Correlation = require('./correlation')
module.exports = {
Node: Node,
Schema: Schema,
SchemaCollection: SchemaCollection,
SchemaArray: SchemaArray,
Correlation: Correlation
};
///////
// abstract base class, mostly
function Node(parent){
this.parent = parent;
this.nodeType = 'Node';
this._scope = undefined;
this._refs = (parent && parent.refs()) || new Refs()
}
Node.prototype.parse = function(obj){} // subclass parse
Node.prototype.get = function(key){} // subclass property/element accessor
Node.prototype.has = function(key){}
Node.prototype.set = function(key,val){} // setter
Node.prototype.each = function(fn){} // iterator
Node.prototype.toObject = function(){} // serializer
Node.prototype.toString = function(){
return JSON.stringify(this.toObject());
}
Node.prototype.scope = function(id){
var cur = this._scope || (this.parent && this.parent.scope());
if (arguments.length == 0) {
return cur;
} else {
var uri = Uri(cur).join(id);
this._scope = uri.toString();
this._refs.addScope(this._scope,this);
}
}
Node.prototype.refs = function(){
return this._refs;
}
Node.prototype.root = function(){
if (!this.parent) { return this; }
else { return this.parent.root(); }
}
Node.prototype.eachRef = function(fn){
this._refs.each( fn );
}
Node.prototype.addRef = function(ref,key){
var uri = Uri(this.scope()).join(ref)
this._refs.add(uri.toString(),this,key);
}
Node.prototype.$ = function(key){
var uri = Uri(this.scope()).join(key)
var ret = this.getId(uri)
if (ret) return ret;
var base = uri.base()
, fragment = uri.fragment()
var root = base ? this.getId(base) : this;
if (!root) return;
return root.getPath(fragment);
}
Node.prototype.getId = function(uri){
return this._refs.getScope(uri.toString());
}
// recursive get via (absolute or relative) path
Node.prototype.getPath = function(path){
var segs = path.split('/')
, seg = segs.shift()
, path = segs.join('/')
if (0 == seg.length && 0 == segs.length) return this;
if ('#' == seg) return this.root().getPath(path);
if (!this.has(seg)) return; // or throw error ?
if (0 == path.length){
return this.get(seg);
} else {
return this.get(seg).getPath(path);
}
}
function refOf(obj){
return ("object"==type(obj) && obj['$ref']);
}
///////
// core classes
function Schema(parent){
Node.call(this,parent);
this.nodeType = 'Schema';
this._properties = {};
this._conditions = {};
}
inherit(Schema,Node);
Schema.prototype.parse = function(obj){
if (has.call(obj,'id')) this.scope(obj.id);
var self = this;
each(obj, function(key,val){
if (val == 'id') return;
var ref = refOf(val)
if (ref) { self.addRef(ref,key); return; }
var klass = Schema.getType(key);
if (klass){
self.addCondition(key,val,klass);
} else {
self.addProperty(key,val);
}
})
return this;
}
Schema.prototype.get = function(key){
return this._conditions[key];
}
Schema.prototype.set = function(key,cond){
this._conditions[key] = cond;
}
Schema.prototype.has = function(key){
return has.call(this._conditions,key);
}
Schema.prototype.each = function(fn){
each(this._conditions, fn);
}
Schema.prototype.addCondition = function(key,val,klass){
var parsed = new klass(this).parse(val);
if (has.call(parsed,'nodeType')){
this.set(key,parsed);
} else {
this.addProperty(key,parsed);
}
}
Schema.prototype.addProperty = function(key,val){
this._properties[key] = val;
}
Schema.prototype.getProperty =
Schema.prototype.property = function(key){
return this._properties[key];
}
Schema.prototype.hasProperty = function(key){
return has.call(this._properties,key);
}
Schema.prototype.eachProperty = function(fn){
each(this._properties, fn);
}
Schema.prototype.toObject = function(){
var obj = {}
this.eachProperty( function(key,val){
obj[key] = val;
})
this.each( function(key,cond){
obj[key] = cond.toObject();
})
return obj;
}
// mix in each binding method into a Correlation object
Schema.prototype.bind = function(instance){
var ret = new Correlation(this,instance);
for (var key in Schema._bindings){
var fn = Schema._bindings[key];
ret[key] = fn.bind(ret);
}
return ret;
}
// Schema class methods
Schema.union =
Schema.allOf = function(schemas){
var schema = new Schema()
schema.addCondition('allOf',[],AllOf);
var allOf = schema.get('allOf');
for (var i=0;i<schemas.length;++i){
allOf.set(schemas[i]);
}
return schema;
}
Schema.getType = function(prop){
return this._types[prop];
}
Schema.addType = function(prop,klass){
this._types[prop] = klass;
}
Schema.addBinding = function(key, fn){
this._bindings[key] = fn;
}
Schema.use = function(plugin){
plugin(this);
}
Schema._types = {};
Schema._bindings = {};
///////
// inject node parse classes by default
function base(target){
target.addType('items',Items);
target.addType('additionalItems',AdditionalItems);
target.addType('definitions',Definitions);
target.addType('properties',Properties);
target.addType('patternProperties',PatternProperties);
target.addType('additionalProperties',AdditionalProperties);
target.addType('dependencies',Dependencies);
// target.addType('type',Type);
target.addType('allOf',AllOf);
target.addType('anyOf',AnyOf);
target.addType('oneOf',OneOf);
target.addType('not',Not);
}
Schema.use(base);
///////
// base node parse classes
function SchemaCollection(parent){
Node.call(this,parent);
this.nodeType = 'SchemaCollection';
this._schemas = {};
}
inherit(SchemaCollection,Node);
SchemaCollection.prototype.parse = function(obj){
var self = this;
each(obj, function(key,val){
var ref = refOf(val)
if (ref) { self.addRef(ref,key); return; }
self.addSchema(key,val);
})
return this;
}
SchemaCollection.prototype.get = function(key){
return this._schemas[key];
}
SchemaCollection.prototype.set = function(key,schema){
this._schemas[key] = schema;
}
SchemaCollection.prototype.has = function(key){
return has.call(this._schemas,key);
}
SchemaCollection.prototype.each = function(fn){
each(this._schemas, fn);
}
SchemaCollection.prototype.addSchema = function(key,val){
var schema = new Schema(this).parse(val);
this.set(key,schema);
}
SchemaCollection.prototype.toObject = function(){
var obj = {}
this.each( function(key,schema){
obj[key] = schema.toObject();
})
return obj;
}
function SchemaArray(parent){
Node.call(this,parent);
this.nodeType = 'SchemaArray';
this._schemas = [];
}
inherit(SchemaArray,Node);
SchemaArray.prototype.parse = function(obj){
var self = this;
each(obj, function(val,i){
var ref = refOf(val)
if (ref) { self.addRef(ref,i); return; }
self.addSchema(val);
})
return this;
}
SchemaArray.prototype.get = function(i){
return this._schemas[i];
}
SchemaArray.prototype.set = function(i,schema){
if (arguments.length == 1){
schema = i; i = undefined;
this._schemas.push(schema);
} else {
this._schemas[i] = schema;
}
}
SchemaArray.prototype.has = function(i){
return !!this.get(i);
}
SchemaArray.prototype.each = function(fn){
each(this._schemas, function(obj,i){ fn(i,obj); });
}
SchemaArray.prototype.addSchema = function(val){
var schema = new Schema(this).parse(val);
this.set(schema);
}
SchemaArray.prototype.toObject = function(){
var obj = []
this.each( function(i,schema){ obj.push(schema.toObject()); } );
return obj;
}
function SchemaOrBoolean(parent){
if (!(this instanceof SchemaOrBoolean)) return new SchemaOrBoolean(parent);
this.parent = parent;
return this;
}
SchemaOrBoolean.prototype.parse = function(obj){
return (type(obj) == 'boolean' ? obj
: new Schema(this.parent).parse(obj)
);
}
///////
// concrete node parse classes
function Definitions(parent){
SchemaCollection.call(this,parent);
this.nodeType = 'Definitions';
}
function Properties(parent){
SchemaCollection.call(this,parent);
this.nodeType = 'Properties';
}
function PatternProperties(parent){
SchemaCollection.call(this,parent);
this.nodeType = 'PatternProperties';
}
inherit(Definitions, SchemaCollection);
inherit(Properties, SchemaCollection);
inherit(PatternProperties, SchemaCollection);
function AllOf(parent){
SchemaArray.call(this,parent);
this.nodeType = 'AllOf';
}
function AnyOf(parent){
SchemaArray.call(this,parent);
this.nodeType = 'AnyOf';
}
function OneOf(parent){
SchemaArray.call(this,parent);
this.nodeType = 'OneOf';
}
inherit(AllOf, SchemaArray);
inherit(AnyOf, SchemaArray);
inherit(OneOf, SchemaArray);
function Not(parent){
Schema.call(this,parent);
this.nodeType = 'Not';
}
inherit(Not, Schema);
function AdditionalProperties(parent){
SchemaOrBoolean.call(this,parent);
}
function AdditionalItems(parent){
SchemaOrBoolean.call(this,parent);
}
inherit(AdditionalProperties,SchemaOrBoolean);
inherit(AdditionalItems,SchemaOrBoolean);
// custom node classes
function Items(parent){
if (!(this instanceof Items)) return new Items(parent);
this.parent = parent;
return this;
}
Items.prototype.parse = function(obj){
return (type(obj) == 'array' ? new SchemaArray(this.parent).parse(obj)
: new Schema(this.parent).parse(obj)
);
}
function Dependencies(parent){
Node.call(this,parent);
this.nodeType = 'Dependencies';
this._deps = {};
}
inherit(Dependencies,Node);
Dependencies.prototype.parse = function(obj){
var self = this;
each(obj, function(key,val){
var ref = refOf(val)
if (ref) { self.addRef(ref,key); return; }
self.addDependency(key,val);
})
return this;
}
Dependencies.prototype.get = function(key){
return this._deps[key];
}
Dependencies.prototype.set = function(key,schema){
this._deps[key] = schema;
}
Dependencies.prototype.has = function(key){
return has.call(this._deps,key);
}
Dependencies.prototype.each = function(fn){
each(this._deps, fn);
}
Dependencies.prototype.addDependency = function(key,val){
var dep = new Dependency(this).parse(val);
this.set(key,dep);
}
Dependencies.prototype.eachSchemaDependency = function(fn){
each(this._deps, function(key,dep){
if (dep instanceof Schema) fn(key,dep);
})
}
Dependencies.prototype.eachPropertyDependency = function(fn){
each(this._deps, function(key,dep){
if (type(dep) == 'array') fn(key,dep);
})
}
Dependencies.prototype.toObject = function(){
var obj = {}
this.each( function(key,dep){
if (dep.nodeType && dep.nodeType == 'Schema'){
obj[key] = dep.toObject();
} else {
obj[key] = dep;
}
});
return obj;
}
function Dependency(parent){
if (!(this instanceof Dependency)) return new Dependency(parent);
this.parent = parent;
return this;
}
Dependency.prototype.parse = function(obj){
return (type(obj) == 'array' ? obj
: new Schema(this.parent).parse(obj)
);
}