fastify-ata 0.4.1 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +4 -1
- package/package.json +2 -2
- package/test-types.ts +49 -0
- package/test.js +25 -0
package/index.js
CHANGED
|
@@ -31,7 +31,10 @@ function fastifyAta(fastify, opts, done) {
|
|
|
31
31
|
fastify.setValidatorCompiler(({ schema }) => {
|
|
32
32
|
let validator = cache.get(schema)
|
|
33
33
|
if (!validator) {
|
|
34
|
-
|
|
34
|
+
// Pass schemas registered via `fastify.addSchema` so cross-schema `$ref`
|
|
35
|
+
// (e.g. `{ $ref: 'shared#' }`) resolves. Compilers run at ready() time,
|
|
36
|
+
// after every addSchema, so getSchemas() returns the full bucket.
|
|
37
|
+
validator = new Validator(schema, { ...validatorOpts, schemas: fastify.getSchemas() })
|
|
35
38
|
cache.set(schema, validator)
|
|
36
39
|
}
|
|
37
40
|
const validate = (data) => {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "fastify-ata",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "Fastify plugin for ata-validator. Runtime-competitive with the default, 24x faster serverless cold start, Standard Schema V1.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "index.d.ts",
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
},
|
|
27
27
|
"homepage": "https://github.com/ata-core/fastify-ata#readme",
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"ata-validator": "^0.
|
|
29
|
+
"ata-validator": "^0.18.0",
|
|
30
30
|
"fastify-plugin": "^5.1.0",
|
|
31
31
|
"sanitize-filename": "^1.6.4"
|
|
32
32
|
},
|
package/test-types.ts
CHANGED
|
@@ -36,3 +36,52 @@ app.get('/search', {
|
|
|
36
36
|
void _q
|
|
37
37
|
return { ok: true }
|
|
38
38
|
})
|
|
39
|
+
|
|
40
|
+
// anyOf body narrows to a union
|
|
41
|
+
app.post('/event', {
|
|
42
|
+
schema: {
|
|
43
|
+
body: defineSchema({
|
|
44
|
+
anyOf: [
|
|
45
|
+
{ type: 'object', properties: { kind: { const: 'a' }, n: { type: 'number' } }, required: ['kind', 'n'] },
|
|
46
|
+
{ type: 'object', properties: { kind: { const: 'b' }, s: { type: 'string' } }, required: ['kind', 's'] },
|
|
47
|
+
],
|
|
48
|
+
}),
|
|
49
|
+
},
|
|
50
|
+
}, async (req) => {
|
|
51
|
+
const _exact: Expect<typeof req.body, { kind: 'a'; n: number } | { kind: 'b'; s: string }> = true
|
|
52
|
+
void _exact
|
|
53
|
+
return { ok: true }
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
// $ref body resolves the referenced $defs entry
|
|
57
|
+
app.post('/place', {
|
|
58
|
+
schema: {
|
|
59
|
+
body: defineSchema({
|
|
60
|
+
$defs: { Point: { type: 'object', properties: { x: { type: 'number' }, y: { type: 'number' } }, required: ['x', 'y'] } },
|
|
61
|
+
type: 'object',
|
|
62
|
+
properties: { at: { $ref: '#/$defs/Point' } },
|
|
63
|
+
required: ['at'],
|
|
64
|
+
}),
|
|
65
|
+
},
|
|
66
|
+
}, async (req) => {
|
|
67
|
+
const _x: number = req.body.at.x
|
|
68
|
+
const _exact: Expect<typeof req.body, { at: { x: number; y: number } }> = true
|
|
69
|
+
void _x; void _exact
|
|
70
|
+
return { ok: true }
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
// allOf body narrows to an intersection
|
|
74
|
+
app.post('/merged', {
|
|
75
|
+
schema: {
|
|
76
|
+
body: defineSchema({
|
|
77
|
+
allOf: [
|
|
78
|
+
{ type: 'object', properties: { a: { type: 'number' } }, required: ['a'] },
|
|
79
|
+
{ type: 'object', properties: { b: { type: 'string' } }, required: ['b'] },
|
|
80
|
+
],
|
|
81
|
+
}),
|
|
82
|
+
},
|
|
83
|
+
}, async (req) => {
|
|
84
|
+
const _exact: Expect<typeof req.body, { a: number; b: string }> = true
|
|
85
|
+
void _exact
|
|
86
|
+
return { ok: true }
|
|
87
|
+
})
|
package/test.js
CHANGED
|
@@ -459,6 +459,31 @@ async function run() {
|
|
|
459
459
|
await tApp.close()
|
|
460
460
|
*/
|
|
461
461
|
|
|
462
|
+
// Cross-schema $ref via addSchema: the plugin must pass external schemas to ata,
|
|
463
|
+
// and ata must resolve both external $id refs and draft-07 #anchor refs.
|
|
464
|
+
const refApp = fastify()
|
|
465
|
+
await refApp.register(fastifyAta)
|
|
466
|
+
refApp.addSchema({ $id: 'http://foo/test', type: 'object', properties: { id: { type: 'number' } } })
|
|
467
|
+
refApp.post('/ref', {
|
|
468
|
+
schema: {
|
|
469
|
+
body: {
|
|
470
|
+
$id: 'http://foo/user',
|
|
471
|
+
type: 'object',
|
|
472
|
+
definitions: { address: { $id: '#address', type: 'object', properties: { city: { type: 'string' } }, required: ['city'] } },
|
|
473
|
+
required: ['address'],
|
|
474
|
+
properties: { test: { $ref: 'http://foo/test#' }, address: { $ref: '#address' } },
|
|
475
|
+
},
|
|
476
|
+
},
|
|
477
|
+
}, (req, reply) => reply.send({ ok: true }))
|
|
478
|
+
await refApp.ready()
|
|
479
|
+
const refGood = await refApp.inject({ method: 'POST', url: '/ref', payload: { test: { id: 1 }, address: { city: 'x' } } })
|
|
480
|
+
const refBadExt = await refApp.inject({ method: 'POST', url: '/ref', payload: { test: { id: 'no' }, address: { city: 'x' } } })
|
|
481
|
+
const refBadLocal = await refApp.inject({ method: 'POST', url: '/ref', payload: { test: { id: 1 }, address: {} } })
|
|
482
|
+
assert(refGood.statusCode === 200, 'cross-schema $ref: valid request accepted')
|
|
483
|
+
assert(refBadExt.statusCode === 400 && !/cannot resolve/.test(refBadExt.payload), 'cross-schema $ref: external $id ref enforced')
|
|
484
|
+
assert(refBadLocal.statusCode === 400 && !/cannot resolve/.test(refBadLocal.payload), 'cross-schema $ref: local #anchor enforced')
|
|
485
|
+
await refApp.close()
|
|
486
|
+
|
|
462
487
|
console.log(`\n${pass}/${pass + fail} tests passed\n`)
|
|
463
488
|
process.exit(fail > 0 ? 1 : 0)
|
|
464
489
|
}
|