boolSchema.ts 1.3 KB

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