1 | import type {MacroKeywordDefinition} from "ajv"
|
2 | import type {GetDefinition} from "./_types"
|
3 |
|
4 | type RequiredKwd = "anyRequired" | "oneRequired"
|
5 |
|
6 | export default function getRequiredDef(
|
7 | keyword: RequiredKwd
|
8 | ): GetDefinition<MacroKeywordDefinition> {
|
9 | return () => ({
|
10 | keyword,
|
11 | type: "object",
|
12 | schemaType: "array",
|
13 | macro(schema: string[]) {
|
14 | if (schema.length === 0) return true
|
15 | if (schema.length === 1) return {required: schema}
|
16 | const comb = keyword === "anyRequired" ? "anyOf" : "oneOf"
|
17 | return {[comb]: schema.map((p) => ({required: [p]}))}
|
18 | },
|
19 | metaSchema: {
|
20 | type: "array",
|
21 | items: {type: "string"},
|
22 | },
|
23 | })
|
24 | }
|