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