properties.jst 9.8 KB

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