1 | import type {CodeKeywordDefinition} from "../../types"
|
2 | import {KeywordCxt} from "../../compile/validate"
|
3 | import {propertyInData, allSchemaProperties} from "../code"
|
4 | import {alwaysValidSchema, toHash, mergeEvaluated} from "../../compile/util"
|
5 | import apDef from "./additionalProperties"
|
6 |
|
7 | const def: CodeKeywordDefinition = {
|
8 | keyword: "properties",
|
9 | type: "object",
|
10 | schemaType: "object",
|
11 | code(cxt: KeywordCxt) {
|
12 | const {gen, schema, parentSchema, data, it} = cxt
|
13 | if (it.opts.removeAdditional === "all" && parentSchema.additionalProperties === undefined) {
|
14 | apDef.code(new KeywordCxt(it, apDef, "additionalProperties"))
|
15 | }
|
16 | const allProps = allSchemaProperties(schema)
|
17 | for (const prop of allProps) {
|
18 | it.definedProperties.add(prop)
|
19 | }
|
20 | if (it.opts.unevaluated && allProps.length && it.props !== true) {
|
21 | it.props = mergeEvaluated.props(gen, toHash(allProps), it.props)
|
22 | }
|
23 | const properties = allProps.filter((p) => !alwaysValidSchema(it, schema[p]))
|
24 | if (properties.length === 0) return
|
25 | const valid = gen.name("valid")
|
26 |
|
27 | for (const prop of properties) {
|
28 | if (hasDefault(prop)) {
|
29 | applyPropertySchema(prop)
|
30 | } else {
|
31 | gen.if(propertyInData(gen, data, prop, it.opts.ownProperties))
|
32 | applyPropertySchema(prop)
|
33 | if (!it.allErrors) gen.else().var(valid, true)
|
34 | gen.endIf()
|
35 | }
|
36 | cxt.it.definedProperties.add(prop)
|
37 | cxt.ok(valid)
|
38 | }
|
39 |
|
40 | function hasDefault(prop: string): boolean | undefined {
|
41 | return it.opts.useDefaults && !it.compositeRule && schema[prop].default !== undefined
|
42 | }
|
43 |
|
44 | function applyPropertySchema(prop: string): void {
|
45 | cxt.subschema(
|
46 | {
|
47 | keyword: "properties",
|
48 | schemaProp: prop,
|
49 | dataProp: prop,
|
50 | },
|
51 | valid
|
52 | )
|
53 | }
|
54 | },
|
55 | }
|
56 |
|
57 | export default def
|