boolSchema.ts
 1  import type {KeywordErrorDefinition, KeywordErrorCxt} from "../../types"
 2  import type {SchemaCxt} from ".."
 3  import {reportError} from "../errors"
 4  import {_, Name} from "../codegen"
 5  import N from "../names"
 6  
 7  const boolError: KeywordErrorDefinition = {
 8    message: "boolean schema is false",
 9  }
10  
11  export function topBoolOrEmptySchema(it: SchemaCxt): void {
12    const {gen, schema, validateName} = it
13    if (schema === false) {
14      falseSchemaError(it, false)
15    } else if (typeof schema == "object" && schema.$async === true) {
16      gen.return(N.data)
17    } else {
18      gen.assign(_`${validateName}.errors`, null)
19      gen.return(true)
20    }
21  }
22  
23  export function boolOrEmptySchema(it: SchemaCxt, valid: Name): void {
24    const {gen, schema} = it
25    if (schema === false) {
26      gen.var(valid, false) // TODO var
27      falseSchemaError(it)
28    } else {
29      gen.var(valid, true) // TODO var
30    }
31  }
32  
33  function falseSchemaError(it: SchemaCxt, overrideAllErrors?: boolean): void {
34    const {gen, data} = it
35    // TODO maybe some other interface should be used for non-keyword validation errors...
36    const cxt: KeywordErrorCxt = {
37      gen,
38      keyword: "false schema",
39      data,
40      schema: false,
41      schemaCode: false,
42      schemaValue: false,
43      params: {},
44      it,
45    }
46    reportError(cxt, boolError, undefined, overrideAllErrors)
47  }