additionalItems.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import type {
  2. CodeKeywordDefinition,
  3. ErrorObject,
  4. KeywordErrorDefinition,
  5. AnySchema,
  6. } from "../../types"
  7. import type KeywordCxt from "../../compile/context"
  8. import {_, str, not, Name} from "../../compile/codegen"
  9. import {Type} from "../../compile/subschema"
  10. import {alwaysValidSchema} from "../../compile/util"
  11. import {checkStrictMode} from "../../compile/validate"
  12. export type AdditionalItemsError = ErrorObject<"additionalItems", {limit: number}, AnySchema>
  13. const error: KeywordErrorDefinition = {
  14. message: ({params: {len}}) => str`should NOT have more than ${len} items`,
  15. params: ({params: {len}}) => _`{limit: ${len}}`,
  16. }
  17. const def: CodeKeywordDefinition = {
  18. keyword: "additionalItems" as const,
  19. type: "array",
  20. schemaType: ["boolean", "object"],
  21. before: "uniqueItems",
  22. error,
  23. code(cxt: KeywordCxt) {
  24. const {gen, schema, parentSchema, data, it} = cxt
  25. const {items} = parentSchema
  26. if (!Array.isArray(items)) {
  27. checkStrictMode(it, '"additionalItems" is ignored when "items" is not an array of schemas')
  28. return
  29. }
  30. it.items = true
  31. const len = gen.const("len", _`${data}.length`)
  32. if (schema === false) {
  33. cxt.setParams({len: items.length})
  34. cxt.pass(_`${len} <= ${items.length}`)
  35. } else if (typeof schema == "object" && !alwaysValidSchema(it, schema)) {
  36. const valid = gen.var("valid", _`${len} <= ${items.length}`) // TODO var
  37. gen.if(not(valid), () => validateItems(valid))
  38. cxt.ok(valid)
  39. }
  40. function validateItems(valid: Name): void {
  41. gen.forRange("i", items.length, len, (i) => {
  42. cxt.subschema({keyword: "additionalItems", dataProp: i, dataPropType: Type.Num}, valid)
  43. if (!it.allErrors) gen.if(not(valid), () => gen.break())
  44. })
  45. }
  46. },
  47. }
  48. export default def