main
Last change
on this file was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 4 days ago |
F4 Finalna Verzija
|
-
Property mode
set to
100644
|
File size:
857 bytes
|
Rev | Line | |
---|
[79a0317] | 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 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.