iterate.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.schemaKeywords = void 0;
  4. const applicability_1 = require("./applicability");
  5. const dataType_1 = require("./dataType");
  6. const defaults_1 = require("./defaults");
  7. const keyword_1 = require("./keyword");
  8. const util_1 = require("../util");
  9. const _1 = require(".");
  10. const codegen_1 = require("../codegen");
  11. const names_1 = require("../names");
  12. function schemaKeywords(it, types, typeErrors, errsCount) {
  13. const { gen, schema, data, allErrors, opts, self } = it;
  14. const { RULES } = self;
  15. if (schema.$ref && (opts.ignoreKeywordsWithRef || !util_1.schemaHasRulesButRef(schema, RULES))) {
  16. gen.block(() => keyword_1.keywordCode(it, "$ref", RULES.all.$ref.definition)); // TODO typecast
  17. return;
  18. }
  19. if (!opts.jtd)
  20. checkStrictTypes(it, types);
  21. gen.block(() => {
  22. for (const group of RULES.rules)
  23. groupKeywords(group);
  24. groupKeywords(RULES.post);
  25. });
  26. function groupKeywords(group) {
  27. if (!applicability_1.shouldUseGroup(schema, group))
  28. return;
  29. if (group.type) {
  30. gen.if(dataType_1.checkDataType(group.type, data, opts.strict));
  31. iterateKeywords(it, group);
  32. if (types.length === 1 && types[0] === group.type && typeErrors) {
  33. gen.else();
  34. dataType_1.reportTypeError(it);
  35. }
  36. gen.endIf();
  37. }
  38. else {
  39. iterateKeywords(it, group);
  40. }
  41. // TODO make it "ok" call?
  42. if (!allErrors)
  43. gen.if(codegen_1._ `${names_1.default.errors} === ${errsCount || 0}`);
  44. }
  45. }
  46. exports.schemaKeywords = schemaKeywords;
  47. function iterateKeywords(it, group) {
  48. const { gen, schema, opts: { useDefaults }, } = it;
  49. if (useDefaults)
  50. defaults_1.assignDefaults(it, group.type);
  51. gen.block(() => {
  52. for (const rule of group.rules) {
  53. if (applicability_1.shouldUseRule(schema, rule)) {
  54. keyword_1.keywordCode(it, rule.keyword, rule.definition, group.type);
  55. }
  56. }
  57. });
  58. }
  59. function checkStrictTypes(it, types) {
  60. if (it.schemaEnv.meta || !it.opts.strictTypes)
  61. return;
  62. checkContextTypes(it, types);
  63. if (!it.opts.allowUnionTypes)
  64. checkMultipleTypes(it, types);
  65. checkKeywordTypes(it, it.dataTypes);
  66. }
  67. function checkContextTypes(it, types) {
  68. if (!types.length)
  69. return;
  70. if (!it.dataTypes.length) {
  71. it.dataTypes = types;
  72. return;
  73. }
  74. types.forEach((t) => {
  75. if (!includesType(it.dataTypes, t)) {
  76. strictTypesError(it, `type "${t}" not allowed by context "${it.dataTypes.join(",")}"`);
  77. }
  78. });
  79. it.dataTypes = it.dataTypes.filter((t) => includesType(types, t));
  80. }
  81. function checkMultipleTypes(it, ts) {
  82. if (ts.length > 1 && !(ts.length === 2 && ts.includes("null"))) {
  83. strictTypesError(it, "use allowUnionTypes to allow union type keyword");
  84. }
  85. }
  86. function checkKeywordTypes(it, ts) {
  87. const rules = it.self.RULES.all;
  88. for (const keyword in rules) {
  89. const rule = rules[keyword];
  90. if (typeof rule == "object" && applicability_1.shouldUseRule(it.schema, rule)) {
  91. const { type } = rule.definition;
  92. if (type.length && !type.some((t) => hasApplicableType(ts, t))) {
  93. strictTypesError(it, `missing type "${type.join(",")}" for keyword "${keyword}"`);
  94. }
  95. }
  96. }
  97. }
  98. function hasApplicableType(schTs, kwdT) {
  99. return schTs.includes(kwdT) || (kwdT === "number" && schTs.includes("integer"));
  100. }
  101. function includesType(ts, t) {
  102. return ts.includes(t) || (t === "integer" && ts.includes("number"));
  103. }
  104. function strictTypesError(it, msg) {
  105. const schemaPath = it.schemaEnv.baseId + it.errSchemaPath;
  106. msg += ` at "${schemaPath}" (strictTypes)`;
  107. _1.checkStrictMode(it, msg, it.opts.strictTypes);
  108. }
  109. //# sourceMappingURL=iterate.js.map