1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
import type {
CodeKeywordDefinition,
KeywordErrorDefinition,
ErrorObject,
AnySchema,
} from "../../types"
import {_, not, and, Name, Code} from "../../compile/codegen"
import {alwaysValidSchema, Type} from "../../compile/util"
import N from "../../compile/names"
export type UnevaluatedPropertiesError = ErrorObject<
"unevaluatedProperties",
{unevaluatedProperty: string},
AnySchema
>
const error: KeywordErrorDefinition = {
message: "must NOT have unevaluated properties",
params: ({params}) => _`{unevaluatedProperty: ${params.unevaluatedProperty}}`,
}
const def: CodeKeywordDefinition = {
keyword: "unevaluatedProperties",
type: "object",
schemaType: ["boolean", "object"],
trackErrors: true,
error,
code(cxt) {
const {gen, schema, data, errsCount, it} = cxt
/* istanbul ignore if */
if (!errsCount) throw new Error("ajv implementation error")
const {allErrors, props} = it
if (props instanceof Name) {
gen.if(_`${props} !== true`, () =>
gen.forIn("key", data, (key: Name) =>
gen.if(unevaluatedDynamic(props, key), () => unevaluatedPropCode(key))
)
)
} else if (props !== true) {
gen.forIn("key", data, (key: Name) =>
props === undefined
? unevaluatedPropCode(key)
: gen.if(unevaluatedStatic(props, key), () => unevaluatedPropCode(key))
)
}
it.props = true
cxt.ok(_`${errsCount} === ${N.errors}`)
function unevaluatedPropCode(key: Name): void {
if (schema === false) {
cxt.setParams({unevaluatedProperty: key})
cxt.error()
if (!allErrors) gen.break()
return
}
if (!alwaysValidSchema(it, schema)) {
const valid = gen.name("valid")
cxt.subschema(
{
keyword: "unevaluatedProperties",
dataProp: key,
dataPropType: Type.Str,
},
valid
)
if (!allErrors) gen.if(not(valid), () => gen.break())
}
}
function unevaluatedDynamic(evaluatedProps: Name, key: Name): Code {
return _`!${evaluatedProps} || !${evaluatedProps}[${key}]`
}
function unevaluatedStatic(evaluatedProps: {[K in string]?: true}, key: Name): Code {
const ps: Code[] = []
for (const p in evaluatedProps) {
if (evaluatedProps[p] === true) ps.push(_`${key} !== ${p}`)
}
return and(...ps)
}
},
}
export default def
|