Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

validate.jst 6.7KB

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