/
index.ts
198 lines (177 loc) · 6.67 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import type {AnySchema} from "../../types"
import type {SchemaCxt, SchemaObjCxt} from ".."
import type {InstanceOptions} from "../../ajv"
import {boolOrEmptySchema, topBoolOrEmptySchema} from "./boolSchema"
import {coerceAndCheckDataType, getSchemaTypes} from "./dataType"
import {schemaKeywords} from "./iterate"
import {_, nil, str, Block, Code, Name, CodeGen} from "../codegen"
import N from "../names"
import {resolveUrl} from "../resolve"
import {schemaHasRulesButRef, checkUnknownRules} from "../util"
// schema compilation - generates validation function, subschemaCode (below) is used for subschemas
export function validateFunctionCode(it: SchemaCxt): void {
if (isSchemaObj(it)) {
checkKeywords(it)
if (schemaCxtHasRules(it)) {
topSchemaObjCode(it)
return
}
}
validateFunction(it, () => topBoolOrEmptySchema(it))
}
function validateFunction(
{gen, validateName, schema, schemaEnv, opts}: SchemaCxt,
body: Block
): void {
gen.return(() =>
opts.code.es5
? gen.func(validateName, _`${N.data}, ${N.valCxt}`, schemaEnv.$async, () => {
gen.code(_`"use strict"; ${funcSourceUrl(schema, opts)}`)
destructureValCxtES5(gen, opts)
gen.code(body)
})
: gen.func(validateName, _`${N.data}, ${destructureValCxt(opts)}`, schemaEnv.$async, () =>
gen.code(funcSourceUrl(schema, opts)).code(body)
)
)
}
function destructureValCxt(opts: InstanceOptions): Code {
return _`{${N.dataPath}="", ${N.parentData}, ${N.parentDataProperty}, ${N.rootData}=${N.data}${
opts.dynamicRef ? _`, ${N.dynamicAnchors}={}` : nil
}}={}`
}
function destructureValCxtES5(gen: CodeGen, opts: InstanceOptions): void {
gen.if(
N.valCxt,
() => {
gen.var(N.dataPath, _`${N.valCxt}.${N.dataPath}`)
gen.var(N.parentData, _`${N.valCxt}.${N.parentData}`)
gen.var(N.parentDataProperty, _`${N.valCxt}.${N.parentDataProperty}`)
gen.var(N.rootData, _`${N.valCxt}.${N.rootData}`)
if (opts.dynamicRef) gen.var(N.dynamicAnchors, _`${N.valCxt}.${N.dynamicAnchors}`)
},
() => {
gen.var(N.dataPath, _`""`)
gen.var(N.parentData, _`undefined`)
gen.var(N.parentDataProperty, _`undefined`)
gen.var(N.rootData, N.data)
if (opts.dynamicRef) gen.var(N.dynamicAnchors, _`{}`)
}
)
}
function topSchemaObjCode(it: SchemaObjCxt): void {
const {schema, opts, gen} = it
validateFunction(it, () => {
if (opts.$comment && schema.$comment) commentKeyword(it)
checkNoDefault(it)
gen.let(N.vErrors, null)
gen.let(N.errors, 0)
if (opts.unevaluated) resetEvaluated(it)
typeAndKeywords(it)
returnResults(it)
})
return
}
function resetEvaluated(it: SchemaObjCxt): void {
// TODO maybe some hook to execute it in the end to check whether props/items are Name, as in assignEvaluated
const {gen, validateName} = it
it.evaluated = gen.const("evaluated", _`${validateName}.evaluated`)
gen.if(_`${it.evaluated}.dynamicProps`, () => gen.assign(_`${it.evaluated}.props`, _`undefined`))
gen.if(_`${it.evaluated}.dynamicItems`, () => gen.assign(_`${it.evaluated}.items`, _`undefined`))
}
function funcSourceUrl(schema: AnySchema, opts: InstanceOptions): Code {
return typeof schema == "object" && schema.$id && (opts.code.source || opts.code.process)
? _`/*# sourceURL=${schema.$id} */`
: nil
}
// schema compilation - this function is used recursively to generate code for sub-schemas
export function subschemaCode(it: SchemaCxt, valid: Name): void {
if (isSchemaObj(it)) {
checkKeywords(it)
if (schemaCxtHasRules(it)) {
subSchemaObjCode(it, valid)
return
}
}
boolOrEmptySchema(it, valid)
}
export function schemaCxtHasRules({schema, self}: SchemaCxt): boolean {
if (typeof schema == "boolean") return !schema
for (const key in schema) if (self.RULES.all[key]) return true
return false
}
function isSchemaObj(it: SchemaCxt): it is SchemaObjCxt {
return typeof it.schema != "boolean"
}
function subSchemaObjCode(it: SchemaObjCxt, valid: Name): void {
const {schema, gen, opts} = it
if (opts.$comment && schema.$comment) commentKeyword(it)
updateContext(it)
checkAsync(it)
const errsCount = gen.const("_errs", N.errors)
typeAndKeywords(it, errsCount)
// TODO var
gen.var(valid, _`${errsCount} === ${N.errors}`)
}
function checkKeywords(it: SchemaObjCxt): void {
checkUnknownRules(it)
checkRefsAndKeywords(it)
}
function typeAndKeywords(it: SchemaObjCxt, errsCount?: Name): void {
const types = getSchemaTypes(it.schema)
const checkedTypes = coerceAndCheckDataType(it, types)
schemaKeywords(it, types, !checkedTypes, errsCount)
}
function checkRefsAndKeywords(it: SchemaObjCxt): void {
const {schema, errSchemaPath, opts, self} = it
if (schema.$ref && opts.ignoreKeywordsWithRef && schemaHasRulesButRef(schema, self.RULES)) {
self.logger.warn(`$ref: keywords ignored in schema at path "${errSchemaPath}"`)
}
}
function checkNoDefault(it: SchemaObjCxt): void {
const {schema, opts} = it
if (schema.default !== undefined && opts.useDefaults && opts.strict) {
checkStrictMode(it, "default is ignored in the schema root")
}
}
function updateContext(it: SchemaObjCxt): void {
if (it.schema.$id) it.baseId = resolveUrl(it.baseId, it.schema.$id)
}
function checkAsync(it: SchemaObjCxt): void {
if (it.schema.$async && !it.schemaEnv.$async) throw new Error("async schema in sync schema")
}
function commentKeyword({gen, schemaEnv, schema, errSchemaPath, opts}: SchemaObjCxt): void {
const msg = schema.$comment
if (opts.$comment === true) {
gen.code(_`${N.self}.logger.log(${msg})`)
} else if (typeof opts.$comment == "function") {
const schemaPath = str`${errSchemaPath}/$comment`
const rootName = gen.scopeValue("root", {ref: schemaEnv.root})
gen.code(_`${N.self}.opts.$comment(${msg}, ${schemaPath}, ${rootName}.schema)`)
}
}
function returnResults(it: SchemaCxt): void {
const {gen, schemaEnv, validateName, ValidationError, opts} = it
if (schemaEnv.$async) {
// TODO assign unevaluated
gen.if(
_`${N.errors} === 0`,
() => gen.return(N.data),
() => gen.throw(_`new ${ValidationError as Name}(${N.vErrors})`)
)
} else {
gen.assign(_`${validateName}.errors`, N.vErrors)
if (opts.unevaluated) assignEvaluated(it)
gen.return(_`${N.errors} === 0`)
}
}
function assignEvaluated({gen, evaluated, props, items}: SchemaCxt): void {
if (props instanceof Name) gen.assign(_`${evaluated}.props`, props)
if (items instanceof Name) gen.assign(_`${evaluated}.items`, items)
}
export function checkStrictMode(it: SchemaCxt, msg: string, mode = it.opts.strict): void {
if (!mode) return
msg = `strict mode: ${msg}`
if (mode === true) throw new Error(msg)
it.self.logger.warn(msg)
}