iterate.js 3.9 KB

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