UNPKG

3.86 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.extendSubschemaMode = exports.extendSubschemaData = exports.getSubschema = void 0;
4const codegen_1 = require("../codegen");
5const util_1 = require("../util");
6function getSubschema(it, { keyword, schemaProp, schema, schemaPath, errSchemaPath, topSchemaRef }) {
7 if (keyword !== undefined && schema !== undefined) {
8 throw new Error('both "keyword" and "schema" passed, only one allowed');
9 }
10 if (keyword !== undefined) {
11 const sch = it.schema[keyword];
12 return schemaProp === undefined
13 ? {
14 schema: sch,
15 schemaPath: (0, codegen_1._) `${it.schemaPath}${(0, codegen_1.getProperty)(keyword)}`,
16 errSchemaPath: `${it.errSchemaPath}/${keyword}`,
17 }
18 : {
19 schema: sch[schemaProp],
20 schemaPath: (0, codegen_1._) `${it.schemaPath}${(0, codegen_1.getProperty)(keyword)}${(0, codegen_1.getProperty)(schemaProp)}`,
21 errSchemaPath: `${it.errSchemaPath}/${keyword}/${(0, util_1.escapeFragment)(schemaProp)}`,
22 };
23 }
24 if (schema !== undefined) {
25 if (schemaPath === undefined || errSchemaPath === undefined || topSchemaRef === undefined) {
26 throw new Error('"schemaPath", "errSchemaPath" and "topSchemaRef" are required with "schema"');
27 }
28 return {
29 schema,
30 schemaPath,
31 topSchemaRef,
32 errSchemaPath,
33 };
34 }
35 throw new Error('either "keyword" or "schema" must be passed');
36}
37exports.getSubschema = getSubschema;
38function extendSubschemaData(subschema, it, { dataProp, dataPropType: dpType, data, dataTypes, propertyName }) {
39 if (data !== undefined && dataProp !== undefined) {
40 throw new Error('both "data" and "dataProp" passed, only one allowed');
41 }
42 const { gen } = it;
43 if (dataProp !== undefined) {
44 const { errorPath, dataPathArr, opts } = it;
45 const nextData = gen.let("data", (0, codegen_1._) `${it.data}${(0, codegen_1.getProperty)(dataProp)}`, true);
46 dataContextProps(nextData);
47 subschema.errorPath = (0, codegen_1.str) `${errorPath}${(0, util_1.getErrorPath)(dataProp, dpType, opts.jsPropertySyntax)}`;
48 subschema.parentDataProperty = (0, codegen_1._) `${dataProp}`;
49 subschema.dataPathArr = [...dataPathArr, subschema.parentDataProperty];
50 }
51 if (data !== undefined) {
52 const nextData = data instanceof codegen_1.Name ? data : gen.let("data", data, true); // replaceable if used once?
53 dataContextProps(nextData);
54 if (propertyName !== undefined)
55 subschema.propertyName = propertyName;
56 // TODO something is possibly wrong here with not changing parentDataProperty and not appending dataPathArr
57 }
58 if (dataTypes)
59 subschema.dataTypes = dataTypes;
60 function dataContextProps(_nextData) {
61 subschema.data = _nextData;
62 subschema.dataLevel = it.dataLevel + 1;
63 subschema.dataTypes = [];
64 it.definedProperties = new Set();
65 subschema.parentData = it.data;
66 subschema.dataNames = [...it.dataNames, _nextData];
67 }
68}
69exports.extendSubschemaData = extendSubschemaData;
70function extendSubschemaMode(subschema, { jtdDiscriminator, jtdMetadata, compositeRule, createErrors, allErrors }) {
71 if (compositeRule !== undefined)
72 subschema.compositeRule = compositeRule;
73 if (createErrors !== undefined)
74 subschema.createErrors = createErrors;
75 if (allErrors !== undefined)
76 subschema.allErrors = allErrors;
77 subschema.jtdDiscriminator = jtdDiscriminator; // not inherited
78 subschema.jtdMetadata = jtdMetadata; // not inherited
79}
80exports.extendSubschemaMode = extendSubschemaMode;
81//# sourceMappingURL=subschema.js.map
\No newline at end of file