validate.jst 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. {{# def.definitions }}
  2. {{# def.errors }}
  3. {{# def.defaults }}
  4. {{# def.coerce }}
  5. {{ /**
  6. * schema compilation (render) time:
  7. * it = { schema, RULES, _validate, opts }
  8. * it.validate - this template function,
  9. * it is used recursively to generate code for subschemas
  10. *
  11. * runtime:
  12. * "validate" is a variable name to which this function will be assigned
  13. * validateRef etc. are defined in the parent scope in index.js
  14. */ }}
  15. {{ var $async = it.schema.$async === true; }}
  16. {{? it.isTop}}
  17. {{
  18. var $top = it.isTop
  19. , $lvl = it.level = 0
  20. , $dataLvl = it.dataLevel = 0
  21. , $data = 'data';
  22. it.rootId = it.resolve.fullPath(it.root.schema.id);
  23. it.baseId = it.baseId || it.rootId;
  24. if ($async) {
  25. it.async = true;
  26. var $es7 = it.opts.async == 'es7';
  27. it.yieldAwait = $es7 ? 'await' : 'yield';
  28. }
  29. delete it.isTop;
  30. it.dataPathArr = [undefined];
  31. }}
  32. var validate =
  33. {{? $async }}
  34. {{? $es7 }}
  35. (async function
  36. {{??}}
  37. {{? it.opts.async == 'co*'}}co.wrap{{?}}(function*
  38. {{?}}
  39. {{??}}
  40. (function
  41. {{?}}
  42. (data, dataPath, parentData, parentDataProperty, rootData) {
  43. 'use strict';
  44. var vErrors = null; {{ /* don't edit, used in replace */ }}
  45. var errors = 0; {{ /* don't edit, used in replace */ }}
  46. if (rootData === undefined) rootData = data;
  47. {{??}}
  48. {{
  49. var $lvl = it.level
  50. , $dataLvl = it.dataLevel
  51. , $data = 'data' + ($dataLvl || '');
  52. if (it.schema.id) it.baseId = it.resolve.url(it.baseId, it.schema.id);
  53. if ($async && !it.async) throw new Error('async schema in sync schema');
  54. }}
  55. var errs_{{=$lvl}} = errors;
  56. {{?}}
  57. {{
  58. var $valid = 'valid' + $lvl
  59. , $breakOnError = !it.opts.allErrors
  60. , $closingBraces1 = ''
  61. , $closingBraces2 = '';
  62. var $errorKeyword;
  63. var $typeSchema = it.schema.type
  64. , $typeIsArray = Array.isArray($typeSchema);
  65. }}
  66. {{## def.checkType:
  67. {{
  68. var $schemaPath = it.schemaPath + '.type'
  69. , $errSchemaPath = it.errSchemaPath + '/type'
  70. , $method = $typeIsArray ? 'checkDataTypes' : 'checkDataType';
  71. }}
  72. if ({{= it.util[$method]($typeSchema, $data, true) }}) {
  73. #}}
  74. {{? $typeSchema && it.opts.coerceTypes }}
  75. {{ var $coerceToTypes = it.util.coerceToTypes(it.opts.coerceTypes, $typeSchema); }}
  76. {{? $coerceToTypes }}
  77. {{# def.checkType }}
  78. {{# def.coerceType }}
  79. }
  80. {{?}}
  81. {{?}}
  82. {{ var $refKeywords; }}
  83. {{? it.schema.$ref && ($refKeywords = it.util.schemaHasRulesExcept(it.schema, it.RULES.all, '$ref')) }}
  84. {{? it.opts.extendRefs == 'fail' }}
  85. {{ throw new Error('$ref: validation keywords used in schema at path "' + it.errSchemaPath + '"'); }}
  86. {{?? it.opts.extendRefs == 'ignore' }}
  87. {{
  88. $refKeywords = false;
  89. console.log('$ref: keywords ignored in schema at path "' + it.errSchemaPath + '"');
  90. }}
  91. {{?? it.opts.extendRefs !== true }}
  92. {{ 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'); }}
  93. {{?}}
  94. {{?}}
  95. {{? it.schema.$ref && !$refKeywords }}
  96. {{= it.RULES.all.$ref.code(it, '$ref') }}
  97. {{? $breakOnError }}
  98. }
  99. if (errors === {{?$top}}0{{??}}errs_{{=$lvl}}{{?}}) {
  100. {{ $closingBraces2 += '}'; }}
  101. {{?}}
  102. {{??}}
  103. {{~ it.RULES:$rulesGroup }}
  104. {{? $shouldUseGroup($rulesGroup) }}
  105. {{? $rulesGroup.type }}
  106. if ({{= it.util.checkDataType($rulesGroup.type, $data) }}) {
  107. {{?}}
  108. {{? it.opts.useDefaults && !it.compositeRule }}
  109. {{? $rulesGroup.type == 'object' && it.schema.properties }}
  110. {{# def.defaultProperties }}
  111. {{?? $rulesGroup.type == 'array' && Array.isArray(it.schema.items) }}
  112. {{# def.defaultItems }}
  113. {{?}}
  114. {{?}}
  115. {{~ $rulesGroup.rules:$rule }}
  116. {{? $shouldUseRule($rule) }}
  117. {{= $rule.code(it, $rule.keyword) }}
  118. {{? $breakOnError }}
  119. {{ $closingBraces1 += '}'; }}
  120. {{?}}
  121. {{?}}
  122. {{~}}
  123. {{? $breakOnError }}
  124. {{= $closingBraces1 }}
  125. {{ $closingBraces1 = ''; }}
  126. {{?}}
  127. {{? $rulesGroup.type }}
  128. }
  129. {{? $typeSchema && $typeSchema === $rulesGroup.type && !$coerceToTypes }}
  130. {{ var $typeChecked = true; }}
  131. else {
  132. {{
  133. var $schemaPath = it.schemaPath + '.type'
  134. , $errSchemaPath = it.errSchemaPath + '/type';
  135. }}
  136. {{# def.error:'type' }}
  137. }
  138. {{?}}
  139. {{?}}
  140. {{? $breakOnError }}
  141. if (errors === {{?$top}}0{{??}}errs_{{=$lvl}}{{?}}) {
  142. {{ $closingBraces2 += '}'; }}
  143. {{?}}
  144. {{?}}
  145. {{~}}
  146. {{?}}
  147. {{? $typeSchema && !$typeChecked && !$coerceToTypes }}
  148. {{# def.checkType }}
  149. {{# def.error:'type' }}
  150. }
  151. {{?}}
  152. {{? $breakOnError }} {{= $closingBraces2 }} {{?}}
  153. {{? $top }}
  154. {{? $async }}
  155. if (errors === 0) return true; {{ /* don't edit, used in replace */ }}
  156. else throw new ValidationError(vErrors); {{ /* don't edit, used in replace */ }}
  157. {{??}}
  158. validate.errors = vErrors; {{ /* don't edit, used in replace */ }}
  159. return errors === 0; {{ /* don't edit, used in replace */ }}
  160. {{?}}
  161. });
  162. return validate;
  163. {{??}}
  164. var {{=$valid}} = errors === errs_{{=$lvl}};
  165. {{?}}
  166. {{# def.cleanUp }}
  167. {{? $top && $breakOnError }}
  168. {{# def.cleanUpVarErrors }}
  169. {{?}}
  170. {{
  171. function $shouldUseGroup($rulesGroup) {
  172. for (var i=0; i < $rulesGroup.rules.length; i++)
  173. if ($shouldUseRule($rulesGroup.rules[i]))
  174. return true;
  175. }
  176. function $shouldUseRule($rule) {
  177. return it.schema[$rule.keyword] !== undefined ||
  178. ( $rule.keyword == 'properties' &&
  179. ( it.schema.additionalProperties === false ||
  180. typeof it.schema.additionalProperties == 'object'
  181. || ( it.schema.patternProperties &&
  182. Object.keys(it.schema.patternProperties).length )
  183. || ( it.opts.v5 && it.schema.patternGroups &&
  184. Object.keys(it.schema.patternGroups).length )));
  185. }
  186. }}