errors.def 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. {{# def.definitions }}
  2. {{## def._error:_rule:
  3. {{ 'istanbul ignore else'; }}
  4. {{? it.createErrors !== false }}
  5. {
  6. keyword: '{{= $errorKeyword || _rule }}'
  7. , dataPath: (dataPath || '') + {{= it.errorPath }}
  8. , schemaPath: {{=it.util.toQuotedString($errSchemaPath)}}
  9. , params: {{# def._errorParams[_rule] }}
  10. {{? it.opts.messages !== false }}
  11. , message: {{# def._errorMessages[_rule] }}
  12. {{?}}
  13. {{? it.opts.verbose }}
  14. , schema: {{# def._errorSchemas[_rule] }}
  15. , parentSchema: validate.schema{{=it.schemaPath}}
  16. , data: {{=$data}}
  17. {{?}}
  18. }
  19. {{??}}
  20. {}
  21. {{?}}
  22. #}}
  23. {{## def._addError:_rule:
  24. if (vErrors === null) vErrors = [err];
  25. else vErrors.push(err);
  26. errors++;
  27. #}}
  28. {{## def.addError:_rule:
  29. var err = {{# def._error:_rule }};
  30. {{# def._addError:_rule }}
  31. #}}
  32. {{## def.error:_rule:
  33. {{# def.beginDefOut}}
  34. {{# def._error:_rule }}
  35. {{# def.storeDefOut:__err }}
  36. {{? !it.compositeRule && $breakOnError }}
  37. {{ 'istanbul ignore if'; }}
  38. {{? it.async }}
  39. throw new ValidationError([{{=__err}}]);
  40. {{??}}
  41. validate.errors = [{{=__err}}];
  42. return false;
  43. {{?}}
  44. {{??}}
  45. var err = {{=__err}};
  46. {{# def._addError:_rule }}
  47. {{?}}
  48. #}}
  49. {{## def.extraError:_rule:
  50. {{# def.addError:_rule}}
  51. {{? !it.compositeRule && $breakOnError }}
  52. {{ 'istanbul ignore if'; }}
  53. {{? it.async }}
  54. throw new ValidationError(vErrors);
  55. {{??}}
  56. validate.errors = vErrors;
  57. return false;
  58. {{?}}
  59. {{?}}
  60. #}}
  61. {{## def.checkError:_rule:
  62. if (!{{=$valid}}) {
  63. {{# def.error:_rule }}
  64. }
  65. #}}
  66. {{## def.resetErrors:
  67. errors = {{=$errs}};
  68. if (vErrors !== null) {
  69. if ({{=$errs}}) vErrors.length = {{=$errs}};
  70. else vErrors = null;
  71. }
  72. #}}
  73. {{## def.concatSchema:{{?$isData}}' + {{=$schemaValue}} + '{{??}}{{=$schema}}{{?}}#}}
  74. {{## def.appendSchema:{{?$isData}}' + {{=$schemaValue}}{{??}}{{=$schemaValue}}'{{?}}#}}
  75. {{## def.concatSchemaEQ:{{?$isData}}' + {{=$schemaValue}} + '{{??}}{{=it.util.escapeQuotes($schema)}}{{?}}#}}
  76. {{## def._errorMessages = {
  77. 'false schema': "'boolean schema is false'",
  78. $ref: "'can\\\'t resolve reference {{=it.util.escapeQuotes($schema)}}'",
  79. additionalItems: "'should NOT have more than {{=$schema.length}} items'",
  80. additionalProperties: "'{{? it.opts._errorDataPathProperty }}is an invalid additional property{{??}}should NOT have additional properties{{?}}'",
  81. anyOf: "'should match some schema in anyOf'",
  82. const: "'should be equal to constant'",
  83. contains: "'should contain a valid item'",
  84. dependencies: "'should have {{? $deps.length == 1 }}property {{= it.util.escapeQuotes($deps[0]) }}{{??}}properties {{= it.util.escapeQuotes($deps.join(\", \")) }}{{?}} when property {{= it.util.escapeQuotes($property) }} is present'",
  85. 'enum': "'should be equal to one of the allowed values'",
  86. format: "'should match format \"{{#def.concatSchemaEQ}}\"'",
  87. 'if': "'should match \"' + {{=$ifClause}} + '\" schema'",
  88. _limit: "'should be {{=$opStr}} {{#def.appendSchema}}",
  89. _exclusiveLimit: "'{{=$exclusiveKeyword}} should be boolean'",
  90. _limitItems: "'should NOT have {{?$keyword=='maxItems'}}more{{??}}fewer{{?}} than {{#def.concatSchema}} items'",
  91. _limitLength: "'should NOT be {{?$keyword=='maxLength'}}longer{{??}}shorter{{?}} than {{#def.concatSchema}} characters'",
  92. _limitProperties:"'should NOT have {{?$keyword=='maxProperties'}}more{{??}}fewer{{?}} than {{#def.concatSchema}} properties'",
  93. multipleOf: "'should be multiple of {{#def.appendSchema}}",
  94. not: "'should NOT be valid'",
  95. oneOf: "'should match exactly one schema in oneOf'",
  96. pattern: "'should match pattern \"{{#def.concatSchemaEQ}}\"'",
  97. propertyNames: "'property name \\'{{=$invalidName}}\\' is invalid'",
  98. required: "'{{? it.opts._errorDataPathProperty }}is a required property{{??}}should have required property \\'{{=$missingProperty}}\\'{{?}}'",
  99. type: "'should be {{? $typeIsArray }}{{= $typeSchema.join(\",\") }}{{??}}{{=$typeSchema}}{{?}}'",
  100. uniqueItems: "'should NOT have duplicate items (items ## ' + j + ' and ' + i + ' are identical)'",
  101. custom: "'should pass \"{{=$rule.keyword}}\" keyword validation'",
  102. patternRequired: "'should have property matching pattern \\'{{=$missingPattern}}\\''",
  103. switch: "'should pass \"switch\" keyword validation'",
  104. _formatLimit: "'should be {{=$opStr}} \"{{#def.concatSchemaEQ}}\"'",
  105. _formatExclusiveLimit: "'{{=$exclusiveKeyword}} should be boolean'"
  106. } #}}
  107. {{## def.schemaRefOrVal: {{?$isData}}validate.schema{{=$schemaPath}}{{??}}{{=$schema}}{{?}} #}}
  108. {{## def.schemaRefOrQS: {{?$isData}}validate.schema{{=$schemaPath}}{{??}}{{=it.util.toQuotedString($schema)}}{{?}} #}}
  109. {{## def._errorSchemas = {
  110. 'false schema': "false",
  111. $ref: "{{=it.util.toQuotedString($schema)}}",
  112. additionalItems: "false",
  113. additionalProperties: "false",
  114. anyOf: "validate.schema{{=$schemaPath}}",
  115. const: "validate.schema{{=$schemaPath}}",
  116. contains: "validate.schema{{=$schemaPath}}",
  117. dependencies: "validate.schema{{=$schemaPath}}",
  118. 'enum': "validate.schema{{=$schemaPath}}",
  119. format: "{{#def.schemaRefOrQS}}",
  120. 'if': "validate.schema{{=$schemaPath}}",
  121. _limit: "{{#def.schemaRefOrVal}}",
  122. _exclusiveLimit: "validate.schema{{=$schemaPath}}",
  123. _limitItems: "{{#def.schemaRefOrVal}}",
  124. _limitLength: "{{#def.schemaRefOrVal}}",
  125. _limitProperties:"{{#def.schemaRefOrVal}}",
  126. multipleOf: "{{#def.schemaRefOrVal}}",
  127. not: "validate.schema{{=$schemaPath}}",
  128. oneOf: "validate.schema{{=$schemaPath}}",
  129. pattern: "{{#def.schemaRefOrQS}}",
  130. propertyNames: "validate.schema{{=$schemaPath}}",
  131. required: "validate.schema{{=$schemaPath}}",
  132. type: "validate.schema{{=$schemaPath}}",
  133. uniqueItems: "{{#def.schemaRefOrVal}}",
  134. custom: "validate.schema{{=$schemaPath}}",
  135. patternRequired: "validate.schema{{=$schemaPath}}",
  136. switch: "validate.schema{{=$schemaPath}}",
  137. _formatLimit: "{{#def.schemaRefOrQS}}",
  138. _formatExclusiveLimit: "validate.schema{{=$schemaPath}}"
  139. } #}}
  140. {{## def.schemaValueQS: {{?$isData}}{{=$schemaValue}}{{??}}{{=it.util.toQuotedString($schema)}}{{?}} #}}
  141. {{## def._errorParams = {
  142. 'false schema': "{}",
  143. $ref: "{ ref: '{{=it.util.escapeQuotes($schema)}}' }",
  144. additionalItems: "{ limit: {{=$schema.length}} }",
  145. additionalProperties: "{ additionalProperty: '{{=$additionalProperty}}' }",
  146. anyOf: "{}",
  147. const: "{ allowedValue: schema{{=$lvl}} }",
  148. contains: "{}",
  149. dependencies: "{ property: '{{= it.util.escapeQuotes($property) }}', missingProperty: '{{=$missingProperty}}', depsCount: {{=$deps.length}}, deps: '{{= it.util.escapeQuotes($deps.length==1 ? $deps[0] : $deps.join(\", \")) }}' }",
  150. 'enum': "{ allowedValues: schema{{=$lvl}} }",
  151. format: "{ format: {{#def.schemaValueQS}} }",
  152. 'if': "{ failingKeyword: {{=$ifClause}} }",
  153. _limit: "{ comparison: {{=$opExpr}}, limit: {{=$schemaValue}}, exclusive: {{=$exclusive}} }",
  154. _exclusiveLimit: "{}",
  155. _limitItems: "{ limit: {{=$schemaValue}} }",
  156. _limitLength: "{ limit: {{=$schemaValue}} }",
  157. _limitProperties:"{ limit: {{=$schemaValue}} }",
  158. multipleOf: "{ multipleOf: {{=$schemaValue}} }",
  159. not: "{}",
  160. oneOf: "{ passingSchemas: {{=$passingSchemas}} }",
  161. pattern: "{ pattern: {{#def.schemaValueQS}} }",
  162. propertyNames: "{ propertyName: '{{=$invalidName}}' }",
  163. required: "{ missingProperty: '{{=$missingProperty}}' }",
  164. type: "{ type: '{{? $typeIsArray }}{{= $typeSchema.join(\",\") }}{{??}}{{=$typeSchema}}{{?}}' }",
  165. uniqueItems: "{ i: i, j: j }",
  166. custom: "{ keyword: '{{=$rule.keyword}}' }",
  167. patternRequired: "{ missingPattern: '{{=$missingPattern}}' }",
  168. switch: "{ caseIndex: {{=$caseIndex}} }",
  169. _formatLimit: "{ comparison: {{=$opExpr}}, limit: {{#def.schemaValueQS}}, exclusive: {{=$exclusive}} }",
  170. _formatExclusiveLimit: "{}"
  171. } #}}