[6a3a178] | 1 | import type {AddedKeywordDefinition} from "../types"
|
---|
| 2 |
|
---|
| 3 | const _jsonTypes = ["string", "number", "integer", "boolean", "null", "object", "array"] as const
|
---|
| 4 |
|
---|
| 5 | export type JSONType = typeof _jsonTypes[number]
|
---|
| 6 |
|
---|
| 7 | const jsonTypes: Set<string> = new Set(_jsonTypes)
|
---|
| 8 |
|
---|
| 9 | export function isJSONType(x: unknown): x is JSONType {
|
---|
| 10 | return typeof x == "string" && jsonTypes.has(x)
|
---|
| 11 | }
|
---|
| 12 |
|
---|
| 13 | type ValidationTypes = {
|
---|
| 14 | [K in JSONType]: boolean | RuleGroup | undefined
|
---|
| 15 | }
|
---|
| 16 |
|
---|
| 17 | export 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 |
|
---|
| 25 | export interface RuleGroup {
|
---|
| 26 | type?: JSONType
|
---|
| 27 | rules: Rule[]
|
---|
| 28 | }
|
---|
| 29 |
|
---|
| 30 | // This interface wraps KeywordDefinition because definition can have multiple keywords
|
---|
| 31 | export interface Rule {
|
---|
| 32 | keyword: string
|
---|
| 33 | definition: AddedKeywordDefinition
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 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: []},
|
---|
| 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 | }
|
---|