1 import type {AddedKeywordDefinition} from "../types"
3 const _jsonTypes = ["string", "number", "integer", "boolean", "null", "object", "array"] as const
5 export type JSONType = typeof _jsonTypes[number]
7 const jsonTypes: Set<string> = new Set(_jsonTypes)
9 export function isJSONType(x: unknown): x is JSONType {
10 return typeof x == "string" && jsonTypes.has(x)
13 type ValidationTypes = {
14 [K in JSONType]: boolean | RuleGroup | undefined
17 export interface ValidationRules {
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
25 export interface RuleGroup {
30 // This interface wraps KeywordDefinition because definition can have multiple keywords
31 export interface Rule {
33 definition: AddedKeywordDefinition
36 export 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: []},
44 types: {...groups, integer: true, boolean: true, null: true},
45 rules: [{rules: []}, groups.number, groups.string, groups.array, groups.object],