validate.jst 7.6 KB

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