1 | import type {MacroKeywordDefinition} from "ajv"
|
2 | import type {GetDefinition} from "./_types"
|
3 |
|
4 | type RangeKwd = "range" | "exclusiveRange"
|
5 |
|
6 | export default function getRangeDef(keyword: RangeKwd): GetDefinition<MacroKeywordDefinition> {
|
7 | return () => ({
|
8 | keyword,
|
9 | type: "number",
|
10 | schemaType: "array",
|
11 | macro: function ([min, max]: [number, number]) {
|
12 | validateRangeSchema(min, max)
|
13 | return keyword === "range"
|
14 | ? {minimum: min, maximum: max}
|
15 | : {exclusiveMinimum: min, exclusiveMaximum: max}
|
16 | },
|
17 | metaSchema: {
|
18 | type: "array",
|
19 | minItems: 2,
|
20 | maxItems: 2,
|
21 | items: {type: "number"},
|
22 | },
|
23 | })
|
24 |
|
25 | function validateRangeSchema(min: number, max: number): void {
|
26 | if (min > max || (keyword === "exclusiveRange" && min === max)) {
|
27 | throw new Error("There are no numbers in range")
|
28 | }
|
29 | }
|
30 | }
|