1 | "use strict";
|
---|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
3 | const codegen_1 = require("../../compile/codegen");
|
---|
4 | const metadata_1 = require("./metadata");
|
---|
5 | const nullable_1 = require("./nullable");
|
---|
6 | const error_1 = require("./error");
|
---|
7 | const types_1 = require("../discriminator/types");
|
---|
8 | const error = {
|
---|
9 | message: (cxt) => {
|
---|
10 | const { schema, params } = cxt;
|
---|
11 | return params.discrError
|
---|
12 | ? params.discrError === types_1.DiscrError.Tag
|
---|
13 | ? `tag "${schema}" must be string`
|
---|
14 | : `value of tag "${schema}" must be in mapping`
|
---|
15 | : error_1.typeErrorMessage(cxt, "object");
|
---|
16 | },
|
---|
17 | params: (cxt) => {
|
---|
18 | const { schema, params } = cxt;
|
---|
19 | return params.discrError
|
---|
20 | ? codegen_1._ `{error: ${params.discrError}, tag: ${schema}, tagValue: ${params.tag}}`
|
---|
21 | : error_1.typeErrorParams(cxt, "object");
|
---|
22 | },
|
---|
23 | };
|
---|
24 | const def = {
|
---|
25 | keyword: "discriminator",
|
---|
26 | schemaType: "string",
|
---|
27 | implements: ["mapping"],
|
---|
28 | error,
|
---|
29 | code(cxt) {
|
---|
30 | metadata_1.checkMetadata(cxt);
|
---|
31 | const { gen, data, schema, parentSchema } = cxt;
|
---|
32 | const [valid, cond] = nullable_1.checkNullableObject(cxt, data);
|
---|
33 | gen.if(cond);
|
---|
34 | validateDiscriminator();
|
---|
35 | gen.elseIf(codegen_1.not(valid));
|
---|
36 | cxt.error();
|
---|
37 | gen.endIf();
|
---|
38 | cxt.ok(valid);
|
---|
39 | function validateDiscriminator() {
|
---|
40 | const tag = gen.const("tag", codegen_1._ `${data}${codegen_1.getProperty(schema)}`);
|
---|
41 | gen.if(codegen_1._ `${tag} === undefined`);
|
---|
42 | cxt.error(false, { discrError: types_1.DiscrError.Tag, tag });
|
---|
43 | gen.elseIf(codegen_1._ `typeof ${tag} == "string"`);
|
---|
44 | validateMapping(tag);
|
---|
45 | gen.else();
|
---|
46 | cxt.error(false, { discrError: types_1.DiscrError.Tag, tag }, { instancePath: schema });
|
---|
47 | gen.endIf();
|
---|
48 | }
|
---|
49 | function validateMapping(tag) {
|
---|
50 | gen.if(false);
|
---|
51 | for (const tagValue in parentSchema.mapping) {
|
---|
52 | gen.elseIf(codegen_1._ `${tag} === ${tagValue}`);
|
---|
53 | gen.assign(valid, applyTagSchema(tagValue));
|
---|
54 | }
|
---|
55 | gen.else();
|
---|
56 | cxt.error(false, { discrError: types_1.DiscrError.Mapping, tag }, { instancePath: schema, schemaPath: "mapping", parentSchema: true });
|
---|
57 | gen.endIf();
|
---|
58 | }
|
---|
59 | function applyTagSchema(schemaProp) {
|
---|
60 | const _valid = gen.name("valid");
|
---|
61 | cxt.subschema({
|
---|
62 | keyword: "mapping",
|
---|
63 | schemaProp,
|
---|
64 | jtdDiscriminator: schema,
|
---|
65 | }, _valid);
|
---|
66 | return _valid;
|
---|
67 | }
|
---|
68 | },
|
---|
69 | };
|
---|
70 | exports.default = def;
|
---|
71 | //# sourceMappingURL=discriminator.js.map |
---|