validate.jst 7.0 KB

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