-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsonschema.js
160 lines (136 loc) · 4.43 KB
/
jsonschema.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
const lodash = _;
const compositeSchema = function (schemaObject, parent) {
if (!parent) parent = [];
if (schemaObject.$ref) {
let referencedSchema = this.Validator.schemas[schemaObject.$ref];
delete schemaObject.$ref;
schemaObject = _.extend({}, schemaObject, referencedSchema);
}
_.each(schemaObject.properties, (schemaField, schemaProperty)=>{
let newParent = _.clone(parent);
newParent.push(schemaProperty);
this._keys.push(newParent.join('.'));
schemaObject.properties[schemaProperty] = compositeSchema.call(this, schemaField, newParent);
});
return schemaObject;
};
JsonSchema = class JsonSchema {
constructor(schema, opt) {
this.opt = _.extend({}, {
validator: 'default'
}, opt);
this._contexts = {};
this._keys = [];
this.Validator = JsonSchemaUtility.Validator(this.opt.validator);
this.schema = schema;
this.compositedSchema = compositeSchema.call(this, schema);
// Adding the schema to the scope of schemas when it provides a id
if (schema.id) {
this.Validator.addSchema(schema, schema.id);
}
}
/**
* getSchema() returns a cleaned schema for the given field.
* You can request also a nested schema
* If the field is not set you will get the hole schema
*/
getSchema(field) {
if (!field) return this.compositedSchema;
/**
Todo:
possibility for arrays
*/
return _.reduce(field.split('.'), function(obj, key){
if (obj.$ref) {
obj = this.Validator.schemas[obj.$ref];
}
if (!obj) return null;
if (!obj.properties || !obj.properties[key]) return null;
return obj.properties[key];
}, this.compositedSchema);
}
getDefaultValue(field) {
let schema = this.getSchema(field);
if (!schema) return;
if (typeof(schema.defaultValue) == 'undefined') return;
if (_.isFunction(schema.defaultValue)) {
return schema.defaultValue.call({});
}
return schema.defaultValue;
}
context(key) {
key = key || '_default';
if (!this._contexts[key]) {
this._contexts[key] = new JsonSchemaContext(this);
}
return this._contexts[key];
}
validate(doc) {
if (!_.isObject(doc) || !doc) doc = {};
const validation = this.Validator.validate(doc, this.schema);
return new JsonSchemaValidation(validation);
}
label(field) {
const label = this.getSchema(field).label;
if (_.isFunction(label)) return label();
return label || field;
}
getKeys() {
return this._keys;
}
attachTo(collection) {
if (!(collection instanceof Meteor.Collection)) {
throw new Meteor.Error(400, 'You must attach a valid Meteor.Collection instance.');
}
if (!collection._mjs) {
collection.attachJsonSchema(this);
}
return this;
}
_cleanJsonschemaObject(context, doc, field, fieldPath) {
let cleanedObject = {};
if (context && context.properties) {
_.each(context.properties, (fieldValue, fieldProperty)=>{
let _fieldPath =(((fieldPath) ? fieldPath + '.':((field) ? field + '.':'')) + fieldProperty);
let _context = context.properties[fieldProperty];
cleanedObject[fieldProperty] = this._cleanJsonschemaObject(_context, doc, fieldProperty, _fieldPath);
});
} else {
let value = fieldPath ? lodash.get(doc, fieldPath, null):doc;
if (_.isNull(value) || _.isUndefined(value)) {
value = this.getDefaultValue(fieldPath) || null;
}
return value;
}
return cleanedObject;
}
clean(doc, opt) {
opt = _.extend({
removeInvalid: false,
setDefaultValueFallbackOnError: true,
}, opt);
let cleanedObject = this._cleanJsonschemaObject(this.schema, doc, null, null);
this.getKeys().map(function(key){
let v = lodash.get(cleanedObject, key, null);
if (_.isNull(v) || _.isUndefined(v)) {
lodash.unset(cleanedObject, key);
}
});
if (opt.removeInvalid) {
let validation = this.validate(cleanedObject);
_.each(validation.invalidKeys, (invalidKey)=>{
// do we have a defaultValue for fallback ?
let defaultValue = null;
if (opt.setDefaultValueFallbackOnError) {
defaultValue = this.getDefaultValue(invalidKey);
}
if (!_.isNull(defaultValue) && !_.isUndefined(defaultValue)) {
lodash.set(cleanedObject, invalidKey, defaultValue);
} else {
lodash.unset(cleanedObject, invalidKey);
}
});
}
return cleanedObject;
}
};