3 var resolve = require('./resolve')
4 , util = require('./util')
5 , errorClasses = require('./error_classes')
6 , stableStringify = require('fast-json-stable-stringify');
8 var validateGenerator = require('../dotjs/validate');
11 * Functions below are used inside compiled validations function
14 var ucs2length = util.ucs2length;
15 var equal = require('fast-deep-equal');
17 // this error is thrown by async schemas to return validation errors via exception
18 var ValidationError = errorClasses.Validation;
20 module.exports = compile;
24 * Compiles schema to validation function
26 * @param {Object} schema schema object
27 * @param {Object} root object with information about the root schema for this schema
28 * @param {Object} localRefs the hash of local references inside the schema (created by resolve.id), used for inline resolution
29 * @param {String} baseId base ID for IDs in the schema
30 * @return {Function} validation function
32 function compile(schema, root, localRefs, baseId) {
33 /* jshint validthis: true, evil: true */
34 /* eslint no-shadow: 0 */
37 , refVal = [ undefined ]
45 root = root || { schema: schema, refVal: refVal, refs: refs };
47 var c = checkCompiling.call(this, schema, root, baseId);
48 var compilation = this._compilations[c.index];
49 if (c.compiling) return (compilation.callValidate = callValidate);
51 var formats = this._formats;
52 var RULES = this.RULES;
55 var v = localCompile(schema, root, localRefs, baseId);
56 compilation.validate = v;
57 var cv = compilation.callValidate;
65 if (opts.sourceCode) cv.source = v.source;
69 endCompiling.call(this, schema, root, baseId);
72 /* @this {*} - custom context, see passContext option */
73 function callValidate() {
74 /* jshint validthis: true */
75 var validate = compilation.validate;
76 var result = validate.apply(this, arguments);
77 callValidate.errors = validate.errors;
81 function localCompile(_schema, _root, localRefs, baseId) {
82 var isRoot = !_root || (_root && _root.schema == _schema);
83 if (_root.schema != root.schema)
84 return compile.call(self, _schema, _root, localRefs, baseId);
86 var $async = _schema.$async === true;
88 var sourceCode = validateGenerator({
97 MissingRefError: errorClasses.MissingRef,
99 validate: validateGenerator,
102 resolveRef: resolveRef,
103 usePattern: usePattern,
104 useDefault: useDefault,
105 useCustomRule: useCustomRule,
112 sourceCode = vars(refVal, refValCode) + vars(patterns, patternCode)
113 + vars(defaults, defaultCode) + vars(customRules, customRuleCode)
116 if (opts.processCode) sourceCode = opts.processCode(sourceCode, _schema);
117 // console.log('\n\n\n *** \n', JSON.stringify(sourceCode));
120 var makeValidate = new Function(
134 validate = makeValidate(
147 refVal[0] = validate;
149 self.logger.error('Error compiling schema, function code:', sourceCode);
153 validate.schema = _schema;
154 validate.errors = null;
155 validate.refs = refs;
156 validate.refVal = refVal;
157 validate.root = isRoot ? validate : _root;
158 if ($async) validate.$async = true;
159 if (opts.sourceCode === true) {
170 function resolveRef(baseId, ref, isRoot) {
171 ref = resolve.url(baseId, ref);
172 var refIndex = refs[ref];
173 var _refVal, refCode;
174 if (refIndex !== undefined) {
175 _refVal = refVal[refIndex];
176 refCode = 'refVal[' + refIndex + ']';
177 return resolvedRef(_refVal, refCode);
179 if (!isRoot && root.refs) {
180 var rootRefId = root.refs[ref];
181 if (rootRefId !== undefined) {
182 _refVal = root.refVal[rootRefId];
183 refCode = addLocalRef(ref, _refVal);
184 return resolvedRef(_refVal, refCode);
188 refCode = addLocalRef(ref);
189 var v = resolve.call(self, localCompile, root, ref);
190 if (v === undefined) {
191 var localSchema = localRefs && localRefs[ref];
193 v = resolve.inlineRef(localSchema, opts.inlineRefs)
195 : compile.call(self, localSchema, root, localRefs, baseId);
199 if (v === undefined) {
202 replaceLocalRef(ref, v);
203 return resolvedRef(v, refCode);
207 function addLocalRef(ref, v) {
208 var refId = refVal.length;
211 return 'refVal' + refId;
214 function removeLocalRef(ref) {
218 function replaceLocalRef(ref, v) {
219 var refId = refs[ref];
223 function resolvedRef(refVal, code) {
224 return typeof refVal == 'object' || typeof refVal == 'boolean'
225 ? { code: code, schema: refVal, inline: true }
226 : { code: code, $async: refVal && !!refVal.$async };
229 function usePattern(regexStr) {
230 var index = patternsHash[regexStr];
231 if (index === undefined) {
232 index = patternsHash[regexStr] = patterns.length;
233 patterns[index] = regexStr;
235 return 'pattern' + index;
238 function useDefault(value) {
239 switch (typeof value) {
244 return util.toQuotedString(value);
246 if (value === null) return 'null';
247 var valueStr = stableStringify(value);
248 var index = defaultsHash[valueStr];
249 if (index === undefined) {
250 index = defaultsHash[valueStr] = defaults.length;
251 defaults[index] = value;
253 return 'default' + index;
257 function useCustomRule(rule, schema, parentSchema, it) {
258 if (self._opts.validateSchema !== false) {
259 var deps = rule.definition.dependencies;
260 if (deps && !deps.every(function(keyword) {
261 return Object.prototype.hasOwnProperty.call(parentSchema, keyword);
263 throw new Error('parent schema must have all required keywords: ' + deps.join(','));
265 var validateSchema = rule.definition.validateSchema;
266 if (validateSchema) {
267 var valid = validateSchema(schema);
269 var message = 'keyword schema is invalid: ' + self.errorsText(validateSchema.errors);
270 if (self._opts.validateSchema == 'log') self.logger.error(message);
271 else throw new Error(message);
276 var compile = rule.definition.compile
277 , inline = rule.definition.inline
278 , macro = rule.definition.macro;
282 validate = compile.call(self, schema, parentSchema, it);
284 validate = macro.call(self, schema, parentSchema, it);
285 if (opts.validateSchema !== false) self.validateSchema(validate, true);
287 validate = inline.call(self, it, rule.keyword, schema, parentSchema);
289 validate = rule.definition.validate;
290 if (!validate) return;
293 if (validate === undefined)
294 throw new Error('custom keyword "' + rule.keyword + '"failed to compile');
296 var index = customRules.length;
297 customRules[index] = validate;
300 code: 'customRule' + index,
308 * Checks if the schema is currently compiled
310 * @param {Object} schema schema to compile
311 * @param {Object} root root object
312 * @param {String} baseId base schema ID
313 * @return {Object} object with properties "index" (compilation index) and "compiling" (boolean)
315 function checkCompiling(schema, root, baseId) {
316 /* jshint validthis: true */
317 var index = compIndex.call(this, schema, root, baseId);
318 if (index >= 0) return { index: index, compiling: true };
319 index = this._compilations.length;
320 this._compilations[index] = {
325 return { index: index, compiling: false };
330 * Removes the schema from the currently compiled list
332 * @param {Object} schema schema to compile
333 * @param {Object} root root object
334 * @param {String} baseId base schema ID
336 function endCompiling(schema, root, baseId) {
337 /* jshint validthis: true */
338 var i = compIndex.call(this, schema, root, baseId);
339 if (i >= 0) this._compilations.splice(i, 1);
344 * Index of schema compilation in the currently compiled list
346 * @param {Object} schema schema to compile
347 * @param {Object} root root object
348 * @param {String} baseId base schema ID
349 * @return {Integer} compilation index
351 function compIndex(schema, root, baseId) {
352 /* jshint validthis: true */
353 for (var i=0; i<this._compilations.length; i++) {
354 var c = this._compilations[i];
355 if (c.schema == schema && c.root == root && c.baseId == baseId) return i;
361 function patternCode(i, patterns) {
362 return 'var pattern' + i + ' = new RegExp(' + util.toQuotedString(patterns[i]) + ');';
366 function defaultCode(i) {
367 return 'var default' + i + ' = defaults[' + i + '];';
371 function refValCode(i, refVal) {
372 return refVal[i] === undefined ? '' : 'var refVal' + i + ' = refVal[' + i + '];';
376 function customRuleCode(i) {
377 return 'var customRule' + i + ' = customRules[' + i + '];';
381 function vars(arr, statement) {
382 if (!arr.length) return '';
384 for (var i=0; i<arr.length; i++)
385 code += statement(i, arr);