source: trip-planner-front/node_modules/ajv/lib/compile/validate/defaults.ts@ bdd6491

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

initial commit

  • Property mode set to 100644
File size: 1.2 KB
Line 
1import type {SchemaObjCxt} from ".."
2import {_, getProperty, stringify} from "../codegen"
3import {checkStrictMode} from "../util"
4
5export function assignDefaults(it: SchemaObjCxt, ty?: string): void {
6 const {properties, items} = it.schema
7 if (ty === "object" && properties) {
8 for (const key in properties) {
9 assignDefault(it, key, properties[key].default)
10 }
11 } else if (ty === "array" && Array.isArray(items)) {
12 items.forEach((sch, i: number) => assignDefault(it, i, sch.default))
13 }
14}
15
16function assignDefault(it: SchemaObjCxt, prop: string | number, defaultValue: unknown): void {
17 const {gen, compositeRule, data, opts} = it
18 if (defaultValue === undefined) return
19 const childData = _`${data}${getProperty(prop)}`
20 if (compositeRule) {
21 checkStrictMode(it, `default is ignored for: ${childData}`)
22 return
23 }
24
25 let condition = _`${childData} === undefined`
26 if (opts.useDefaults === "empty") {
27 condition = _`${condition} || ${childData} === null || ${childData} === ""`
28 }
29 // `${childData} === undefined` +
30 // (opts.useDefaults === "empty" ? ` || ${childData} === null || ${childData} === ""` : "")
31 gen.if(condition, _`${childData} = ${stringify(defaultValue)}`)
32}
Note: See TracBrowser for help on using the repository browser.