[79a0317] | 1 | "use strict";
|
---|
| 2 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
| 3 | const codegen_1 = require("../../compile/codegen");
|
---|
| 4 | const types_1 = require("../discriminator/types");
|
---|
| 5 | const compile_1 = require("../../compile");
|
---|
| 6 | const ref_error_1 = require("../../compile/ref_error");
|
---|
| 7 | const util_1 = require("../../compile/util");
|
---|
| 8 | const error = {
|
---|
| 9 | message: ({ params: { discrError, tagName } }) => discrError === types_1.DiscrError.Tag
|
---|
| 10 | ? `tag "${tagName}" must be string`
|
---|
| 11 | : `value of tag "${tagName}" must be in oneOf`,
|
---|
| 12 | params: ({ params: { discrError, tag, tagName } }) => (0, codegen_1._) `{error: ${discrError}, tag: ${tagName}, tagValue: ${tag}}`,
|
---|
| 13 | };
|
---|
| 14 | const def = {
|
---|
| 15 | keyword: "discriminator",
|
---|
| 16 | type: "object",
|
---|
| 17 | schemaType: "object",
|
---|
| 18 | error,
|
---|
| 19 | code(cxt) {
|
---|
| 20 | const { gen, data, schema, parentSchema, it } = cxt;
|
---|
| 21 | const { oneOf } = parentSchema;
|
---|
| 22 | if (!it.opts.discriminator) {
|
---|
| 23 | throw new Error("discriminator: requires discriminator option");
|
---|
| 24 | }
|
---|
| 25 | const tagName = schema.propertyName;
|
---|
| 26 | if (typeof tagName != "string")
|
---|
| 27 | throw new Error("discriminator: requires propertyName");
|
---|
| 28 | if (schema.mapping)
|
---|
| 29 | throw new Error("discriminator: mapping is not supported");
|
---|
| 30 | if (!oneOf)
|
---|
| 31 | throw new Error("discriminator: requires oneOf keyword");
|
---|
| 32 | const valid = gen.let("valid", false);
|
---|
| 33 | const tag = gen.const("tag", (0, codegen_1._) `${data}${(0, codegen_1.getProperty)(tagName)}`);
|
---|
| 34 | gen.if((0, codegen_1._) `typeof ${tag} == "string"`, () => validateMapping(), () => cxt.error(false, { discrError: types_1.DiscrError.Tag, tag, tagName }));
|
---|
| 35 | cxt.ok(valid);
|
---|
| 36 | function validateMapping() {
|
---|
| 37 | const mapping = getMapping();
|
---|
| 38 | gen.if(false);
|
---|
| 39 | for (const tagValue in mapping) {
|
---|
| 40 | gen.elseIf((0, codegen_1._) `${tag} === ${tagValue}`);
|
---|
| 41 | gen.assign(valid, applyTagSchema(mapping[tagValue]));
|
---|
| 42 | }
|
---|
| 43 | gen.else();
|
---|
| 44 | cxt.error(false, { discrError: types_1.DiscrError.Mapping, tag, tagName });
|
---|
| 45 | gen.endIf();
|
---|
| 46 | }
|
---|
| 47 | function applyTagSchema(schemaProp) {
|
---|
| 48 | const _valid = gen.name("valid");
|
---|
| 49 | const schCxt = cxt.subschema({ keyword: "oneOf", schemaProp }, _valid);
|
---|
| 50 | cxt.mergeEvaluated(schCxt, codegen_1.Name);
|
---|
| 51 | return _valid;
|
---|
| 52 | }
|
---|
| 53 | function getMapping() {
|
---|
| 54 | var _a;
|
---|
| 55 | const oneOfMapping = {};
|
---|
| 56 | const topRequired = hasRequired(parentSchema);
|
---|
| 57 | let tagRequired = true;
|
---|
| 58 | for (let i = 0; i < oneOf.length; i++) {
|
---|
| 59 | let sch = oneOf[i];
|
---|
| 60 | if ((sch === null || sch === void 0 ? void 0 : sch.$ref) && !(0, util_1.schemaHasRulesButRef)(sch, it.self.RULES)) {
|
---|
| 61 | const ref = sch.$ref;
|
---|
| 62 | sch = compile_1.resolveRef.call(it.self, it.schemaEnv.root, it.baseId, ref);
|
---|
| 63 | if (sch instanceof compile_1.SchemaEnv)
|
---|
| 64 | sch = sch.schema;
|
---|
| 65 | if (sch === undefined)
|
---|
| 66 | throw new ref_error_1.default(it.opts.uriResolver, it.baseId, ref);
|
---|
| 67 | }
|
---|
| 68 | const propSch = (_a = sch === null || sch === void 0 ? void 0 : sch.properties) === null || _a === void 0 ? void 0 : _a[tagName];
|
---|
| 69 | if (typeof propSch != "object") {
|
---|
| 70 | throw new Error(`discriminator: oneOf subschemas (or referenced schemas) must have "properties/${tagName}"`);
|
---|
| 71 | }
|
---|
| 72 | tagRequired = tagRequired && (topRequired || hasRequired(sch));
|
---|
| 73 | addMappings(propSch, i);
|
---|
| 74 | }
|
---|
| 75 | if (!tagRequired)
|
---|
| 76 | throw new Error(`discriminator: "${tagName}" must be required`);
|
---|
| 77 | return oneOfMapping;
|
---|
| 78 | function hasRequired({ required }) {
|
---|
| 79 | return Array.isArray(required) && required.includes(tagName);
|
---|
| 80 | }
|
---|
| 81 | function addMappings(sch, i) {
|
---|
| 82 | if (sch.const) {
|
---|
| 83 | addMapping(sch.const, i);
|
---|
| 84 | }
|
---|
| 85 | else if (sch.enum) {
|
---|
| 86 | for (const tagValue of sch.enum) {
|
---|
| 87 | addMapping(tagValue, i);
|
---|
| 88 | }
|
---|
| 89 | }
|
---|
| 90 | else {
|
---|
| 91 | throw new Error(`discriminator: "properties/${tagName}" must have "const" or "enum"`);
|
---|
| 92 | }
|
---|
| 93 | }
|
---|
| 94 | function addMapping(tagValue, i) {
|
---|
| 95 | if (typeof tagValue != "string" || tagValue in oneOfMapping) {
|
---|
| 96 | throw new Error(`discriminator: "${tagName}" values must be unique strings`);
|
---|
| 97 | }
|
---|
| 98 | oneOfMapping[tagValue] = i;
|
---|
| 99 | }
|
---|
| 100 | }
|
---|
| 101 | },
|
---|
| 102 | };
|
---|
| 103 | exports.default = def;
|
---|
| 104 | //# sourceMappingURL=index.js.map |
---|