UNPKG

976 BPlain TextView Raw
1import type {CodeKeywordDefinition, SchemaObject} from "../../types"
2import type {KeywordCxt} from "../../compile/validate"
3import {alwaysValidSchema} from "../../compile/util"
4import {validateArray} from "../code"
5import {_, not} from "../../compile/codegen"
6import {checkMetadata} from "./metadata"
7import {checkNullable} from "./nullable"
8import {typeError, _JTDTypeError} from "./error"
9
10export type JTDElementsError = _JTDTypeError<"elements", "array", SchemaObject>
11
12const def: CodeKeywordDefinition = {
13 keyword: "elements",
14 schemaType: "object",
15 error: typeError("array"),
16 code(cxt: KeywordCxt) {
17 checkMetadata(cxt)
18 const {gen, data, schema, it} = cxt
19 if (alwaysValidSchema(it, schema)) return
20 const [valid] = checkNullable(cxt)
21 gen.if(not(valid), () =>
22 gen.if(
23 _`Array.isArray(${data})`,
24 () => gen.assign(valid, validateArray(cxt)),
25 () => cxt.error()
26 )
27 )
28 cxt.ok(valid)
29 },
30}
31
32export default def