items.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.validateTuple = void 0;
  4. const codegen_1 = require("../../compile/codegen");
  5. const util_1 = require("../../compile/util");
  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 { schema, it } = cxt;
  14. if (Array.isArray(schema))
  15. return validateTuple(cxt, "additionalItems", schema);
  16. it.items = true;
  17. if ((0, util_1.alwaysValidSchema)(it, schema))
  18. return;
  19. cxt.ok((0, code_1.validateArray)(cxt));
  20. },
  21. };
  22. function validateTuple(cxt, extraItems, schArr = cxt.schema) {
  23. const { gen, parentSchema, data, keyword, it } = cxt;
  24. checkStrictTuple(parentSchema);
  25. if (it.opts.unevaluated && schArr.length && it.items !== true) {
  26. it.items = util_1.mergeEvaluated.items(gen, schArr.length, it.items);
  27. }
  28. const valid = gen.name("valid");
  29. const len = gen.const("len", (0, codegen_1._) `${data}.length`);
  30. schArr.forEach((sch, i) => {
  31. if ((0, util_1.alwaysValidSchema)(it, sch))
  32. return;
  33. gen.if((0, codegen_1._) `${len} > ${i}`, () => cxt.subschema({
  34. keyword,
  35. schemaProp: i,
  36. dataProp: i,
  37. }, valid));
  38. cxt.ok(valid);
  39. });
  40. function checkStrictTuple(sch) {
  41. const { opts, errSchemaPath } = it;
  42. const l = schArr.length;
  43. const fullTuple = l === sch.minItems && (l === sch.maxItems || sch[extraItems] === false);
  44. if (opts.strictTuples && !fullTuple) {
  45. const msg = `"${keyword}" is ${l}-tuple, but minItems or maxItems/${extraItems} are not specified or different at path "${errSchemaPath}"`;
  46. (0, util_1.checkStrictMode)(it, msg, opts.strictTuples);
  47. }
  48. }
  49. }
  50. exports.validateTuple = validateTuple;
  51. exports.default = def;
  52. //# sourceMappingURL=items.js.map