source: trip-planner-front/node_modules/ajv/dist/vocabularies/jtd/discriminator.js

Last change on this file was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 2.7 KB
Line 
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const codegen_1 = require("../../compile/codegen");
4const metadata_1 = require("./metadata");
5const nullable_1 = require("./nullable");
6const error_1 = require("./error");
7const types_1 = require("../discriminator/types");
8const 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};
24const 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};
70exports.default = def;
71//# sourceMappingURL=discriminator.js.map
Note: See TracBrowser for help on using the repository browser.