nullable.ts 629 B

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