-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson.ts
349 lines (285 loc) · 11.2 KB
/
json.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
// deno-lint-ignore no-explicit-any
export type Json = any
/** @deprecated Use `SafeUnknown` instead of validators */
export interface StringDescriptor {
type: 'string'
values?: string[]
}
/** @deprecated Use `SafeUnknown` instead of validators */
export interface NumberDescriptor {
type: 'number'
min?: number
max?: number
/** @default false */
forbidDecimals?: boolean
/** @default false */
canBeNaN?: boolean
}
/** @deprecated Use `SafeUnknown` instead of validators */
export interface NullDescriptor {
type: 'null'
}
/** @deprecated Use `SafeUnknown` instead of validators */
export interface BooleanDescriptor {
type: 'boolean'
}
/** @deprecated Use `SafeUnknown` instead of validators */
export interface ArrayDescriptor {
type: 'array'
keyType: JsonDescriptor
maxLength?: number
minLength?: number
}
/** @deprecated Use `SafeUnknown` instead of validators */
export interface ObjectDescriptor {
type: 'object'
/** Either `keys` or `valueType` must be specified */
keys?: {
[key: string]: JsonDescriptor
}
/**
* The key can be anything, but the value must match this.
* Either `keys` or `valueType` must be specified
*/
valueType?: JsonDescriptor
}
/** @deprecated Use `SafeUnknown` instead of validators */
export interface AnyDescriptor {
type: 'any'
}
/** @deprecated Use `SafeUnknown` instead of validators */
export interface TypeChoiceDescriptor {
type: 'choice'
options: JsonDescriptor[]
}
/** @deprecated Use `SafeUnknown` instead of validators */
export type JsonDescriptor =
| NullDescriptor
| StringDescriptor
| NumberDescriptor
| BooleanDescriptor
| ArrayDescriptor
| ObjectDescriptor
| AnyDescriptor
| TypeChoiceDescriptor
/** @deprecated Use `SafeUnknown` instead of validators */
export interface ValidatorError {
message: string
path: string
}
/** @deprecated Use `SafeUnknown` instead of validators */
export interface ValidatorResultOk {
ok: true
}
/** @deprecated Use `SafeUnknown` instead of validators */
export interface ValidatorResultNotOk {
ok: false
errors: ValidatorError[]
}
/** @deprecated Use `SafeUnknown` instead of validators */
export type ValidatorResult = ValidatorResultOk | ValidatorResultNotOk
/** @deprecated Use `SafeUnknown` instead of validators */
export function validateJson(descriptor: JsonDescriptor, json: Json): ValidatorResult {
interface ValidatorResultOk {
ok: true
}
interface ValidatorResultNotOk {
ok: false
errors: ValidatorError[]
}
type ValidatorResult = ValidatorResultOk | ValidatorResultNotOk
const ok = (): ValidatorResultOk => ({ ok: true })
const notOk = (errors: ValidatorError[]): ValidatorResultNotOk => ({ ok: false, errors })
const getIdKeyFormat = (path: string, key: string) => path + (/[a-zA-Z_$][a-zA-Z0-9_$]*/.test(key) ? `.${key}` : `['${key}']`)
function getCorrectValidator(descriptor: JsonDescriptor) {
let validator: null | ((value: Json, path: string) => ValidatorResult) = null
if (descriptor.type === 'null') validator = (value, path) => validateNull(value, descriptor, path)
else if (descriptor.type === 'string') validator = (value, path) => validateString(value, descriptor, path)
else if (descriptor.type === 'number') validator = (value, path) => validateNumber(value, descriptor, path)
else if (descriptor.type === 'boolean') validator = (value, path) => validateBoolean(value, descriptor, path)
else if (descriptor.type === 'array') validator = (value, path) => validateArray(value, descriptor, path)
else if (descriptor.type === 'object') validator = (value, path) => validateObject(value, descriptor, path)
else if (descriptor.type === 'any') validator = (value, path) => validateAny(value, descriptor, path)
else validator = (value, path) => validateChoice(value, descriptor, path)
return validator
}
function validateNull(value: Json, _descriptor: NullDescriptor, path: string): ValidatorResult {
if (value === null || value === undefined) return ok()
return notOk([{ message: `Expected null, but got: ${value}`, path }])
}
function validateString(value: Json, descriptor: StringDescriptor, path: string): ValidatorResult {
if (typeof value !== 'string') return notOk([{ message: `Expected a string, but got: '${value}'`, path }])
if (!descriptor.values) return ok()
if (descriptor.values.indexOf(value) === -1) {
return notOk([
{
message: `Expected one of the following values: ${
descriptor.values
.map((i) => `'${i}'`)
.join(', ')
}, but found '${value}'`,
path,
},
])
}
return ok()
}
function validateNumber(value: Json, descriptor: NumberDescriptor, path: string): ValidatorResult {
if (typeof value !== 'number') return notOk([{ message: `Expected a number, but got: '${value}'`, path }])
if (isNaN(value)) {
if (descriptor.canBeNaN) return ok()
else return notOk([{ message: `NaN is not allowed`, path }])
}
if (descriptor.min !== undefined && value < descriptor.min) {
return notOk([{ message: `Value was ${value}, lower than the minimum of ${descriptor.min}`, path }])
}
if (descriptor.max !== undefined && value > descriptor.max) {
return notOk([{ message: `Value was ${value}, greater than the maximum of ${descriptor.max}`, path }])
}
if (descriptor.forbidDecimals && String(value).indexOf('.') !== -1) {
return notOk([{ message: `Decimal numbers are not allowed`, path }])
}
return ok()
}
function validateBoolean(value: Json, _descriptor: BooleanDescriptor, path: string): ValidatorResult {
if (typeof value !== 'boolean') return notOk([{ message: `Expected a boolean, but got: ${value}`, path }])
return ok()
}
function validateArray(value: Json, descriptor: ArrayDescriptor, path: string): ValidatorResult {
if (!Array.isArray(value)) return notOk([{ message: `Expected an array, but got: ${value}`, path }])
if (descriptor.minLength !== undefined && value.length < descriptor.minLength) {
return notOk([{ message: `Array length was ${value}, lower than the minimum length of ${descriptor.minLength}`, path }])
}
if (descriptor.maxLength !== undefined && value.length > descriptor.maxLength) {
return notOk([{ message: `Array length was ${value}, greater than the maximum length of ${descriptor.maxLength}`, path }])
}
const errors: ValidatorError[] = []
const validator = getCorrectValidator(descriptor.keyType)
value.forEach((item, index) => {
const res = validator(item, `${path}[${index}]`)
if (!res.ok) errors.push(...res.errors)
})
if (errors.length) return notOk(errors)
return ok()
}
function validateObject(value: Json, descriptor: ObjectDescriptor, path: string): ValidatorResult {
if (typeof value !== 'object' || Array.isArray(value) || value === null || typeof value === 'undefined') {
return notOk([{ message: `Expected an object, but found: ${value}`, path }])
}
// Keys and value types must match
if (descriptor.keys) {
const errors: ValidatorError[] = []
const uncheckedValueKeys = Object.keys(value)
Object.keys(descriptor.keys).forEach((key) => {
if (!descriptor.keys) throw new Error(`What was... wasn't...`)
const index = uncheckedValueKeys.indexOf(key)
if (index === -1) return errors.push({ message: `Property '${key}' was expected, but not found`, path })
uncheckedValueKeys.splice(index, 1)
const newPath = getIdKeyFormat(path, key)
const res = getCorrectValidator(descriptor.keys[key])(value[key], newPath)
if (!res.ok) errors.push(...res.errors)
})
uncheckedValueKeys.forEach((key) => errors.push({ message: `Property '${key}' is not allowed here`, path }))
if (errors.length) return notOk(errors)
return ok()
} // Each key must match this descriptor
else if (descriptor.valueType) {
const validator = getCorrectValidator(descriptor.valueType)
const errors: ValidatorError[] = []
Object.keys(value).forEach((key) => {
const item = value[key]
const newPath = getIdKeyFormat(path, key)
const res = validator(item, newPath)
if (!res.ok) errors.push(...res.errors)
return ok()
})
if (errors.length) return notOk(errors)
return ok()
} // Object is expected to be empty
else {
if (Object.keys(value).length) return notOk([{ message: `Expected object to be empty`, path }])
return ok()
}
}
function validateAny(_value: Json, _descriptor: AnyDescriptor, _path: string): ValidatorResult {
return ok()
}
function validateChoice(value: Json, descriptor: TypeChoiceDescriptor, path: string): ValidatorResult {
if (!descriptor.options.length) return ok()
const errors: ValidatorError[] = []
for (const typeDescriptor of descriptor.options) {
const res = getCorrectValidator(typeDescriptor)(value, path)
if (!res.ok) errors.push(...res.errors)
else return res
}
return notOk([
{ message: `Did not match any of the types expected: ${descriptor.options.map((t) => t.type).join(', ')}`, path },
...errors,
])
}
const res = getCorrectValidator(descriptor)(json, '')
// Remove the first dot on some of the paths and return
if (res.ok) return res
return {
ok: false,
errors: res.errors.map((error) => {
if (error.path.startsWith('.')) error.path = error.path.slice(1)
return error
}),
}
}
/** @deprecated Will be removed in next major release. Use `jsonDecode` instead */
// deno-lint-ignore ban-types
export function jsonParse(json: string, fallback: {} | [] | null = null): Json {
if (!json.length) return json
if (json.startsWith('"') && json.endsWith('"')) return json.slice(1, -1)
if (json === 'true') return true
if (json === 'false') return false
if (json === 'null' || json === 'undefined') return null
if ((json.startsWith('{') && json.endsWith('}')) || (json.startsWith('[') && json.endsWith(']'))) {
try {
return JSON.parse(json)
} catch (e) {
if (!fallback) {
e.raw = json
throw e
}
console.warn(`Failed to parse JSON. Resorting to fallback. DUMP:`, e, json)
return fallback
}
}
const numTry = Number(json)
if (!isNaN(numTry)) return numTry
return json
}
/** @deprecated Will be removed in next major release. Use `jsonEncode` instead */
export function jsonStringify(data: Json, spacer = ''): string {
return JSON.stringify(data, undefined, spacer)
}
/**
* Parse json.
*
* Wraps the native implementation, the difference being that primitives are parsed at the top level
* (i.e. `jsonParse("null")`) is valid */
export function jsonDecode(json: string): unknown {
json = json.trim()
if (!json.length) throw new Error('Expected a json primitive, object, or array, but found nothing')
if (json.startsWith('"') && json.endsWith('"')) return json.slice(1, -1)
if (json === 'true') return true
if (json === 'false') return false
if (json === 'null' || json === 'undefined') return null
if ((json.startsWith('{') && json.endsWith('}')) || (json.startsWith('[') && json.endsWith(']'))) {
try {
return JSON.parse(json)
} catch (error) {
throw new Error(`Failed to parse json. ${error.message} ... Dump: "${json}"`)
}
}
const numTry = Number(json)
if (!isNaN(numTry)) return numTry
throw new Error(`Expected a json primitive, object, or array, but found: "${json}"`)
}
/** Stringify json with an optional spacer. Wraps the native implementation */
export function jsonEncode(data: unknown, spacer = ''): string {
return JSON.stringify(data, undefined, spacer)
}