Skip to content

Commit f59f086

Browse files
authored
test: addres v5 support (#56)
1 parent 1c1dafd commit f59f086

File tree

2 files changed

+123
-71
lines changed

2 files changed

+123
-71
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"license": "MIT",
3636
"devDependencies": {
3737
"@types/node": "^22.0.0",
38-
"fastify": "^4.6.0",
38+
"fastify": "^5.2.1",
3939
"husky": "^9.0.11",
4040
"proxyquire": "^2.1.3",
4141
"snazzy": "^9.0.0",

test/index.test.js

Lines changed: 122 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -10,54 +10,61 @@ const test = tap.test
1010

1111
tap.plan(15)
1212

13-
test('Should allow custom AJV instance for querystring', async t => {
14-
t.plan(1)
15-
const customAjv = new AJV({ coerceTypes: false })
16-
const server = Fastify()
13+
test(
14+
'Should allow custom AJV instance for querystring',
15+
{ only: true },
16+
async t => {
17+
t.plan(1)
18+
const customAjv = new AJV({ coerceTypes: false })
19+
const server = Fastify()
1720

18-
server.register(plugin, {})
21+
server.register(plugin, {})
1922

20-
server.get(
21-
'/',
22-
{
23-
schema: {
24-
querystring: {
25-
msg: {
26-
type: 'array',
27-
items: {
28-
type: 'string'
23+
server.get(
24+
'/',
25+
{
26+
schema: {
27+
querystring: {
28+
type: 'object',
29+
properties: {
30+
msg: {
31+
type: 'array',
32+
items: {
33+
type: 'string'
34+
}
35+
}
2936
}
3037
}
38+
},
39+
config: {
40+
schemaValidators: {
41+
// TODO: normalize to query
42+
querystring: customAjv
43+
}
3144
}
3245
},
33-
config: {
34-
schemaValidators: {
35-
// TODO: normalize to query
36-
querystring: customAjv
37-
}
38-
}
39-
},
40-
(req, reply) => {}
41-
)
46+
(req, reply) => {}
47+
)
4248

43-
try {
44-
const res = await server.inject({
45-
method: 'GET',
46-
url: '/',
47-
query: {
48-
msg: ['hello world']
49-
}
50-
})
49+
try {
50+
const res = await server.inject({
51+
method: 'GET',
52+
url: '/',
53+
query: {
54+
msg: ['hello world']
55+
}
56+
})
5157

52-
t.equal(
53-
res.statusCode,
54-
400,
55-
'Should coerce the single element array into string'
56-
)
57-
} catch (err) {
58-
t.error(err)
58+
t.equal(
59+
res.statusCode,
60+
400,
61+
'Should coerce the single element array into string'
62+
)
63+
} catch (err) {
64+
t.error(err)
65+
}
5966
}
60-
})
67+
)
6168

6269
test('Should allow custom AJV instance for body', async t => {
6370
t.plan(2)
@@ -125,8 +132,11 @@ test('Should allow custom AJV instance for params', async t => {
125132
{
126133
schema: {
127134
params: {
128-
msg: {
129-
type: 'integer'
135+
type: 'object',
136+
properties: {
137+
msg: {
138+
type: 'integer'
139+
}
130140
}
131141
}
132142
},
@@ -170,8 +180,11 @@ test('Should allow custom AJV instance for headers', async t => {
170180
{
171181
schema: {
172182
headers: {
173-
'x-type': {
174-
type: 'integer'
183+
type: 'object',
184+
properties: {
185+
'x-type': {
186+
type: 'integer'
187+
}
175188
}
176189
}
177190
},
@@ -234,8 +247,11 @@ test('Should work with referenced schemas (querystring)', async t => {
234247
{
235248
schema: {
236249
query: {
237-
msg: {
238-
$ref: 'some#'
250+
type: 'object',
251+
properties: {
252+
msg: {
253+
$ref: 'some#'
254+
}
239255
}
240256
}
241257
},
@@ -289,8 +305,11 @@ test('Should work with referenced schemas (params)', async t => {
289305
{
290306
schema: {
291307
params: {
292-
id: {
293-
$ref: 'some#'
308+
type: 'object',
309+
properties: {
310+
id: {
311+
$ref: 'some#'
312+
}
294313
}
295314
}
296315
},
@@ -337,8 +356,11 @@ test('Should work with referenced schemas (headers)', async t => {
337356
{
338357
schema: {
339358
headers: {
340-
'x-id': {
341-
$ref: 'some#'
359+
type: 'object',
360+
properties: {
361+
'x-id': {
362+
$ref: 'some#'
363+
}
342364
}
343365
}
344366
},
@@ -577,13 +599,19 @@ test('Should work with parent nested schemas', async t => {
577599
{
578600
schema: {
579601
querystring: {
580-
msg: {
581-
$ref: 'some#'
602+
type: 'object',
603+
properties: {
604+
msg: {
605+
$ref: 'some#'
606+
}
582607
}
583608
},
584609
headers: {
585-
'x-another': {
586-
$ref: 'another#'
610+
type: 'object',
611+
properties: {
612+
'x-another': {
613+
$ref: 'another#'
614+
}
587615
}
588616
}
589617
},
@@ -659,13 +687,19 @@ test('Should handle parsing to querystring (query)', async t => {
659687
{
660688
schema: {
661689
query: {
662-
msg: {
663-
$ref: 'some#'
690+
type: 'object',
691+
properties: {
692+
msg: {
693+
$ref: 'some#'
694+
}
664695
}
665696
},
666697
headers: {
667-
'x-another': {
668-
$ref: 'another#'
698+
type: 'object',
699+
properties: {
700+
'x-another': {
701+
$ref: 'another#'
702+
}
669703
}
670704
}
671705
},
@@ -756,13 +790,19 @@ test('Should use default plugin validator as fallback', async t => {
756790
{
757791
schema: {
758792
query: {
759-
msg: {
760-
$ref: 'some#'
793+
type: 'object',
794+
properties: {
795+
msg: {
796+
$ref: 'some#'
797+
}
761798
}
762799
},
763800
headers: {
764-
'x-another': {
765-
$ref: 'another#'
801+
type: 'object',
802+
properties: {
803+
'x-another': {
804+
$ref: 'another#'
805+
}
766806
}
767807
}
768808
}
@@ -830,7 +870,7 @@ test('Should always cache schema to default plugin validator', async t => {
830870
}
831871
})
832872

833-
server.register(async (instance, opts, done) => {
873+
server.register(async (instance, opts) => {
834874
instance.addSchema({
835875
$id: 'another',
836876
type: 'integer'
@@ -843,13 +883,19 @@ test('Should always cache schema to default plugin validator', async t => {
843883
{
844884
schema: {
845885
query: {
846-
msg: {
847-
$ref: 'some#'
886+
type: 'object',
887+
properties: {
888+
msg: {
889+
$ref: 'some#'
890+
}
848891
}
849892
},
850893
headers: {
851-
'x-another': {
852-
$ref: 'another#'
894+
type: 'object',
895+
properties: {
896+
'x-another': {
897+
$ref: 'another#'
898+
}
853899
}
854900
}
855901
},
@@ -922,13 +968,19 @@ test('Should use default provided validator as fallback', async t => {
922968
{
923969
schema: {
924970
query: {
925-
msg: {
926-
$ref: 'some#'
971+
type: 'object',
972+
properties: {
973+
msg: {
974+
$ref: 'some#'
975+
}
927976
}
928977
},
929978
headers: {
930-
'x-another': {
931-
$ref: 'another#'
979+
type: 'object',
980+
properties: {
981+
'x-another': {
982+
$ref: 'another#'
983+
}
932984
}
933985
}
934986
}

0 commit comments

Comments
 (0)