additionalItems.ts 1.8 KB

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