limitItems.ts 812 B

1234567891011121314151617181920212223242526
  1. import type {CodeKeywordDefinition, KeywordErrorDefinition} from "../../types"
  2. import type {KeywordCxt} from "../../compile/validate"
  3. import {_, str, operators} from "../../compile/codegen"
  4. const error: KeywordErrorDefinition = {
  5. message({keyword, schemaCode}) {
  6. const comp = keyword === "maxItems" ? "more" : "fewer"
  7. return str`must NOT have ${comp} than ${schemaCode} items`
  8. },
  9. params: ({schemaCode}) => _`{limit: ${schemaCode}}`,
  10. }
  11. const def: CodeKeywordDefinition = {
  12. keyword: ["maxItems", "minItems"],
  13. type: "array",
  14. schemaType: "number",
  15. $data: true,
  16. error,
  17. code(cxt: KeywordCxt) {
  18. const {keyword, data, schemaCode} = cxt
  19. const op = keyword === "maxItems" ? operators.GT : operators.LT
  20. cxt.fail$data(_`${data}.length ${op} ${schemaCode}`)
  21. },
  22. }
  23. export default def