UNPKG

1.05 kBPlain TextView Raw
1import type {
2 CodeKeywordDefinition,
3 KeywordErrorDefinition,
4 ErrorObject,
5 AnySchema,
6} from "../../types"
7import type {KeywordCxt} from "../../compile/validate"
8import {_, str} from "../../compile/codegen"
9import {alwaysValidSchema} from "../../compile/util"
10import {validateArray} from "../code"
11import {validateAdditionalItems} from "./additionalItems"
12
13export type ItemsError = ErrorObject<"items", {limit: number}, AnySchema>
14
15const error: KeywordErrorDefinition = {
16 message: ({params: {len}}) => str`must NOT have more than ${len} items`,
17 params: ({params: {len}}) => _`{limit: ${len}}`,
18}
19
20const 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
36export default def