errors.def 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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}}{{??}}{{=$schema}}'{{?}}#}}
  75. {{## def.concatSchemaEQ:{{?$isData}}' + {{=$schemaValue}} + '{{??}}{{=it.util.escapeQuotes($schema)}}{{?}}#}}
  76. {{## def._errorMessages = {
  77. $ref: "'can\\\'t resolve reference {{=it.util.escapeQuotes($schema)}}'",
  78. additionalItems: "'should NOT have more than {{=$schema.length}} items'",
  79. additionalProperties: "'should NOT have additional properties'",
  80. anyOf: "'should match some schema in anyOf'",
  81. 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'",
  82. 'enum': "'should be equal to one of the allowed values'",
  83. format: "'should match format \"{{#def.concatSchemaEQ}}\"'",
  84. _limit: "'should be {{=$opStr}} {{#def.appendSchema}}",
  85. _exclusiveLimit: "'{{=$exclusiveKeyword}} should be boolean'",
  86. _limitItems: "'should NOT have {{?$keyword=='maxItems'}}more{{??}}less{{?}} than {{#def.concatSchema}} items'",
  87. _limitLength: "'should NOT be {{?$keyword=='maxLength'}}longer{{??}}shorter{{?}} than {{#def.concatSchema}} characters'",
  88. _limitProperties:"'should NOT have {{?$keyword=='maxProperties'}}more{{??}}less{{?}} than {{#def.concatSchema}} properties'",
  89. multipleOf: "'should be multiple of {{#def.appendSchema}}",
  90. not: "'should NOT be valid'",
  91. oneOf: "'should match exactly one schema in oneOf'",
  92. pattern: "'should match pattern \"{{#def.concatSchemaEQ}}\"'",
  93. required: "'{{? it.opts._errorDataPathProperty }}is a required property{{??}}should have required property \\'{{=$missingProperty}}\\'{{?}}'",
  94. type: "'should be {{? $typeIsArray }}{{= $typeSchema.join(\",\") }}{{??}}{{=$typeSchema}}{{?}}'",
  95. uniqueItems: "'should NOT have duplicate items (items ## ' + j + ' and ' + i + ' are identical)'",
  96. custom: "'should pass \"{{=$rule.keyword}}\" keyword validation'",
  97. patternGroups: "'should NOT have {{=$moreOrLess}} than {{=$limit}} properties matching pattern \"{{=it.util.escapeQuotes($pgProperty)}}\"'",
  98. patternRequired: "'should have property matching pattern \\'{{=$missingPattern}}\\''",
  99. switch: "'should pass \"switch\" keyword validation'",
  100. constant: "'should be equal to constant'",
  101. _formatLimit: "'should be {{=$opStr}} \"{{#def.concatSchemaEQ}}\"'",
  102. _formatExclusiveLimit: "'{{=$exclusiveKeyword}} should be boolean'"
  103. } #}}
  104. {{## def.schemaRefOrVal: {{?$isData}}validate.schema{{=$schemaPath}}{{??}}{{=$schema}}{{?}} #}}
  105. {{## def.schemaRefOrQS: {{?$isData}}validate.schema{{=$schemaPath}}{{??}}{{=it.util.toQuotedString($schema)}}{{?}} #}}
  106. {{## def._errorSchemas = {
  107. $ref: "{{=it.util.toQuotedString($schema)}}",
  108. additionalItems: "false",
  109. additionalProperties: "false",
  110. anyOf: "validate.schema{{=$schemaPath}}",
  111. dependencies: "validate.schema{{=$schemaPath}}",
  112. 'enum': "validate.schema{{=$schemaPath}}",
  113. format: "{{#def.schemaRefOrQS}}",
  114. _limit: "{{#def.schemaRefOrVal}}",
  115. _exclusiveLimit: "validate.schema{{=$schemaPath}}",
  116. _limitItems: "{{#def.schemaRefOrVal}}",
  117. _limitLength: "{{#def.schemaRefOrVal}}",
  118. _limitProperties:"{{#def.schemaRefOrVal}}",
  119. multipleOf: "{{#def.schemaRefOrVal}}",
  120. not: "validate.schema{{=$schemaPath}}",
  121. oneOf: "validate.schema{{=$schemaPath}}",
  122. pattern: "{{#def.schemaRefOrQS}}",
  123. required: "validate.schema{{=$schemaPath}}",
  124. type: "validate.schema{{=$schemaPath}}",
  125. uniqueItems: "{{#def.schemaRefOrVal}}",
  126. custom: "validate.schema{{=$schemaPath}}",
  127. patternGroups: "validate.schema{{=$schemaPath}}",
  128. patternRequired: "validate.schema{{=$schemaPath}}",
  129. switch: "validate.schema{{=$schemaPath}}",
  130. constant: "validate.schema{{=$schemaPath}}",
  131. _formatLimit: "{{#def.schemaRefOrQS}}",
  132. _formatExclusiveLimit: "validate.schema{{=$schemaPath}}"
  133. } #}}
  134. {{## def.schemaValueQS: {{?$isData}}{{=$schemaValue}}{{??}}{{=it.util.toQuotedString($schema)}}{{?}} #}}
  135. {{## def._errorParams = {
  136. $ref: "{ ref: '{{=it.util.escapeQuotes($schema)}}' }",
  137. additionalItems: "{ limit: {{=$schema.length}} }",
  138. additionalProperties: "{ additionalProperty: '{{=$additionalProperty}}' }",
  139. anyOf: "{}",
  140. dependencies: "{ property: '{{= it.util.escapeQuotes($property) }}', missingProperty: '{{=$missingProperty}}', depsCount: {{=$deps.length}}, deps: '{{= it.util.escapeQuotes($deps.length==1 ? $deps[0] : $deps.join(\", \")) }}' }",
  141. 'enum': "{ allowedValues: schema{{=$lvl}} }",
  142. format: "{ format: {{#def.schemaValueQS}} }",
  143. _limit: "{ comparison: {{=$opExpr}}, limit: {{=$schemaValue}}, exclusive: {{=$exclusive}} }",
  144. _exclusiveLimit: "{}",
  145. _limitItems: "{ limit: {{=$schemaValue}} }",
  146. _limitLength: "{ limit: {{=$schemaValue}} }",
  147. _limitProperties:"{ limit: {{=$schemaValue}} }",
  148. multipleOf: "{ multipleOf: {{=$schemaValue}} }",
  149. not: "{}",
  150. oneOf: "{}",
  151. pattern: "{ pattern: {{#def.schemaValueQS}} }",
  152. required: "{ missingProperty: '{{=$missingProperty}}' }",
  153. type: "{ type: '{{? $typeIsArray }}{{= $typeSchema.join(\",\") }}{{??}}{{=$typeSchema}}{{?}}' }",
  154. uniqueItems: "{ i: i, j: j }",
  155. custom: "{ keyword: '{{=$rule.keyword}}' }",
  156. patternGroups: "{ reason: '{{=$reason}}', limit: {{=$limit}}, pattern: '{{=it.util.escapeQuotes($pgProperty)}}' }",
  157. patternRequired: "{ missingPattern: '{{=$missingPattern}}' }",
  158. switch: "{ caseIndex: {{=$caseIndex}} }",
  159. constant: "{}",
  160. _formatLimit: "{ comparison: {{=$opExpr}}, limit: {{#def.schemaValueQS}}, exclusive: {{=$exclusive}} }",
  161. _formatExclusiveLimit: "{}"
  162. } #}}