items.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const codegen_1 = require("../../compile/codegen");
  4. const util_1 = require("../../compile/util");
  5. const validate_1 = require("../../compile/validate");
  6. const code_1 = require("../code");
  7. const def = {
  8. keyword: "items",
  9. type: "array",
  10. schemaType: ["object", "array", "boolean"],
  11. before: "uniqueItems",
  12. code(cxt) {
  13. const { gen, schema, it } = cxt;
  14. if (Array.isArray(schema)) {
  15. if (it.opts.unevaluated && schema.length && it.items !== true) {
  16. it.items = util_1.mergeEvaluated.items(gen, schema.length, it.items);
  17. }
  18. validateTuple(schema);
  19. }
  20. else {
  21. it.items = true;
  22. if (util_1.alwaysValidSchema(it, schema))
  23. return;
  24. cxt.ok(code_1.validateArray(cxt));
  25. }
  26. function validateTuple(schArr) {
  27. const { parentSchema, data } = cxt;
  28. if (it.opts.strictTuples && !fullTupleSchema(schArr.length, parentSchema)) {
  29. const msg = `"items" is ${schArr.length}-tuple, but minItems or maxItems/additionalItems are not specified or different`;
  30. validate_1.checkStrictMode(it, msg, it.opts.strictTuples);
  31. }
  32. const valid = gen.name("valid");
  33. const len = gen.const("len", codegen_1._ `${data}.length`);
  34. schArr.forEach((sch, i) => {
  35. if (util_1.alwaysValidSchema(it, sch))
  36. return;
  37. gen.if(codegen_1._ `${len} > ${i}`, () => cxt.subschema({
  38. keyword: "items",
  39. schemaProp: i,
  40. dataProp: i,
  41. }, valid));
  42. cxt.ok(valid);
  43. });
  44. }
  45. },
  46. };
  47. function fullTupleSchema(len, sch) {
  48. return len === sch.minItems && (len === sch.maxItems || sch.additionalItems === false);
  49. }
  50. exports.default = def;
  51. //# sourceMappingURL=items.js.map