UNPKG

1.45 kBPlain TextView Raw
1import type {AddedKeywordDefinition} from "../types"
2
3const _jsonTypes = ["string", "number", "integer", "boolean", "null", "object", "array"] as const
4
5export type JSONType = typeof _jsonTypes[number]
6
7const jsonTypes: Set<string> = new Set(_jsonTypes)
8
9export function isJSONType(x: unknown): x is JSONType {
10 return typeof x == "string" && jsonTypes.has(x)
11}
12
13type ValidationTypes = {
14 [K in JSONType]: boolean | RuleGroup | undefined
15}
16
17export interface ValidationRules {
18 rules: RuleGroup[]
19 post: RuleGroup
20 all: {[Key in string]?: boolean | Rule} // rules that have to be validated
21 keywords: {[Key in string]?: boolean} // all known keywords (superset of "all")
22 types: ValidationTypes
23}
24
25export interface RuleGroup {
26 type?: JSONType
27 rules: Rule[]
28}
29
30// This interface wraps KeywordDefinition because definition can have multiple keywords
31export interface Rule {
32 keyword: string
33 definition: AddedKeywordDefinition
34}
35
36export function getRules(): ValidationRules {
37 const groups: Record<"number" | "string" | "array" | "object", RuleGroup> = {
38 number: {type: "number", rules: []},
39 string: {type: "string", rules: []},
40 array: {type: "array", rules: []},
41 object: {type: "object", rules: []},
42 }
43 return {
44 types: {...groups, integer: true, boolean: true, null: true},
45 rules: [{rules: []}, groups.number, groups.string, groups.array, groups.object],
46 post: {rules: []},
47 all: {},
48 keywords: {},
49 }
50}