not.ts 859 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. import type {CodeKeywordDefinition, ErrorNoParams, AnySchema} from "../../types"
  2. import type {KeywordCxt} from "../../compile/validate"
  3. import {alwaysValidSchema} from "../../compile/util"
  4. export type NotKeywordError = ErrorNoParams<"not", AnySchema>
  5. const def: CodeKeywordDefinition = {
  6. keyword: "not",
  7. schemaType: ["object", "boolean"],
  8. trackErrors: true,
  9. code(cxt: KeywordCxt) {
  10. const {gen, schema, it} = cxt
  11. if (alwaysValidSchema(it, schema)) {
  12. cxt.fail()
  13. return
  14. }
  15. const valid = gen.name("valid")
  16. cxt.subschema(
  17. {
  18. keyword: "not",
  19. compositeRule: true,
  20. createErrors: false,
  21. allErrors: false,
  22. },
  23. valid
  24. )
  25. cxt.failResult(
  26. valid,
  27. () => cxt.reset(),
  28. () => cxt.error()
  29. )
  30. },
  31. error: {message: "must NOT be valid"},
  32. }
  33. export default def