properties.jst 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. {{# def.definitions }}
  2. {{# def.errors }}
  3. {{# def.setupKeyword }}
  4. {{# def.setupNextLevel }}
  5. {{## def.validateAdditional:
  6. {{ /* additionalProperties is schema */
  7. $it.schema = $aProperties;
  8. $it.schemaPath = it.schemaPath + '.additionalProperties';
  9. $it.errSchemaPath = it.errSchemaPath + '/additionalProperties';
  10. $it.errorPath = it.opts._errorDataPathProperty
  11. ? it.errorPath
  12. : it.util.getPathExpr(it.errorPath, $key, it.opts.jsonPointers);
  13. var $passData = $data + '[' + $key + ']';
  14. $it.dataPathArr[$dataNxt] = $key;
  15. }}
  16. {{# def.generateSubschemaCode }}
  17. {{# def.optimizeValidate }}
  18. #}}
  19. {{
  20. var $key = 'key' + $lvl
  21. , $idx = 'idx' + $lvl
  22. , $dataNxt = $it.dataLevel = it.dataLevel + 1
  23. , $nextData = 'data' + $dataNxt
  24. , $dataProperties = 'dataProperties' + $lvl;
  25. var $schemaKeys = Object.keys($schema || {})
  26. , $pProperties = it.schema.patternProperties || {}
  27. , $pPropertyKeys = Object.keys($pProperties)
  28. , $aProperties = it.schema.additionalProperties
  29. , $someProperties = $schemaKeys.length || $pPropertyKeys.length
  30. , $noAdditional = $aProperties === false
  31. , $additionalIsSchema = typeof $aProperties == 'object'
  32. && Object.keys($aProperties).length
  33. , $removeAdditional = it.opts.removeAdditional
  34. , $checkAdditional = $noAdditional || $additionalIsSchema || $removeAdditional
  35. , $ownProperties = it.opts.ownProperties
  36. , $currentBaseId = it.baseId;
  37. var $required = it.schema.required;
  38. if ($required && !(it.opts.v5 && $required.$data) && $required.length < it.opts.loopRequired)
  39. var $requiredHash = it.util.toHash($required);
  40. if (it.opts.patternGroups) {
  41. var $pgProperties = it.schema.patternGroups || {}
  42. , $pgPropertyKeys = Object.keys($pgProperties);
  43. }
  44. }}
  45. var {{=$errs}} = errors;
  46. var {{=$nextValid}} = true;
  47. {{? $ownProperties }}
  48. var {{=$dataProperties}} = undefined;
  49. {{?}}
  50. {{? $checkAdditional }}
  51. {{# def.iterateProperties }}
  52. {{? $someProperties }}
  53. var isAdditional{{=$lvl}} = !(false
  54. {{? $schemaKeys.length }}
  55. {{? $schemaKeys.length > 5 }}
  56. || validate.schema{{=$schemaPath}}[{{=$key}}]
  57. {{??}}
  58. {{~ $schemaKeys:$propertyKey }}
  59. || {{=$key}} == {{= it.util.toQuotedString($propertyKey) }}
  60. {{~}}
  61. {{?}}
  62. {{?}}
  63. {{? $pPropertyKeys.length }}
  64. {{~ $pPropertyKeys:$pProperty:$i }}
  65. || {{= it.usePattern($pProperty) }}.test({{=$key}})
  66. {{~}}
  67. {{?}}
  68. {{? it.opts.patternGroups && $pgPropertyKeys.length }}
  69. {{~ $pgPropertyKeys:$pgProperty:$i }}
  70. || {{= it.usePattern($pgProperty) }}.test({{=$key}})
  71. {{~}}
  72. {{?}}
  73. );
  74. if (isAdditional{{=$lvl}}) {
  75. {{?}}
  76. {{? $removeAdditional == 'all' }}
  77. delete {{=$data}}[{{=$key}}];
  78. {{??}}
  79. {{
  80. var $currentErrorPath = it.errorPath;
  81. var $additionalProperty = '\' + ' + $key + ' + \'';
  82. if (it.opts._errorDataPathProperty) {
  83. it.errorPath = it.util.getPathExpr(it.errorPath, $key, it.opts.jsonPointers);
  84. }
  85. }}
  86. {{? $noAdditional }}
  87. {{? $removeAdditional }}
  88. delete {{=$data}}[{{=$key}}];
  89. {{??}}
  90. {{=$nextValid}} = false;
  91. {{
  92. var $currErrSchemaPath = $errSchemaPath;
  93. $errSchemaPath = it.errSchemaPath + '/additionalProperties';
  94. }}
  95. {{# def.error:'additionalProperties' }}
  96. {{ $errSchemaPath = $currErrSchemaPath; }}
  97. {{? $breakOnError }} break; {{?}}
  98. {{?}}
  99. {{?? $additionalIsSchema }}
  100. {{? $removeAdditional == 'failing' }}
  101. var {{=$errs}} = errors;
  102. {{# def.setCompositeRule }}
  103. {{# def.validateAdditional }}
  104. if (!{{=$nextValid}}) {
  105. errors = {{=$errs}};
  106. if (validate.errors !== null) {
  107. if (errors) validate.errors.length = errors;
  108. else validate.errors = null;
  109. }
  110. delete {{=$data}}[{{=$key}}];
  111. }
  112. {{# def.resetCompositeRule }}
  113. {{??}}
  114. {{# def.validateAdditional }}
  115. {{? $breakOnError }} if (!{{=$nextValid}}) break; {{?}}
  116. {{?}}
  117. {{?}}
  118. {{ it.errorPath = $currentErrorPath; }}
  119. {{?}}
  120. {{? $someProperties }}
  121. }
  122. {{?}}
  123. }
  124. {{# def.ifResultValid }}
  125. {{?}}
  126. {{ var $useDefaults = it.opts.useDefaults && !it.compositeRule; }}
  127. {{? $schemaKeys.length }}
  128. {{~ $schemaKeys:$propertyKey }}
  129. {{ var $sch = $schema[$propertyKey]; }}
  130. {{? {{# def.nonEmptySchema:$sch}} }}
  131. {{
  132. var $prop = it.util.getProperty($propertyKey)
  133. , $passData = $data + $prop
  134. , $hasDefault = $useDefaults && $sch.default !== undefined;
  135. $it.schema = $sch;
  136. $it.schemaPath = $schemaPath + $prop;
  137. $it.errSchemaPath = $errSchemaPath + '/' + it.util.escapeFragment($propertyKey);
  138. $it.errorPath = it.util.getPath(it.errorPath, $propertyKey, it.opts.jsonPointers);
  139. $it.dataPathArr[$dataNxt] = it.util.toQuotedString($propertyKey);
  140. }}
  141. {{# def.generateSubschemaCode }}
  142. {{? {{# def.willOptimize }} }}
  143. {{
  144. $code = {{# def._optimizeValidate }};
  145. var $useData = $passData;
  146. }}
  147. {{??}}
  148. {{ var $useData = $nextData; }}
  149. var {{=$nextData}} = {{=$passData}};
  150. {{?}}
  151. {{? $hasDefault }}
  152. {{= $code }}
  153. {{??}}
  154. {{? $requiredHash && $requiredHash[$propertyKey] }}
  155. if ({{# def.noPropertyInData }}) {
  156. {{=$nextValid}} = false;
  157. {{
  158. var $currentErrorPath = it.errorPath
  159. , $currErrSchemaPath = $errSchemaPath
  160. , $missingProperty = it.util.escapeQuotes($propertyKey);
  161. if (it.opts._errorDataPathProperty) {
  162. it.errorPath = it.util.getPath($currentErrorPath, $propertyKey, it.opts.jsonPointers);
  163. }
  164. $errSchemaPath = it.errSchemaPath + '/required';
  165. }}
  166. {{# def.error:'required' }}
  167. {{ $errSchemaPath = $currErrSchemaPath; }}
  168. {{ it.errorPath = $currentErrorPath; }}
  169. } else {
  170. {{??}}
  171. {{? $breakOnError }}
  172. if ({{# def.noPropertyInData }}) {
  173. {{=$nextValid}} = true;
  174. } else {
  175. {{??}}
  176. if ({{=$useData}} !== undefined
  177. {{? $ownProperties }}
  178. && {{# def.isOwnProperty }}
  179. {{?}}
  180. ) {
  181. {{?}}
  182. {{?}}
  183. {{= $code }}
  184. }
  185. {{?}} {{ /* $hasDefault */ }}
  186. {{?}} {{ /* def.nonEmptySchema */ }}
  187. {{# def.ifResultValid }}
  188. {{~}}
  189. {{?}}
  190. {{? $pPropertyKeys.length }}
  191. {{~ $pPropertyKeys:$pProperty }}
  192. {{ var $sch = $pProperties[$pProperty]; }}
  193. {{? {{# def.nonEmptySchema:$sch}} }}
  194. {{
  195. $it.schema = $sch;
  196. $it.schemaPath = it.schemaPath + '.patternProperties' + it.util.getProperty($pProperty);
  197. $it.errSchemaPath = it.errSchemaPath + '/patternProperties/'
  198. + it.util.escapeFragment($pProperty);
  199. }}
  200. {{# def.iterateProperties }}
  201. if ({{= it.usePattern($pProperty) }}.test({{=$key}})) {
  202. {{
  203. $it.errorPath = it.util.getPathExpr(it.errorPath, $key, it.opts.jsonPointers);
  204. var $passData = $data + '[' + $key + ']';
  205. $it.dataPathArr[$dataNxt] = $key;
  206. }}
  207. {{# def.generateSubschemaCode }}
  208. {{# def.optimizeValidate }}
  209. {{? $breakOnError }} if (!{{=$nextValid}}) break; {{?}}
  210. }
  211. {{? $breakOnError }} else {{=$nextValid}} = true; {{?}}
  212. }
  213. {{# def.ifResultValid }}
  214. {{?}} {{ /* def.nonEmptySchema */ }}
  215. {{~}}
  216. {{?}}
  217. {{? it.opts.patternGroups && $pgPropertyKeys.length }}
  218. {{~ $pgPropertyKeys:$pgProperty }}
  219. {{
  220. var $pgSchema = $pgProperties[$pgProperty]
  221. , $sch = $pgSchema.schema;
  222. }}
  223. {{? {{# def.nonEmptySchema:$sch}} }}
  224. {{
  225. $it.schema = $sch;
  226. $it.schemaPath = it.schemaPath + '.patternGroups' + it.util.getProperty($pgProperty) + '.schema';
  227. $it.errSchemaPath = it.errSchemaPath + '/patternGroups/'
  228. + it.util.escapeFragment($pgProperty)
  229. + '/schema';
  230. }}
  231. var pgPropCount{{=$lvl}} = 0;
  232. {{# def.iterateProperties }}
  233. if ({{= it.usePattern($pgProperty) }}.test({{=$key}})) {
  234. pgPropCount{{=$lvl}}++;
  235. {{
  236. $it.errorPath = it.util.getPathExpr(it.errorPath, $key, it.opts.jsonPointers);
  237. var $passData = $data + '[' + $key + ']';
  238. $it.dataPathArr[$dataNxt] = $key;
  239. }}
  240. {{# def.generateSubschemaCode }}
  241. {{# def.optimizeValidate }}
  242. {{? $breakOnError }} if (!{{=$nextValid}}) break; {{?}}
  243. }
  244. {{? $breakOnError }} else {{=$nextValid}} = true; {{?}}
  245. }
  246. {{# def.ifResultValid }}
  247. {{
  248. var $pgMin = $pgSchema.minimum
  249. , $pgMax = $pgSchema.maximum;
  250. }}
  251. {{? $pgMin !== undefined || $pgMax !== undefined }}
  252. var {{=$valid}} = true;
  253. {{ var $currErrSchemaPath = $errSchemaPath; }}
  254. {{? $pgMin !== undefined }}
  255. {{ var $limit = $pgMin, $reason = 'minimum', $moreOrLess = 'less'; }}
  256. {{=$valid}} = pgPropCount{{=$lvl}} >= {{=$pgMin}};
  257. {{ $errSchemaPath = it.errSchemaPath + '/patternGroups/minimum'; }}
  258. {{# def.checkError:'patternGroups' }}
  259. {{? $pgMax !== undefined }}
  260. else
  261. {{?}}
  262. {{?}}
  263. {{? $pgMax !== undefined }}
  264. {{ var $limit = $pgMax, $reason = 'maximum', $moreOrLess = 'more'; }}
  265. {{=$valid}} = pgPropCount{{=$lvl}} <= {{=$pgMax}};
  266. {{ $errSchemaPath = it.errSchemaPath + '/patternGroups/maximum'; }}
  267. {{# def.checkError:'patternGroups' }}
  268. {{?}}
  269. {{ $errSchemaPath = $currErrSchemaPath; }}
  270. {{# def.ifValid }}
  271. {{?}}
  272. {{?}} {{ /* def.nonEmptySchema */ }}
  273. {{~}}
  274. {{?}}
  275. {{? $breakOnError }}
  276. {{= $closingBraces }}
  277. if ({{=$errs}} == errors) {
  278. {{?}}
  279. {{# def.cleanUp }}