forked from rvagg/js-ipld-schema-validator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathipld-schema-validator.js
451 lines (398 loc) · 18.3 KB
/
ipld-schema-validator.js
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/* eslint-disable no-new-func */
/**
* @typedef {import('ipld-schema/schema-schema').EnumValue} EnumValue
* @typedef {import('ipld-schema/schema-schema').KindInt} KindInt
* @typedef {import('ipld-schema/schema-schema').KindString} KindString
* @typedef {import('ipld-schema/schema-schema').Schema} Schema
* @typedef {import('ipld-schema/schema-schema').Type} Type
* @typedef {import('ipld-schema/schema-schema').TypeName} TypeName
* @typedef {import('ipld-schema/schema-schema').TypeTerm} TypeTerm
* @typedef {(obj:any)=>boolean} ValidatorFunction
*/
const safeNameRe = /^[a-z][a-z0-9]+$/i
/**
* @param {string} name
* @returns {string}
*/
function safeReference (name) {
return safeNameRe.test(name) ? `.${name}` : `['${name}']`
}
/**
* @param {string[]|number[]} list
* @returns {string}
*/
function fromArray (list) {
return JSON.stringify(list).replace(/"/g, '\'').replace(/,/g, ', ')
}
const KindsDefn =
`const Kinds = {
Null: /** @returns {boolean} */ (/** @type {any} */ obj) => obj === null,
Int: /** @returns {boolean} */ (/** @type {any} */ obj) => Number.isInteger(obj),
Float: /** @returns {boolean} */ (/** @type {any} */ obj) => typeof obj === 'number' && Number.isFinite(obj),
String: /** @returns {boolean} */ (/** @type {any} */ obj) => typeof obj === 'string',
Bool: /** @returns {boolean} */ (/** @type {any} */ obj) => typeof obj === 'boolean',
Bytes: /** @returns {boolean} */ (/** @type {any} */ obj) => obj instanceof Uint8Array,
Link: /** @returns {boolean} */ (/** @type {any} */ obj) => !Kinds.Null(obj) && typeof obj === 'object' && obj.asCID === obj,
List: /** @returns {boolean} */ (/** @type {any} */ obj) => Array.isArray(obj),
Map: /** @returns {boolean} */ (/** @type {any} */ obj) => !Kinds.Null(obj) && typeof obj === 'object' && obj.asCID !== obj && !Kinds.List(obj) && !Kinds.Bytes(obj)
}`
const ScalarKindNames = ['Null', 'Int', 'Float', 'String', 'Bool', 'Bytes', 'Link']
const ScalarKindNamesLower = ScalarKindNames.map((n) => n.toLowerCase())
// const TypeKindNames = ['string', 'bool', 'bytes', 'int', 'float', 'map', 'list', 'link', 'union', 'struct', 'enum', 'copy']
/** @type {{ [ k in string]: Type }} */
const implicits = {
Null: /** @type {TypeNull} */ { kind: 'null' },
Int: /** @type {TypeInt} */ { kind: 'int' },
Float: /** @type {TypeFloat} */ { kind: 'float' },
String: /** @type {TypeString} */ { kind: 'string' },
Bool: /** @type {TypeBool} */ { kind: 'bool' },
Bytes: /** @type {TypeBytes} */ { kind: 'bytes' },
Link: /** @type {TypeLink} */ { kind: 'link' }
}
implicits.AnyScalar = /** @type {TypeUnion} */ {
kind: 'union',
representation: {
kinded: {
bool: 'Bool',
string: 'String',
bytes: 'Bytes',
int: 'Int',
float: 'Float'
}
}
}
implicits.AnyMap = /** @type {TypeMap} */ {
kind: 'map',
keyType: 'String',
valueType: 'Any'
}
implicits.AnyList = /** @type {TypeList} */ {
kind: 'list',
valueType: 'Any'
}
implicits.Any = /** @type {TypeUnion} */ {
kind: 'union',
representation: {
kinded: {
bool: 'Bool',
string: 'String',
bytes: 'Bytes',
int: 'Int',
float: 'Float',
null: 'Null',
link: 'Link',
map: 'AnyMap',
list: 'AnyList'
}
}
}
/**
* @param {string} s
* @returns {string}
*/
function tc (s) {
return s.charAt(0).toUpperCase() + s.substring(1)
}
/**
* @param {Schema} schema
* @param {string} root
* @returns {ValidatorFunction}
*/
function create (schema, root) {
if (!root || typeof root !== 'string') {
throw new TypeError('A root is required')
}
const builder = new Builder(schema)
builder.addType(root)
let fn = builder.dumpValidators()
fn += `return Types['${root}'](obj);`
// console.log(fn)
return /** @type {ValidatorFunction} */ (new Function('obj', fn))
}
class Builder {
/**
* @param {Schema} schema
*/
constructor (schema) {
if (!schema || typeof schema.types !== 'object') {
throw new TypeError('Invalid schema definition')
}
// new schema with implicits
this.schema = {
types: Object.assign({}, implicits, schema.types)
}
/** @type {Record<string, string>} */
this.typeValidators = {}
}
dumpValidators () {
const objKey = (/** @type {string} */ name) => {
return safeNameRe.test(name) ? name : `'${name}'`
}
const fn = `${KindsDefn}\n` +
'/** @type {{ [k in string]: (obj:any)=>boolean}} */\n' +
`const Types = {\n${Object.entries(this.typeValidators).map(([name, fn]) => ` ${objKey(name)}: ${fn}`).join(',\n')}\n}\n`
return fn
}
/**
* @param {TypeName} typeName
* @param {Type} [typeDef]
* @returns {void}
*/
addType (typeName, typeDef) {
if (this.typeValidators[typeName]) { // already added this one
return
}
if (typeName === 'Any') {
// special case for Any because it's a recursive definition, so we set up a dummy in place so
// any recursive attempt to add finds a definition before it's set
this.typeValidators[typeName] = '() => false'
}
if (typeDef === undefined && typeName in this.schema.types && typeof this.schema.types[typeName] === 'object') {
typeDef = this.schema.types[typeName]
}
if (typeDef === undefined) {
throw new TypeError(`A type must match an existing type definition ("${typeName}")`)
}
if (typeof typeDef === 'object' &&
typeof typeDef.kind === 'string' &&
ScalarKindNamesLower.includes(typeDef.kind)) {
this.typeValidators[typeName] = `Kinds.${tc(typeDef.kind)}`
return
}
/**
* @param {TypeTerm|string} defType
* @param {string} name
* @returns {string}
*/
const defineType = (defType, name) => {
if (defType === typeName) {
throw new Error(`Recursive typedef in type "${typeName}"`)
}
let innerTypeName = defType
if (typeof innerTypeName === 'object' && typeof defType !== 'string' && defType.kind) { // anonymous inline map or list!
innerTypeName = `${typeName} > ${name} (anon)`
this.addType(innerTypeName, defType)
} else if (typeof innerTypeName === 'string') {
this.addType(innerTypeName)
} else {
throw new Error(`Bad type for "${name}" in "${typeName}"`)
}
return innerTypeName
}
if (typeDef.kind === 'list') {
const valueTypeName = defineType(typeDef.valueType, 'valueType')
let valueValidator = `Types${safeReference(valueTypeName)}`
if (typeDef.valueNullable === true) {
valueValidator = `(v) => v === null || ${valueValidator}(v)`
}
this.typeValidators[typeName] = `/** @returns {boolean} */ (/** @type {any} */ obj) => Kinds.List(obj) && Array.prototype.every.call(obj, ${valueValidator})`
return
}
if (typeDef.kind === 'map') {
if (typeDef.keyType !== 'String') {
throw new Error(`Invalid keyType for Map "${typeName}", expected String, found "${typeDef.keyType}"`)
}
let representation = 'map'
if (typeDef.representation !== undefined) {
if ('listpairs' in typeDef.representation && typeof typeDef.representation.listpairs === 'object') {
representation = 'listpairs'
} else if (!('map' in typeDef.representation) || typeof typeDef.representation.map !== 'object') {
throw new Error(`Unsupported map representation "${Object.keys(typeDef.representation).join(',')}"`)
}
}
const valueTypeName = defineType(typeDef.valueType, 'valueType')
let valueValidator = `Types${safeReference(valueTypeName)}`
if (typeDef.valueNullable === true) {
valueValidator = `(v) => v === null || ${valueValidator}(v)`
}
if (representation === 'listpairs') {
this.typeValidators[typeName] = `/** @returns {boolean} */ (/** @type {any} */ obj) => Kinds.List(obj) && Array.prototype.every.call(obj, (e) => Kinds.List(e) && e.length === 2 && Kinds.String(e[0]) && (${valueValidator})(e[1]))`
return
}
this.typeValidators[typeName] = `/** @returns {boolean} */ (/** @type {any} */ obj) => Kinds.Map(obj) && Array.prototype.every.call(Object.values(obj), ${valueValidator})`
return
}
if (typeDef.kind === 'struct') {
let representation = 'map'
if (typeDef.representation !== undefined) {
if ('tuple' in typeDef.representation && typeof typeDef.representation.tuple === 'object') {
representation = 'tuple'
} else if (!('map' in typeDef.representation) || typeof typeDef.representation.map !== 'object') {
throw new Error(`Unsupported struct representation for "${typeName}": "${Object.keys(typeDef.representation).join(',')}"`)
}
}
let requiredFields = []
for (let [fieldName, fieldDef] of Object.entries(typeDef.fields)) {
let required = representation !== 'map' || fieldDef.optional !== true
if (typeDef.representation !== undefined &&
'map' in typeDef.representation &&
typeof typeDef.representation.map === 'object' &&
typeof typeDef.representation.map.fields === 'object' &&
typeof typeDef.representation.map.fields[fieldName] === 'object') {
if (typeDef.representation.map.fields[fieldName].implicit !== undefined) {
required = false
}
const fieldDef = typeDef.representation.map.fields[fieldName]
if (typeof fieldDef.rename === 'string') {
fieldName = fieldDef.rename
}
}
const fieldKey = `${typeName} > ${fieldName}`
if (required) {
requiredFields.push(fieldName)
}
if (representation !== 'map' && fieldDef.optional === true) {
throw new Error(`Struct "${typeName}" includes "optional" fields for non-map struct`)
}
const fieldTypeName = defineType(fieldDef.type, fieldName)
let fieldValidator = `Types${safeReference(fieldTypeName)}(obj)`
if (fieldDef.nullable === true) {
fieldValidator = `obj === null || ${fieldValidator}`
}
this.typeValidators[fieldKey] = `/** @returns {boolean} */ (/** @type {any} */ obj) => ${fieldValidator}`
}
if (representation === 'tuple') {
if (typeDef.representation &&
'tuple' in typeDef.representation &&
Array.isArray(typeDef.representation.tuple.fieldOrder)) {
requiredFields = typeDef.representation.tuple.fieldOrder
}
this.typeValidators[typeName] = `/** @returns {boolean} */ (/** @type {any} */ obj) => Kinds.List(obj) && obj.length === ${requiredFields.length}${requiredFields.map((fieldName, i) => ` && Types['${typeName} > ${fieldName}'](obj[${i}])`).join('')}`
} else {
this.typeValidators[typeName] = `/** @returns {boolean} */ (/** @type {any} */ obj) => { const keys = obj && Object.keys(obj); return Kinds.Map(obj) && ${fromArray(requiredFields)}.every((k) => keys.includes(k)) && Object.entries(obj).every(([name, value]) => Types['${typeName} > ' + name] && Types['${typeName} > ' + name](value)) }`
}
return
}
if (typeDef.kind === 'union') {
if (typeof typeDef.representation !== 'object') {
throw new Error(`Bad union definition for "${typeName}"`)
}
if ('keyed' in typeDef.representation && typeof typeDef.representation.keyed === 'object') {
const keys = typeDef.representation.keyed
const validators = Object.entries(keys).map(([key, innerTypeName]) => {
if (typeof innerTypeName !== 'string') {
throw new Error(`Keyed union "${typeName} refers to non-string type name: ${JSON.stringify(innerTypeName)}`)
}
this.addType(innerTypeName)
const validator = `Types${safeReference(innerTypeName)}`
return `(keys[0] === '${key}' && ${validator}(obj${safeReference(key)}))`
})
this.typeValidators[typeName] = `/** @returns {boolean} */ (/** @type {any} */ obj) => { const keys = obj && Object.keys(obj); return Kinds.Map(obj) && keys.length === 1 && ${fromArray(Object.keys(keys))}.includes(keys[0]) && (${validators.join(' || ')}) }`
return
}
if ('kinded' in typeDef.representation && typeof typeDef.representation.kinded === 'object') {
const kinds = typeDef.representation.kinded
const validators = Object.entries(kinds).map(([kind, innerTypeName]) => {
if (typeof innerTypeName === 'object' && innerTypeName.kind === 'link') {
const defn = innerTypeName
innerTypeName = `${typeName} > ${innerTypeName.expectedType} (anon)`
this.addType(innerTypeName, defn)
}
if (typeof innerTypeName !== 'string') {
throw new Error(`Kinded union "${typeName} refers to non-string type name: ${JSON.stringify(innerTypeName)}`)
}
this.addType(innerTypeName)
// the Kinds.X(obj) prefix here results in a double-check in practice once we go into Types["Y"],
// because we should be able to presume that the type in question will do a kind check of its own.
// _But_, it makes sure that a broken schema that uses a bad kind discriminator will properly fail
// instead of erroneously passing
return `(Kinds.${tc(kind)}(obj) && Types${safeReference(innerTypeName)}(obj))`
})
this.typeValidators[typeName] = `/** @returns {boolean} */ (/** @type {any} */ obj) => ${validators.join(' || ')}`
return
}
if ('inline' in typeDef.representation && typeof typeDef.representation.inline === 'object') {
const inline = typeDef.representation.inline
if (typeof inline.discriminantKey !== 'string') {
throw new Error(`Expected "discriminantKey" for inline union "${typeName}"`)
}
if (typeof inline.discriminantTable !== 'object') {
throw new Error(`Expected "discriminantTable" for inline union "${typeName}"`)
}
const validators = Object.entries(inline.discriminantTable).map(([key, innerTypeName]) => {
if (typeof innerTypeName !== 'string') {
throw new Error(`Inline union "${typeName} refers to non-string type name: ${JSON.stringify(innerTypeName)}`)
}
this.addType(innerTypeName)
return `(key === '${key}' && Types${safeReference(innerTypeName)}(obj))`
})
this.typeValidators[typeName] = `/** @returns {boolean} */ (/** @type {any} */ obj) => { const key = obj && obj${safeReference(inline.discriminantKey)}; if (!Kinds.Map(obj) || !Kinds.String(key)) { return false }; obj = Object.assign({}, obj); delete obj${safeReference(inline.discriminantKey)}; return ${validators.join(' || ')} }`
return
}
if ('envelope' in typeDef.representation && typeof typeDef.representation.envelope === 'object') {
const envelope = typeDef.representation.envelope
if (typeof envelope.discriminantKey !== 'string') {
throw new Error(`Expected "discriminantKey" for envelope union "${typeName}"`)
}
if (typeof envelope.contentKey !== 'string') {
throw new Error(`Expected "contentKey" for envelope union "${typeName}"`)
}
if (typeof envelope.discriminantTable !== 'object') {
throw new Error(`Expected "discriminantTable" for envelope union "${typeName}"`)
}
const validators = Object.entries(envelope.discriminantTable).map(([key, innerTypeName]) => {
if (typeof innerTypeName !== 'string') {
throw new Error(`Envelope union "${typeName} refers to non-string type name: ${JSON.stringify(innerTypeName)}`)
}
this.addType(innerTypeName)
return `(key === '${key}' && Types${safeReference(innerTypeName)}(content))`
})
this.typeValidators[typeName] = `/** @returns {boolean} */ (/** @type {any} */ obj) => { const key = obj && obj${safeReference(envelope.discriminantKey)}; const content = obj && obj${safeReference(envelope.contentKey)}; return Kinds.Map(obj) && Kinds.String(key) && content !== undefined && (${validators.join(' || ')}) }`
return
}
if ('byteprefix' in typeDef.representation && typeof typeDef.representation.byteprefix === 'object') {
/** @type {number[]} */
const bytes = Object.values(typeDef.representation.byteprefix)
for (const byte of bytes) {
if (typeof byte !== 'number' || !Number.isInteger(byte) || byte < 0 || byte > 0xff) {
throw new Error(`Invalid byteprefix byte for "${typeName}": "${byte}"`)
}
}
this.typeValidators[typeName] = `/** @returns {boolean} */ (/** @type {any} */ obj) => { return Kinds.Bytes(obj) && obj.length >= 1 && ${fromArray(bytes)}.includes(obj[0]) }`
return
}
throw new Error(`Unsupported union type for "${typeName}": "${Object.keys(typeDef.representation).join(',')}"`)
}
if (typeDef.kind === 'enum') {
if (typeof typeDef.members !== 'object') {
throw new Error('Enum needs a "members" list')
}
/** @type {string[]|number[]}} */
let values
let representation = 'string'
if (typeof typeDef.representation === 'object') {
if ('string' in typeDef.representation && typeof typeDef.representation.string === 'object') {
const renames = typeDef.representation.string
values = Object.keys(typeDef.members).map((v) => {
v = renames[v] !== undefined ? renames[v] : v
if (typeof v !== 'string') {
throw new Error('Enum members must be strings')
}
return v
})
} else if ('int' in typeDef.representation && typeof typeDef.representation.int === 'object') {
const renames = typeDef.representation.int
values = Object.keys(typeDef.members).map((v) => {
if (renames[v] === undefined || typeof renames[v] !== 'number' || !Number.isInteger(renames[v])) {
throw new Error('Enum members must be ints')
}
return renames[v]
})
representation = 'int'
} else {
throw new Error('Enum doesn\'t have a valid representation')
}
} else {
throw new Error('Enum doesn\'t have a valid representation')
}
this.typeValidators[typeName] = `/** @returns {boolean} */ (/** @type {any} */ obj) => Kinds.${tc(representation)}(obj) && ${fromArray(values)}.includes(obj)`
return
}
throw new Error(`Can't deal with type kind: "${typeDef.kind}"`)
}
}
module.exports = {
safeReference,
create,
Builder,
}