validate.jst 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. {{
  16. var $async = it.schema.$async === true
  17. , $refKeywords = it.util.schemaHasRulesExcept(it.schema, it.RULES.all, '$ref')
  18. , $id = it.self._getId(it.schema);
  19. }}
  20. {{? it.isTop }}
  21. {{? $async }}
  22. {{
  23. it.async = true;
  24. var $es7 = it.opts.async == 'es7';
  25. it.yieldAwait = $es7 ? 'await' : 'yield';
  26. }}
  27. {{?}}
  28. var validate =
  29. {{? $async }}
  30. {{? $es7 }}
  31. (async function
  32. {{??}}
  33. {{? it.opts.async != '*'}}co.wrap{{?}}(function*
  34. {{?}}
  35. {{??}}
  36. (function
  37. {{?}}
  38. (data, dataPath, parentData, parentDataProperty, rootData) {
  39. 'use strict';
  40. {{? $id && (it.opts.sourceCode || it.opts.processCode) }}
  41. {{= '/\*# sourceURL=' + $id + ' */' }}
  42. {{?}}
  43. {{?}}
  44. {{? typeof it.schema == 'boolean' || !($refKeywords || it.schema.$ref) }}
  45. {{ var $keyword = 'false schema'; }}
  46. {{# def.setupKeyword }}
  47. {{? it.schema === false}}
  48. {{? it.isTop}}
  49. {{ $breakOnError = true; }}
  50. {{??}}
  51. var {{=$valid}} = false;
  52. {{?}}
  53. {{# def.error:'false schema' }}
  54. {{??}}
  55. {{? it.isTop}}
  56. {{? $async }}
  57. return data;
  58. {{??}}
  59. validate.errors = null;
  60. return true;
  61. {{?}}
  62. {{??}}
  63. var {{=$valid}} = true;
  64. {{?}}
  65. {{?}}
  66. {{? it.isTop}}
  67. });
  68. return validate;
  69. {{?}}
  70. {{ return out; }}
  71. {{?}}
  72. {{? it.isTop }}
  73. {{
  74. var $top = it.isTop
  75. , $lvl = it.level = 0
  76. , $dataLvl = it.dataLevel = 0
  77. , $data = 'data';
  78. it.rootId = it.resolve.fullPath(it.self._getId(it.root.schema));
  79. it.baseId = it.baseId || it.rootId;
  80. delete it.isTop;
  81. it.dataPathArr = [undefined];
  82. }}
  83. var vErrors = null; {{ /* don't edit, used in replace */ }}
  84. var errors = 0; {{ /* don't edit, used in replace */ }}
  85. if (rootData === undefined) rootData = data; {{ /* don't edit, used in replace */ }}
  86. {{??}}
  87. {{
  88. var $lvl = it.level
  89. , $dataLvl = it.dataLevel
  90. , $data = 'data' + ($dataLvl || '');
  91. if ($id) it.baseId = it.resolve.url(it.baseId, $id);
  92. if ($async && !it.async) throw new Error('async schema in sync schema');
  93. }}
  94. var errs_{{=$lvl}} = errors;
  95. {{?}}
  96. {{
  97. var $valid = 'valid' + $lvl
  98. , $breakOnError = !it.opts.allErrors
  99. , $closingBraces1 = ''
  100. , $closingBraces2 = '';
  101. var $errorKeyword;
  102. var $typeSchema = it.schema.type
  103. , $typeIsArray = Array.isArray($typeSchema);
  104. }}
  105. {{## def.checkType:
  106. {{
  107. var $schemaPath = it.schemaPath + '.type'
  108. , $errSchemaPath = it.errSchemaPath + '/type'
  109. , $method = $typeIsArray ? 'checkDataTypes' : 'checkDataType';
  110. }}
  111. if ({{= it.util[$method]($typeSchema, $data, true) }}) {
  112. #}}
  113. {{? $typeSchema && it.opts.coerceTypes }}
  114. {{ var $coerceToTypes = it.util.coerceToTypes(it.opts.coerceTypes, $typeSchema); }}
  115. {{? $coerceToTypes }}
  116. {{# def.checkType }}
  117. {{# def.coerceType }}
  118. }
  119. {{?}}
  120. {{?}}
  121. {{? it.schema.$ref && $refKeywords }}
  122. {{? it.opts.extendRefs == 'fail' }}
  123. {{ throw new Error('$ref: validation keywords used in schema at path "' + it.errSchemaPath + '" (see option extendRefs)'); }}
  124. {{?? it.opts.extendRefs !== true }}
  125. {{
  126. $refKeywords = false;
  127. console.warn('$ref: keywords ignored in schema at path "' + it.errSchemaPath + '"');
  128. }}
  129. {{?}}
  130. {{?}}
  131. {{? it.schema.$ref && !$refKeywords }}
  132. {{= it.RULES.all.$ref.code(it, '$ref') }}
  133. {{? $breakOnError }}
  134. }
  135. if (errors === {{?$top}}0{{??}}errs_{{=$lvl}}{{?}}) {
  136. {{ $closingBraces2 += '}'; }}
  137. {{?}}
  138. {{??}}
  139. {{? it.opts.v5 && it.schema.patternGroups }}
  140. {{ console.warn('keyword "patternGroups" is deprecated and disabled. Use option patternGroups: true to enable.'); }}
  141. {{?}}
  142. {{~ it.RULES:$rulesGroup }}
  143. {{? $shouldUseGroup($rulesGroup) }}
  144. {{? $rulesGroup.type }}
  145. if ({{= it.util.checkDataType($rulesGroup.type, $data) }}) {
  146. {{?}}
  147. {{? it.opts.useDefaults && !it.compositeRule }}
  148. {{? $rulesGroup.type == 'object' && it.schema.properties }}
  149. {{# def.defaultProperties }}
  150. {{?? $rulesGroup.type == 'array' && Array.isArray(it.schema.items) }}
  151. {{# def.defaultItems }}
  152. {{?}}
  153. {{?}}
  154. {{~ $rulesGroup.rules:$rule }}
  155. {{? $shouldUseRule($rule) }}
  156. {{ var $code = $rule.code(it, $rule.keyword, $rulesGroup.type); }}
  157. {{? $code }}
  158. {{= $code }}
  159. {{? $breakOnError }}
  160. {{ $closingBraces1 += '}'; }}
  161. {{?}}
  162. {{?}}
  163. {{?}}
  164. {{~}}
  165. {{? $breakOnError }}
  166. {{= $closingBraces1 }}
  167. {{ $closingBraces1 = ''; }}
  168. {{?}}
  169. {{? $rulesGroup.type }}
  170. }
  171. {{? $typeSchema && $typeSchema === $rulesGroup.type }}
  172. {{ var $typeChecked = true; }}
  173. else {
  174. {{
  175. var $schemaPath = it.schemaPath + '.type'
  176. , $errSchemaPath = it.errSchemaPath + '/type';
  177. }}
  178. {{# def.error:'type' }}
  179. }
  180. {{?}}
  181. {{?}}
  182. {{? $breakOnError }}
  183. if (errors === {{?$top}}0{{??}}errs_{{=$lvl}}{{?}}) {
  184. {{ $closingBraces2 += '}'; }}
  185. {{?}}
  186. {{?}}
  187. {{~}}
  188. {{?}}
  189. {{? $typeSchema && !$typeChecked && !(it.opts.coerceTypes && $coerceToTypes) }}
  190. {{# def.checkType }}
  191. {{# def.error:'type' }}
  192. }
  193. {{?}}
  194. {{? $breakOnError }} {{= $closingBraces2 }} {{?}}
  195. {{? $top }}
  196. {{? $async }}
  197. if (errors === 0) return data; {{ /* don't edit, used in replace */ }}
  198. else throw new ValidationError(vErrors); {{ /* don't edit, used in replace */ }}
  199. {{??}}
  200. validate.errors = vErrors; {{ /* don't edit, used in replace */ }}
  201. return errors === 0; {{ /* don't edit, used in replace */ }}
  202. {{?}}
  203. });
  204. return validate;
  205. {{??}}
  206. var {{=$valid}} = errors === errs_{{=$lvl}};
  207. {{?}}
  208. {{# def.cleanUp }}
  209. {{? $top }}
  210. {{# def.finalCleanUp }}
  211. {{?}}
  212. {{
  213. function $shouldUseGroup($rulesGroup) {
  214. var rules = $rulesGroup.rules;
  215. for (var i=0; i < rules.length; i++)
  216. if ($shouldUseRule(rules[i]))
  217. return true;
  218. }
  219. function $shouldUseRule($rule) {
  220. return it.schema[$rule.keyword] !== undefined ||
  221. ($rule.implements && $ruleImlementsSomeKeyword($rule));
  222. }
  223. function $ruleImlementsSomeKeyword($rule) {
  224. var impl = $rule.implements;
  225. for (var i=0; i < impl.length; i++)
  226. if (it.schema[impl[i]] !== undefined)
  227. return true;
  228. }
  229. }}