oneOf.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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 error = {
  6. message: "should match exactly one schema in oneOf",
  7. params: ({ params }) => codegen_1._ `{passingSchemas: ${params.passing}}`,
  8. };
  9. const def = {
  10. keyword: "oneOf",
  11. schemaType: "array",
  12. trackErrors: true,
  13. error,
  14. code(cxt) {
  15. const { gen, schema, it } = cxt;
  16. /* istanbul ignore if */
  17. if (!Array.isArray(schema))
  18. throw new Error("ajv implementation error");
  19. const schArr = schema;
  20. const valid = gen.let("valid", false);
  21. const passing = gen.let("passing", null);
  22. const schValid = gen.name("_valid");
  23. cxt.setParams({ passing });
  24. // TODO possibly fail straight away (with warning or exception) if there are two empty always valid schemas
  25. gen.block(validateOneOf);
  26. cxt.result(valid, () => cxt.reset(), () => cxt.error(true));
  27. function validateOneOf() {
  28. schArr.forEach((sch, i) => {
  29. let schCxt;
  30. if (util_1.alwaysValidSchema(it, sch)) {
  31. gen.var(schValid, true);
  32. }
  33. else {
  34. schCxt = cxt.subschema({
  35. keyword: "oneOf",
  36. schemaProp: i,
  37. compositeRule: true,
  38. }, schValid);
  39. }
  40. if (i > 0) {
  41. gen
  42. .if(codegen_1._ `${schValid} && ${valid}`)
  43. .assign(valid, false)
  44. .assign(passing, codegen_1._ `[${passing}, ${i}]`)
  45. .else();
  46. }
  47. gen.if(schValid, () => {
  48. gen.assign(valid, true);
  49. gen.assign(passing, i);
  50. if (schCxt)
  51. cxt.mergeEvaluated(schCxt, codegen_1.Name);
  52. });
  53. });
  54. }
  55. },
  56. };
  57. exports.default = def;
  58. //# sourceMappingURL=oneOf.js.map