1 import type {CodeKeywordDefinition, AnySchema, AnySchemaObject} from "../../types"
2 import type {KeywordCxt} from "../../compile/validate"
3 import {_} from "../../compile/codegen"
4 import {alwaysValidSchema, mergeEvaluated, checkStrictMode} from "../../compile/util"
5 import {validateArray} from "../code"
7 const def: CodeKeywordDefinition = {
10 schemaType: ["object", "array", "boolean"],
11 before: "uniqueItems",
12 code(cxt: KeywordCxt) {
13 const {schema, it} = cxt
14 if (Array.isArray(schema)) return validateTuple(cxt, "additionalItems", schema)
16 if (alwaysValidSchema(it, schema)) return
17 cxt.ok(validateArray(cxt))
21 export function validateTuple(
24 schArr: AnySchema[] = cxt.schema
26 const {gen, parentSchema, data, keyword, it} = cxt
27 checkStrictTuple(parentSchema)
28 if (it.opts.unevaluated && schArr.length && it.items !== true) {
29 it.items = mergeEvaluated.items(gen, schArr.length, it.items)
31 const valid = gen.name("valid")
32 const len = gen.const("len", _`${data}.length`)
33 schArr.forEach((sch: AnySchema, i: number) => {
34 if (alwaysValidSchema(it, sch)) return
35 gen.if(_`${len} > ${i}`, () =>
48 function checkStrictTuple(sch: AnySchemaObject): void {
49 const {opts, errSchemaPath} = it
50 const l = schArr.length
51 const fullTuple = l === sch.minItems && (l === sch.maxItems || sch[extraItems] === false)
52 if (opts.strictTuples && !fullTuple) {
53 const msg = `"${keyword}" is ${l}-tuple, but minItems or maxItems/${extraItems} are not specified or different at path "${errSchemaPath}"`
54 checkStrictMode(it, msg, opts.strictTuples)