forked from Meteor-Community-Packages/meteor-simple-schema
-
Notifications
You must be signed in to change notification settings - Fork 1
/
simple-schema-context.js
226 lines (185 loc) · 7.67 KB
/
simple-schema-context.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
var _ = require("lodash");
var doValidation1 = require("./simple-schema-validation").doValidation1;
var doValidation2 = require("./simple-schema-validation-new").doValidation2;
var shims = require("./shims");
var Meteor = shims.Meteor;
var Deps = shims.Deps;
/* global SimpleSchema */
/* global SimpleSchemaValidationContext:true */
/* global doValidation1 */
/* global doValidation2 */
function doValidation(obj, isModifier, isUpsert, keyToValidate, ss, extendedCustomContext) {
var useOld = true; //for now this can be manually changed to try the experimental method, which doesn't yet work properly
var func = useOld ? doValidation1 : doValidation2;
return func(obj, isModifier, isUpsert, keyToValidate, ss, extendedCustomContext);
}
/*
* PUBLIC API
*/
var SimpleSchemaValidationContext = function SimpleSchemaValidationContext(ss) {
var self = this;
self._simpleSchema = ss;
self._schema = ss.schema();
self._schemaKeys = _.keys(self._schema);
self._invalidKeys = [];
//set up validation dependencies
self._deps = {};
self._depsAny = new Deps.Dependency();
_.each(self._schemaKeys, function(name) {
self._deps[name] = new Deps.Dependency();
});
};
//validates the object against the simple schema and sets a reactive array of error objects
SimpleSchemaValidationContext.prototype.validate = function simpleSchemaValidationContextValidate(doc, options) {
var self = this;
options = _.extend({
modifier: false,
upsert: false,
extendedCustomContext: {}
}, options || {});
//on the client we can add the userId if not already in the custom context
if (Meteor.isClient && options.extendedCustomContext.userId === void 0) {
options.extendedCustomContext.userId = (Meteor.userId && Meteor.userId()) || null;
}
var invalidKeys = doValidation(doc, options.modifier, options.upsert, null, self._simpleSchema, options.extendedCustomContext);
//now update self._invalidKeys and dependencies
//note any currently invalid keys so that we can mark them as changed
//due to new validation (they may be valid now, or invalid in a different way)
var removedKeys = _.pluck(self._invalidKeys, "name");
//update
self._invalidKeys = invalidKeys;
//add newly invalid keys to changedKeys
var addedKeys = _.pluck(self._invalidKeys, "name");
//mark all changed keys as changed
var changedKeys = _.union(addedKeys, removedKeys);
self._markKeysChanged(changedKeys);
// Return true if it was valid; otherwise, return false
return self._invalidKeys.length === 0;
};
//validates doc against self._schema for one key and sets a reactive array of error objects
SimpleSchemaValidationContext.prototype.validateOne = function simpleSchemaValidationContextValidateOne(doc, keyName, options) {
var self = this, i, ln, k;
options = _.extend({
modifier: false,
upsert: false,
extendedCustomContext: {}
}, options || {});
//on the client we can add the userId if not already in the custom context
if (Meteor.isClient && options.extendedCustomContext.userId === void 0) {
options.extendedCustomContext.userId = (Meteor.userId && Meteor.userId()) || null;
}
var invalidKeys = doValidation(doc, options.modifier, options.upsert, keyName, self._simpleSchema, options.extendedCustomContext);
//now update self._invalidKeys and dependencies
//remove objects from self._invalidKeys where name = keyName
var newInvalidKeys = [];
for (i = 0, ln = self._invalidKeys.length; i < ln; i++) {
k = self._invalidKeys[i];
if (k.name !== keyName) {
newInvalidKeys.push(k);
}
}
self._invalidKeys = newInvalidKeys;
//merge invalidKeys into self._invalidKeys
for (i = 0, ln = invalidKeys.length; i < ln; i++) {
k = invalidKeys[i];
self._invalidKeys.push(k);
}
//mark key as changed due to new validation (they may be valid now, or invalid in a different way)
self._markKeysChanged([keyName]);
// Return true if it was valid; otherwise, return false
return !self._keyIsInvalid(keyName);
};
//reset the invalidKeys array
SimpleSchemaValidationContext.prototype.resetValidation = function simpleSchemaValidationContextResetValidation() {
var self = this;
var removedKeys = _.pluck(self._invalidKeys, "name");
self._invalidKeys = [];
self._markKeysChanged(removedKeys);
};
SimpleSchemaValidationContext.prototype.isValid = function simpleSchemaValidationContextIsValid() {
var self = this;
self._depsAny.depend();
return !self._invalidKeys.length;
};
SimpleSchemaValidationContext.prototype.invalidKeys = function simpleSchemaValidationContextInvalidKeys() {
var self = this;
self._depsAny.depend();
return self._invalidKeys;
};
SimpleSchemaValidationContext.prototype.addInvalidKeys = function simpleSchemaValidationContextAddInvalidKeys(errors) {
var self = this;
if (!errors || !errors.length) {
return;
}
var changedKeys = [];
_.each(errors, function (errorObject) {
changedKeys.push(errorObject.name);
self._invalidKeys.push(errorObject);
});
self._markKeysChanged(changedKeys);
};
SimpleSchemaValidationContext.prototype._markKeysChanged = function simpleSchemaValidationContextMarkKeysChanged(keys) {
var self = this;
if (!keys || !keys.length) {
return;
}
_.each(keys, function(name) {
var SimpleSchema = require("./simple-schema");
var genericName = SimpleSchema._makeGeneric(name);
if (genericName in self._deps) {
self._deps[genericName].changed();
}
});
self._depsAny.changed();
};
SimpleSchemaValidationContext.prototype._getInvalidKeyObject = function simpleSchemaValidationContextGetInvalidKeyObject(name, genericName) {
var self = this;
var SimpleSchema = require("./simple-schema");
genericName = genericName || SimpleSchema._makeGeneric(name);
var errorObj = _.findWhere(self._invalidKeys, {name: name});
if (!errorObj) {
errorObj = _.findWhere(self._invalidKeys, {name: genericName});
}
return errorObj;
};
SimpleSchemaValidationContext.prototype._keyIsInvalid = function simpleSchemaValidationContextKeyIsInvalid(name, genericName) {
return !!this._getInvalidKeyObject(name, genericName);
};
// Like the internal one, but with deps
SimpleSchemaValidationContext.prototype.keyIsInvalid = function simpleSchemaValidationContextKeyIsInvalid(name) {
var SimpleSchema = require("./simple-schema");
var self = this, genericName = SimpleSchema._makeGeneric(name);
self._deps[genericName] && self._deps[genericName].depend();
return self._keyIsInvalid(name, genericName);
};
SimpleSchemaValidationContext.prototype.keyErrorMessage = function simpleSchemaValidationContextKeyErrorMessage(name) {
var SimpleSchema = require("./simple-schema");
var self = this, genericName = SimpleSchema._makeGeneric(name);
self._deps[genericName] && self._deps[genericName].depend();
var errorObj = self._getInvalidKeyObject(name, genericName);
if (!errorObj) {
return "";
}
return self._simpleSchema.messageForError(errorObj.type, errorObj.name, null, errorObj.value);
};
SimpleSchemaValidationContext.prototype.getErrorObject = function simpleSchemaValidationContextGetErrorObject() {
var self = this, message, invalidKeys = this._invalidKeys;
if (invalidKeys.length) {
message = self.keyErrorMessage(invalidKeys[0].name);
// We add `message` prop to the invalidKeys.
invalidKeys = _.map(invalidKeys, function (o) {
return _.extend({message: self.keyErrorMessage(o.name)}, o);
});
} else {
message = "Failed validation";
}
var error = new Error(message);
error.invalidKeys = invalidKeys;
// If on the server, we add a sanitized error, too, in case we're
// called from a method.
if (Meteor.isServer) {
error.sanitizedError = new Meteor.Error(400, message);
}
return error;
};
module.exports = SimpleSchemaValidationContext;