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"
7 const def: CodeKeywordDefinition = {
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"))
16 const allProps = allSchemaProperties(schema)
17 for (const prop of allProps) {
18 it.definedProperties.add(prop)
20 if (it.opts.unevaluated && allProps.length && it.props !== true) {
21 it.props = mergeEvaluated.props(gen, toHash(allProps), it.props)
23 const properties = allProps.filter((p) => !alwaysValidSchema(it, schema[p]))
24 if (properties.length === 0) return
25 const valid = gen.name("valid")
27 for (const prop of properties) {
28 if (hasDefault(prop)) {
29 applyPropertySchema(prop)
31 gen.if(propertyInData(gen, data, prop, it.opts.ownProperties))
32 applyPropertySchema(prop)
33 if (!it.allErrors) gen.else().var(valid, true)
36 cxt.it.definedProperties.add(prop)
40 function hasDefault(prop: string): boolean | undefined {
41 return it.opts.useDefaults && !it.compositeRule && schema[prop].default !== undefined
44 function applyPropertySchema(prop: string): void {
47 keyword: "properties",