items.js 2.5 KB

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