properties.jst 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 || {}).filter(notProto)
  26. , $pProperties = it.schema.patternProperties || {}
  27. , $pPropertyKeys = Object.keys($pProperties).filter(notProto)
  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.$data && $required.$data) && $required.length < it.opts.loopRequired) {
  39. var $requiredHash = it.util.toHash($required);
  40. }
  41. function notProto(p) { return p !== '__proto__'; }
  42. }}
  43. var {{=$errs}} = errors;
  44. var {{=$nextValid}} = true;
  45. {{? $ownProperties }}
  46. var {{=$dataProperties}} = undefined;
  47. {{?}}
  48. {{? $checkAdditional }}
  49. {{# def.iterateProperties }}
  50. {{? $someProperties }}
  51. var isAdditional{{=$lvl}} = !(false
  52. {{? $schemaKeys.length }}
  53. {{? $schemaKeys.length > 8 }}
  54. || validate.schema{{=$schemaPath}}.hasOwnProperty({{=$key}})
  55. {{??}}
  56. {{~ $schemaKeys:$propertyKey }}
  57. || {{=$key}} == {{= it.util.toQuotedString($propertyKey) }}
  58. {{~}}
  59. {{?}}
  60. {{?}}
  61. {{? $pPropertyKeys.length }}
  62. {{~ $pPropertyKeys:$pProperty:$i }}
  63. || {{= it.usePattern($pProperty) }}.test({{=$key}})
  64. {{~}}
  65. {{?}}
  66. );
  67. if (isAdditional{{=$lvl}}) {
  68. {{?}}
  69. {{? $removeAdditional == 'all' }}
  70. delete {{=$data}}[{{=$key}}];
  71. {{??}}
  72. {{
  73. var $currentErrorPath = it.errorPath;
  74. var $additionalProperty = '\' + ' + $key + ' + \'';
  75. if (it.opts._errorDataPathProperty) {
  76. it.errorPath = it.util.getPathExpr(it.errorPath, $key, it.opts.jsonPointers);
  77. }
  78. }}
  79. {{? $noAdditional }}
  80. {{? $removeAdditional }}
  81. delete {{=$data}}[{{=$key}}];
  82. {{??}}
  83. {{=$nextValid}} = false;
  84. {{
  85. var $currErrSchemaPath = $errSchemaPath;
  86. $errSchemaPath = it.errSchemaPath + '/additionalProperties';
  87. }}
  88. {{# def.error:'additionalProperties' }}
  89. {{ $errSchemaPath = $currErrSchemaPath; }}
  90. {{? $breakOnError }} break; {{?}}
  91. {{?}}
  92. {{?? $additionalIsSchema }}
  93. {{? $removeAdditional == 'failing' }}
  94. var {{=$errs}} = errors;
  95. {{# def.setCompositeRule }}
  96. {{# def.validateAdditional }}
  97. if (!{{=$nextValid}}) {
  98. errors = {{=$errs}};
  99. if (validate.errors !== null) {
  100. if (errors) validate.errors.length = errors;
  101. else validate.errors = null;
  102. }
  103. delete {{=$data}}[{{=$key}}];
  104. }
  105. {{# def.resetCompositeRule }}
  106. {{??}}
  107. {{# def.validateAdditional }}
  108. {{? $breakOnError }} if (!{{=$nextValid}}) break; {{?}}
  109. {{?}}
  110. {{?}}
  111. {{ it.errorPath = $currentErrorPath; }}
  112. {{?}}
  113. {{? $someProperties }}
  114. }
  115. {{?}}
  116. }
  117. {{# def.ifResultValid }}
  118. {{?}}
  119. {{ var $useDefaults = it.opts.useDefaults && !it.compositeRule; }}
  120. {{? $schemaKeys.length }}
  121. {{~ $schemaKeys:$propertyKey }}
  122. {{ var $sch = $schema[$propertyKey]; }}
  123. {{? {{# def.nonEmptySchema:$sch}} }}
  124. {{
  125. var $prop = it.util.getProperty($propertyKey)
  126. , $passData = $data + $prop
  127. , $hasDefault = $useDefaults && $sch.default !== undefined;
  128. $it.schema = $sch;
  129. $it.schemaPath = $schemaPath + $prop;
  130. $it.errSchemaPath = $errSchemaPath + '/' + it.util.escapeFragment($propertyKey);
  131. $it.errorPath = it.util.getPath(it.errorPath, $propertyKey, it.opts.jsonPointers);
  132. $it.dataPathArr[$dataNxt] = it.util.toQuotedString($propertyKey);
  133. }}
  134. {{# def.generateSubschemaCode }}
  135. {{? {{# def.willOptimize }} }}
  136. {{
  137. $code = {{# def._optimizeValidate }};
  138. var $useData = $passData;
  139. }}
  140. {{??}}
  141. {{ var $useData = $nextData; }}
  142. var {{=$nextData}} = {{=$passData}};
  143. {{?}}
  144. {{? $hasDefault }}
  145. {{= $code }}
  146. {{??}}
  147. {{? $requiredHash && $requiredHash[$propertyKey] }}
  148. if ({{# def.noPropertyInData }}) {
  149. {{=$nextValid}} = false;
  150. {{
  151. var $currentErrorPath = it.errorPath
  152. , $currErrSchemaPath = $errSchemaPath
  153. , $missingProperty = it.util.escapeQuotes($propertyKey);
  154. if (it.opts._errorDataPathProperty) {
  155. it.errorPath = it.util.getPath($currentErrorPath, $propertyKey, it.opts.jsonPointers);
  156. }
  157. $errSchemaPath = it.errSchemaPath + '/required';
  158. }}
  159. {{# def.error:'required' }}
  160. {{ $errSchemaPath = $currErrSchemaPath; }}
  161. {{ it.errorPath = $currentErrorPath; }}
  162. } else {
  163. {{??}}
  164. {{? $breakOnError }}
  165. if ({{# def.noPropertyInData }}) {
  166. {{=$nextValid}} = true;
  167. } else {
  168. {{??}}
  169. if ({{=$useData}} !== undefined
  170. {{? $ownProperties }}
  171. && {{# def.isOwnProperty }}
  172. {{?}}
  173. ) {
  174. {{?}}
  175. {{?}}
  176. {{= $code }}
  177. }
  178. {{?}} {{ /* $hasDefault */ }}
  179. {{?}} {{ /* def.nonEmptySchema */ }}
  180. {{# def.ifResultValid }}
  181. {{~}}
  182. {{?}}
  183. {{? $pPropertyKeys.length }}
  184. {{~ $pPropertyKeys:$pProperty }}
  185. {{ var $sch = $pProperties[$pProperty]; }}
  186. {{? {{# def.nonEmptySchema:$sch}} }}
  187. {{
  188. $it.schema = $sch;
  189. $it.schemaPath = it.schemaPath + '.patternProperties' + it.util.getProperty($pProperty);
  190. $it.errSchemaPath = it.errSchemaPath + '/patternProperties/'
  191. + it.util.escapeFragment($pProperty);
  192. }}
  193. {{# def.iterateProperties }}
  194. if ({{= it.usePattern($pProperty) }}.test({{=$key}})) {
  195. {{
  196. $it.errorPath = it.util.getPathExpr(it.errorPath, $key, it.opts.jsonPointers);
  197. var $passData = $data + '[' + $key + ']';
  198. $it.dataPathArr[$dataNxt] = $key;
  199. }}
  200. {{# def.generateSubschemaCode }}
  201. {{# def.optimizeValidate }}
  202. {{? $breakOnError }} if (!{{=$nextValid}}) break; {{?}}
  203. }
  204. {{? $breakOnError }} else {{=$nextValid}} = true; {{?}}
  205. }
  206. {{# def.ifResultValid }}
  207. {{?}} {{ /* def.nonEmptySchema */ }}
  208. {{~}}
  209. {{?}}
  210. {{? $breakOnError }}
  211. {{= $closingBraces }}
  212. if ({{=$errs}} == errors) {
  213. {{?}}