not.ts 863 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import type {CodeKeywordDefinition, ErrorNoParams, AnySchema} from "../../types"
  2. import type KeywordCxt from "../../compile/context"
  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.result(
  26. valid,
  27. () => cxt.error(),
  28. () => cxt.reset()
  29. )
  30. },
  31. error: {
  32. message: "should NOT be valid",
  33. },
  34. }
  35. export default def