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