UNPKG

649 BPlain TextView Raw
1import type {KeywordCxt} from "../../compile/validate"
2import {_, not, nil, Code, Name} from "../../compile/codegen"
3
4export function checkNullable(
5 {gen, data, parentSchema}: KeywordCxt,
6 cond: Code = nil
7): [Name, Code] {
8 const valid = gen.name("valid")
9 if (parentSchema.nullable) {
10 gen.let(valid, _`${data} === null`)
11 cond = not(valid)
12 } else {
13 gen.let(valid, false)
14 }
15 return [valid, cond]
16}
17
18export function checkNullableObject(cxt: KeywordCxt, cond: Code): [Name, Code] {
19 const [valid, cond_] = checkNullable(cxt, cond)
20 return [valid, _`${cond_} && typeof ${cxt.data} == "object" && !Array.isArray(${cxt.data})`]
21}