validate.jst 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. if ($typeIsArray && $typeSchema.length == 1) {
  105. $typeSchema = $typeSchema[0];
  106. $typeIsArray = false;
  107. }
  108. }}
  109. {{## def.checkType:
  110. {{
  111. var $schemaPath = it.schemaPath + '.type'
  112. , $errSchemaPath = it.errSchemaPath + '/type'
  113. , $method = $typeIsArray ? 'checkDataTypes' : 'checkDataType';
  114. }}
  115. if ({{= it.util[$method]($typeSchema, $data, true) }}) {
  116. #}}
  117. {{? it.schema.$ref && $refKeywords }}
  118. {{? it.opts.extendRefs == 'fail' }}
  119. {{ throw new Error('$ref: validation keywords used in schema at path "' + it.errSchemaPath + '" (see option extendRefs)'); }}
  120. {{?? it.opts.extendRefs !== true }}
  121. {{
  122. $refKeywords = false;
  123. it.logger.warn('$ref: keywords ignored in schema at path "' + it.errSchemaPath + '"');
  124. }}
  125. {{?}}
  126. {{?}}
  127. {{? $typeSchema }}
  128. {{? it.opts.coerceTypes }}
  129. {{ var $coerceToTypes = it.util.coerceToTypes(it.opts.coerceTypes, $typeSchema); }}
  130. {{?}}
  131. {{ var $rulesGroup = it.RULES.types[$typeSchema]; }}
  132. {{? $coerceToTypes || $typeIsArray || $rulesGroup === true ||
  133. ($rulesGroup && !$shouldUseGroup($rulesGroup)) }}
  134. {{
  135. var $schemaPath = it.schemaPath + '.type'
  136. , $errSchemaPath = it.errSchemaPath + '/type';
  137. }}
  138. {{# def.checkType }}
  139. {{? $coerceToTypes }}
  140. {{# def.coerceType }}
  141. {{??}}
  142. {{# def.error:'type' }}
  143. {{?}}
  144. }
  145. {{?}}
  146. {{?}}
  147. {{? it.schema.$ref && !$refKeywords }}
  148. {{= it.RULES.all.$ref.code(it, '$ref') }}
  149. {{? $breakOnError }}
  150. }
  151. if (errors === {{?$top}}0{{??}}errs_{{=$lvl}}{{?}}) {
  152. {{ $closingBraces2 += '}'; }}
  153. {{?}}
  154. {{??}}
  155. {{? it.opts.v5 && it.schema.patternGroups }}
  156. {{ it.logger.warn('keyword "patternGroups" is deprecated and disabled. Use option patternGroups: true to enable.'); }}
  157. {{?}}
  158. {{~ it.RULES:$rulesGroup }}
  159. {{? $shouldUseGroup($rulesGroup) }}
  160. {{? $rulesGroup.type }}
  161. if ({{= it.util.checkDataType($rulesGroup.type, $data) }}) {
  162. {{?}}
  163. {{? it.opts.useDefaults && !it.compositeRule }}
  164. {{? $rulesGroup.type == 'object' && it.schema.properties }}
  165. {{# def.defaultProperties }}
  166. {{?? $rulesGroup.type == 'array' && Array.isArray(it.schema.items) }}
  167. {{# def.defaultItems }}
  168. {{?}}
  169. {{?}}
  170. {{~ $rulesGroup.rules:$rule }}
  171. {{? $shouldUseRule($rule) }}
  172. {{ var $code = $rule.code(it, $rule.keyword, $rulesGroup.type); }}
  173. {{? $code }}
  174. {{= $code }}
  175. {{? $breakOnError }}
  176. {{ $closingBraces1 += '}'; }}
  177. {{?}}
  178. {{?}}
  179. {{?}}
  180. {{~}}
  181. {{? $breakOnError }}
  182. {{= $closingBraces1 }}
  183. {{ $closingBraces1 = ''; }}
  184. {{?}}
  185. {{? $rulesGroup.type }}
  186. }
  187. {{? $typeSchema && $typeSchema === $rulesGroup.type && !$coerceToTypes }}
  188. else {
  189. {{
  190. var $schemaPath = it.schemaPath + '.type'
  191. , $errSchemaPath = it.errSchemaPath + '/type';
  192. }}
  193. {{# def.error:'type' }}
  194. }
  195. {{?}}
  196. {{?}}
  197. {{? $breakOnError }}
  198. if (errors === {{?$top}}0{{??}}errs_{{=$lvl}}{{?}}) {
  199. {{ $closingBraces2 += '}'; }}
  200. {{?}}
  201. {{?}}
  202. {{~}}
  203. {{?}}
  204. {{? $breakOnError }} {{= $closingBraces2 }} {{?}}
  205. {{? $top }}
  206. {{? $async }}
  207. if (errors === 0) return data; {{ /* don't edit, used in replace */ }}
  208. else throw new ValidationError(vErrors); {{ /* don't edit, used in replace */ }}
  209. {{??}}
  210. validate.errors = vErrors; {{ /* don't edit, used in replace */ }}
  211. return errors === 0; {{ /* don't edit, used in replace */ }}
  212. {{?}}
  213. });
  214. return validate;
  215. {{??}}
  216. var {{=$valid}} = errors === errs_{{=$lvl}};
  217. {{?}}
  218. {{# def.cleanUp }}
  219. {{? $top }}
  220. {{# def.finalCleanUp }}
  221. {{?}}
  222. {{
  223. function $shouldUseGroup($rulesGroup) {
  224. var rules = $rulesGroup.rules;
  225. for (var i=0; i < rules.length; i++)
  226. if ($shouldUseRule(rules[i]))
  227. return true;
  228. }
  229. function $shouldUseRule($rule) {
  230. return it.schema[$rule.keyword] !== undefined ||
  231. ($rule.implements && $ruleImplementsSomeKeyword($rule));
  232. }
  233. function $ruleImplementsSomeKeyword($rule) {
  234. var impl = $rule.implements;
  235. for (var i=0; i < impl.length; i++)
  236. if (it.schema[impl[i]] !== undefined)
  237. return true;
  238. }
  239. }}