elements.ts 741 B

1234567891011121314151617181920212223
  1. import type {CodeKeywordDefinition} from "../../types"
  2. import type KeywordCxt from "../../compile/context"
  3. import {alwaysValidSchema} from "../../compile/util"
  4. import {validateArray} from "../code"
  5. import {_, and, nil} from "../../compile/codegen"
  6. import {checkMetadata} from "./metadata"
  7. import {checkNullable} from "./nullable"
  8. const def: CodeKeywordDefinition = {
  9. keyword: "elements",
  10. schemaType: "object",
  11. code(cxt: KeywordCxt) {
  12. checkMetadata(cxt)
  13. const {gen, data, schema, it} = cxt
  14. if (alwaysValidSchema(it, schema)) return
  15. const [valid, cond] = checkNullable(cxt, nil)
  16. gen.if(and(cond, _`Array.isArray(${data})`), () => gen.assign(valid, validateArray(cxt)))
  17. cxt.pass(valid)
  18. },
  19. }
  20. export default def