1 | "use strict";
|
---|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
3 | exports.assignDefaults = void 0;
|
---|
4 | const codegen_1 = require("../codegen");
|
---|
5 | const util_1 = require("../util");
|
---|
6 | function assignDefaults(it, ty) {
|
---|
7 | const { properties, items } = it.schema;
|
---|
8 | if (ty === "object" && properties) {
|
---|
9 | for (const key in properties) {
|
---|
10 | assignDefault(it, key, properties[key].default);
|
---|
11 | }
|
---|
12 | }
|
---|
13 | else if (ty === "array" && Array.isArray(items)) {
|
---|
14 | items.forEach((sch, i) => assignDefault(it, i, sch.default));
|
---|
15 | }
|
---|
16 | }
|
---|
17 | exports.assignDefaults = assignDefaults;
|
---|
18 | function assignDefault(it, prop, defaultValue) {
|
---|
19 | const { gen, compositeRule, data, opts } = it;
|
---|
20 | if (defaultValue === undefined)
|
---|
21 | return;
|
---|
22 | const childData = codegen_1._ `${data}${codegen_1.getProperty(prop)}`;
|
---|
23 | if (compositeRule) {
|
---|
24 | util_1.checkStrictMode(it, `default is ignored for: ${childData}`);
|
---|
25 | return;
|
---|
26 | }
|
---|
27 | let condition = codegen_1._ `${childData} === undefined`;
|
---|
28 | if (opts.useDefaults === "empty") {
|
---|
29 | condition = codegen_1._ `${condition} || ${childData} === null || ${childData} === ""`;
|
---|
30 | }
|
---|
31 | // `${childData} === undefined` +
|
---|
32 | // (opts.useDefaults === "empty" ? ` || ${childData} === null || ${childData} === ""` : "")
|
---|
33 | gen.if(condition, codegen_1._ `${childData} = ${codegen_1.stringify(defaultValue)}`);
|
---|
34 | }
|
---|
35 | //# sourceMappingURL=defaults.js.map |
---|