1 | import type {CodeKeywordDefinition, SchemaObject} from "../../types"
|
2 | import type {KeywordCxt} from "../../compile/validate"
|
3 | import {alwaysValidSchema} from "../../compile/util"
|
4 | import {validateArray} from "../code"
|
5 | import {_, not} from "../../compile/codegen"
|
6 | import {checkMetadata} from "./metadata"
|
7 | import {checkNullable} from "./nullable"
|
8 | import {typeError, _JTDTypeError} from "./error"
|
9 |
|
10 | export type JTDElementsError = _JTDTypeError<"elements", "array", SchemaObject>
|
11 |
|
12 | const 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 |
|
32 | export default def
|