anyOf.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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 def = {
  6. keyword: "anyOf",
  7. schemaType: "array",
  8. trackErrors: true,
  9. code(cxt) {
  10. const { gen, schema, it } = cxt;
  11. /* istanbul ignore if */
  12. if (!Array.isArray(schema))
  13. throw new Error("ajv implementation error");
  14. const alwaysValid = schema.some((sch) => util_1.alwaysValidSchema(it, sch));
  15. if (alwaysValid && !it.opts.unevaluated)
  16. return;
  17. const valid = gen.let("valid", false);
  18. const schValid = gen.name("_valid");
  19. gen.block(() => schema.forEach((_sch, i) => {
  20. const schCxt = cxt.subschema({
  21. keyword: "anyOf",
  22. schemaProp: i,
  23. compositeRule: true,
  24. }, schValid);
  25. gen.assign(valid, codegen_1._ `${valid} || ${schValid}`);
  26. const merged = cxt.mergeValidEvaluated(schCxt, schValid);
  27. // can short-circuit if `unevaluatedProperties/Items` not supported (opts.unevaluated !== true)
  28. // or if all properties and items were evaluated (it.props === true && it.items === true)
  29. if (!merged)
  30. gen.if(codegen_1.not(valid));
  31. }));
  32. cxt.result(valid, () => cxt.reset(), () => cxt.error(true));
  33. },
  34. error: {
  35. message: "should match some schema in anyOf",
  36. },
  37. };
  38. exports.default = def;
  39. //# sourceMappingURL=anyOf.js.map