| 1 | import type { |
| 2 | CodeKeywordDefinition, |
| 3 | KeywordErrorDefinition, |
| 4 | ErrorObject, |
| 5 | AnySchema, |
| 6 | } from "../../types" |
| 7 | import type {KeywordCxt} from "../../compile/validate" |
| 8 | import {_, str} from "../../compile/codegen" |
| 9 | import {alwaysValidSchema} from "../../compile/util" |
| 10 | import {validateArray} from "../code" |
| 11 | import {validateAdditionalItems} from "./additionalItems" |
| 12 | |
| 13 | export type ItemsError = ErrorObject<"items", {limit: number}, AnySchema> |
| 14 | |
| 15 | const error: KeywordErrorDefinition = { |
| 16 | message: ({params: {len}}) => str`must NOT have more than ${len} items`, |
| 17 | params: ({params: {len}}) => _`{limit: ${len}}`, |
| 18 | } |
| 19 | |
| 20 | const def: CodeKeywordDefinition = { |
| 21 | keyword: "items", |
| 22 | type: "array", |
| 23 | schemaType: ["object", "boolean"], |
| 24 | before: "uniqueItems", |
| 25 | error, |
| 26 | code(cxt: KeywordCxt) { |
| 27 | const {schema, parentSchema, it} = cxt |
| 28 | const {prefixItems} = parentSchema |
| 29 | it.items = true |
| 30 | if (alwaysValidSchema(it, schema)) return |
| 31 | if (prefixItems) validateAdditionalItems(cxt, prefixItems) |
| 32 | else cxt.ok(validateArray(cxt)) |
| 33 | }, |
| 34 | } |
| 35 | |
| 36 | export default def |