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