2 module.exports = function generate_validate(it, $keyword) {
4 var $async = it.schema.$async === true;
8 $dataLvl = it.dataLevel = 0,
10 it.rootId = it.resolve.fullPath(it.root.schema.id);
11 it.baseId = it.baseId || it.rootId;
14 var $es7 = it.opts.async == 'es7';
15 it.yieldAwait = $es7 ? 'await' : 'yield';
18 it.dataPathArr = [undefined];
19 out += ' var validate = ';
22 out += ' (async function ';
24 if (it.opts.async == 'co*') {
32 out += ' (data, dataPath, parentData, parentDataProperty, rootData) { \'use strict\'; var vErrors = null; ';
33 out += ' var errors = 0; ';
34 out += ' if (rootData === undefined) rootData = data;';
37 $dataLvl = it.dataLevel,
38 $data = 'data' + ($dataLvl || '');
39 if (it.schema.id) it.baseId = it.resolve.url(it.baseId, it.schema.id);
40 if ($async && !it.async) throw new Error('async schema in sync schema');
41 out += ' var errs_' + ($lvl) + ' = errors;';
43 var $valid = 'valid' + $lvl,
44 $breakOnError = !it.opts.allErrors,
48 var $typeSchema = it.schema.type,
49 $typeIsArray = Array.isArray($typeSchema);
50 if ($typeSchema && it.opts.coerceTypes) {
51 var $coerceToTypes = it.util.coerceToTypes(it.opts.coerceTypes, $typeSchema);
53 var $schemaPath = it.schemaPath + '.type',
54 $errSchemaPath = it.errSchemaPath + '/type',
55 $method = $typeIsArray ? 'checkDataTypes' : 'checkDataType';
56 out += ' if (' + (it.util[$method]($typeSchema, $data, true)) + ') { ';
57 var $dataType = 'dataType' + $lvl,
58 $coerced = 'coerced' + $lvl;
59 out += ' var ' + ($dataType) + ' = typeof ' + ($data) + '; ';
60 if (it.opts.coerceTypes == 'array') {
61 out += ' if (' + ($dataType) + ' == \'object\' && Array.isArray(' + ($data) + ')) ' + ($dataType) + ' = \'array\'; ';
63 out += ' var ' + ($coerced) + ' = undefined; ';
64 var $bracesCoercion = '';
65 var arr1 = $coerceToTypes;
70 $type = arr1[$i += 1];
72 out += ' if (' + ($coerced) + ' === undefined) { ';
73 $bracesCoercion += '}';
75 if (it.opts.coerceTypes == 'array' && $type != 'array') {
76 out += ' if (' + ($dataType) + ' == \'array\' && ' + ($data) + '.length == 1) { ' + ($coerced) + ' = ' + ($data) + ' = ' + ($data) + '[0]; ' + ($dataType) + ' = typeof ' + ($data) + '; } ';
78 if ($type == 'string') {
79 out += ' if (' + ($dataType) + ' == \'number\' || ' + ($dataType) + ' == \'boolean\') ' + ($coerced) + ' = \'\' + ' + ($data) + '; else if (' + ($data) + ' === null) ' + ($coerced) + ' = \'\'; ';
80 } else if ($type == 'number' || $type == 'integer') {
81 out += ' if (' + ($dataType) + ' == \'boolean\' || ' + ($data) + ' === null || (' + ($dataType) + ' == \'string\' && ' + ($data) + ' && ' + ($data) + ' == +' + ($data) + ' ';
82 if ($type == 'integer') {
83 out += ' && !(' + ($data) + ' % 1)';
85 out += ')) ' + ($coerced) + ' = +' + ($data) + '; ';
86 } else if ($type == 'boolean') {
87 out += ' if (' + ($data) + ' === \'false\' || ' + ($data) + ' === 0 || ' + ($data) + ' === null) ' + ($coerced) + ' = false; else if (' + ($data) + ' === \'true\' || ' + ($data) + ' === 1) ' + ($coerced) + ' = true; ';
88 } else if ($type == 'null') {
89 out += ' if (' + ($data) + ' === \'\' || ' + ($data) + ' === 0 || ' + ($data) + ' === false) ' + ($coerced) + ' = null; ';
90 } else if (it.opts.coerceTypes == 'array' && $type == 'array') {
91 out += ' if (' + ($dataType) + ' == \'string\' || ' + ($dataType) + ' == \'number\' || ' + ($dataType) + ' == \'boolean\' || ' + ($data) + ' == null) ' + ($coerced) + ' = [' + ($data) + ']; ';
95 out += ' ' + ($bracesCoercion) + ' if (' + ($coerced) + ' === undefined) { ';
96 var $$outStack = $$outStack || [];
98 out = ''; /* istanbul ignore else */
99 if (it.createErrors !== false) {
100 out += ' { keyword: \'' + ($errorKeyword || 'type') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { type: \'';
102 out += '' + ($typeSchema.join(","));
104 out += '' + ($typeSchema);
107 if (it.opts.messages !== false) {
108 out += ' , message: \'should be ';
110 out += '' + ($typeSchema.join(","));
112 out += '' + ($typeSchema);
116 if (it.opts.verbose) {
117 out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
124 out = $$outStack.pop();
125 if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
127 out += ' throw new ValidationError([' + (__err) + ']); ';
129 out += ' validate.errors = [' + (__err) + ']; return false; ';
132 out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
135 var $parentData = $dataLvl ? 'data' + (($dataLvl - 1) || '') : 'parentData',
136 $parentDataProperty = $dataLvl ? it.dataPathArr[$dataLvl] : 'parentDataProperty';
137 out += ' ' + ($data) + ' = ' + ($coerced) + '; ';
139 out += 'if (' + ($parentData) + ' !== undefined)';
141 out += ' ' + ($parentData) + '[' + ($parentDataProperty) + '] = ' + ($coerced) + '; } } ';
145 if (it.schema.$ref && ($refKeywords = it.util.schemaHasRulesExcept(it.schema, it.RULES.all, '$ref'))) {
146 if (it.opts.extendRefs == 'fail') {
147 throw new Error('$ref: validation keywords used in schema at path "' + it.errSchemaPath + '"');
148 } else if (it.opts.extendRefs == 'ignore') {
149 $refKeywords = false;
150 console.log('$ref: keywords ignored in schema at path "' + it.errSchemaPath + '"');
151 } else if (it.opts.extendRefs !== true) {
152 console.log('$ref: all keywords used in schema at path "' + it.errSchemaPath + '". It will change in the next major version, see issue #260. Use option { extendRefs: true } to keep current behaviour');
155 if (it.schema.$ref && !$refKeywords) {
156 out += ' ' + (it.RULES.all.$ref.code(it, '$ref')) + ' ';
158 out += ' } if (errors === ';
162 out += 'errs_' + ($lvl);
165 $closingBraces2 += '}';
170 var $rulesGroup, i2 = -1,
171 l2 = arr2.length - 1;
173 $rulesGroup = arr2[i2 += 1];
174 if ($shouldUseGroup($rulesGroup)) {
175 if ($rulesGroup.type) {
176 out += ' if (' + (it.util.checkDataType($rulesGroup.type, $data)) + ') { ';
178 if (it.opts.useDefaults && !it.compositeRule) {
179 if ($rulesGroup.type == 'object' && it.schema.properties) {
180 var $schema = it.schema.properties,
181 $schemaKeys = Object.keys($schema);
182 var arr3 = $schemaKeys;
184 var $propertyKey, i3 = -1,
185 l3 = arr3.length - 1;
187 $propertyKey = arr3[i3 += 1];
188 var $sch = $schema[$propertyKey];
189 if ($sch.default !== undefined) {
190 var $passData = $data + it.util.getProperty($propertyKey);
191 out += ' if (' + ($passData) + ' === undefined) ' + ($passData) + ' = ';
192 if (it.opts.useDefaults == 'shared') {
193 out += ' ' + (it.useDefault($sch.default)) + ' ';
195 out += ' ' + (JSON.stringify($sch.default)) + ' ';
201 } else if ($rulesGroup.type == 'array' && Array.isArray(it.schema.items)) {
202 var arr4 = it.schema.items;
205 l4 = arr4.length - 1;
207 $sch = arr4[$i += 1];
208 if ($sch.default !== undefined) {
209 var $passData = $data + '[' + $i + ']';
210 out += ' if (' + ($passData) + ' === undefined) ' + ($passData) + ' = ';
211 if (it.opts.useDefaults == 'shared') {
212 out += ' ' + (it.useDefault($sch.default)) + ' ';
214 out += ' ' + (JSON.stringify($sch.default)) + ' ';
222 var arr5 = $rulesGroup.rules;
225 l5 = arr5.length - 1;
227 $rule = arr5[i5 += 1];
228 if ($shouldUseRule($rule)) {
229 out += ' ' + ($rule.code(it, $rule.keyword)) + ' ';
231 $closingBraces1 += '}';
237 out += ' ' + ($closingBraces1) + ' ';
238 $closingBraces1 = '';
240 if ($rulesGroup.type) {
242 if ($typeSchema && $typeSchema === $rulesGroup.type) {
243 var $typeChecked = true;
245 var $schemaPath = it.schemaPath + '.type',
246 $errSchemaPath = it.errSchemaPath + '/type';
247 var $$outStack = $$outStack || [];
248 $$outStack.push(out);
249 out = ''; /* istanbul ignore else */
250 if (it.createErrors !== false) {
251 out += ' { keyword: \'' + ($errorKeyword || 'type') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { type: \'';
253 out += '' + ($typeSchema.join(","));
255 out += '' + ($typeSchema);
258 if (it.opts.messages !== false) {
259 out += ' , message: \'should be ';
261 out += '' + ($typeSchema.join(","));
263 out += '' + ($typeSchema);
267 if (it.opts.verbose) {
268 out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
275 out = $$outStack.pop();
276 if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
278 out += ' throw new ValidationError([' + (__err) + ']); ';
280 out += ' validate.errors = [' + (__err) + ']; return false; ';
283 out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
289 out += ' if (errors === ';
293 out += 'errs_' + ($lvl);
296 $closingBraces2 += '}';
302 if ($typeSchema && !$typeChecked && !(it.opts.coerceTypes && $coerceToTypes)) {
303 var $schemaPath = it.schemaPath + '.type',
304 $errSchemaPath = it.errSchemaPath + '/type',
305 $method = $typeIsArray ? 'checkDataTypes' : 'checkDataType';
306 out += ' if (' + (it.util[$method]($typeSchema, $data, true)) + ') { ';
307 var $$outStack = $$outStack || [];
308 $$outStack.push(out);
309 out = ''; /* istanbul ignore else */
310 if (it.createErrors !== false) {
311 out += ' { keyword: \'' + ($errorKeyword || 'type') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { type: \'';
313 out += '' + ($typeSchema.join(","));
315 out += '' + ($typeSchema);
318 if (it.opts.messages !== false) {
319 out += ' , message: \'should be ';
321 out += '' + ($typeSchema.join(","));
323 out += '' + ($typeSchema);
327 if (it.opts.verbose) {
328 out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
335 out = $$outStack.pop();
336 if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
338 out += ' throw new ValidationError([' + (__err) + ']); ';
340 out += ' validate.errors = [' + (__err) + ']; return false; ';
343 out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
348 out += ' ' + ($closingBraces2) + ' ';
352 out += ' if (errors === 0) return true; ';
353 out += ' else throw new ValidationError(vErrors); ';
355 out += ' validate.errors = vErrors; ';
356 out += ' return errors === 0; ';
358 out += ' }); return validate;';
360 out += ' var ' + ($valid) + ' = errors === errs_' + ($lvl) + ';';
362 out = it.util.cleanUpCode(out);
363 if ($top && $breakOnError) {
364 out = it.util.cleanUpVarErrors(out, $async);
367 function $shouldUseGroup($rulesGroup) {
368 for (var i = 0; i < $rulesGroup.rules.length; i++)
369 if ($shouldUseRule($rulesGroup.rules[i])) return true;
372 function $shouldUseRule($rule) {
373 return it.schema[$rule.keyword] !== undefined || ($rule.keyword == 'properties' && (it.schema.additionalProperties === false || typeof it.schema.additionalProperties == 'object' || (it.schema.patternProperties && Object.keys(it.schema.patternProperties).length) || (it.opts.v5 && it.schema.patternGroups && Object.keys(it.schema.patternGroups).length)));