1 import type {CodeKeywordDefinition, SchemaObject} from "../../types"
2 import type {KeywordCxt} from "../../compile/validate"
3 import {alwaysValidSchema, Type} from "../../compile/util"
4 import {not, Name} from "../../compile/codegen"
5 import {checkMetadata} from "./metadata"
6 import {checkNullableObject} from "./nullable"
7 import {typeError, _JTDTypeError} from "./error"
9 export type JTDValuesError = _JTDTypeError<"values", "object", SchemaObject>
11 const def: CodeKeywordDefinition = {
14 error: typeError("object"),
15 code(cxt: KeywordCxt) {
17 const {gen, data, schema, it} = cxt
18 if (alwaysValidSchema(it, schema)) return
19 const [valid, cond] = checkNullableObject(cxt, data)
21 gen.assign(valid, validateMap())
22 gen.elseIf(not(valid))
27 function validateMap(): Name | boolean {
28 const _valid = gen.name("valid")
30 const validMap = gen.let("valid", true)
31 validateValues(() => gen.assign(validMap, false))
35 validateValues(() => gen.break())
38 function validateValues(notValid: () => void): void {
39 gen.forIn("key", data, (key) => {
44 dataPropType: Type.Str,
48 gen.if(not(_valid), notValid)