Skip to content

test: addres v5 support #56

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"license": "MIT",
"devDependencies": {
"@types/node": "^22.0.0",
"fastify": "^4.6.0",
"fastify": "^5.2.1",
"husky": "^9.0.11",
"proxyquire": "^2.1.3",
"snazzy": "^9.0.0",
Expand Down
192 changes: 122 additions & 70 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,54 +10,61 @@ const test = tap.test

tap.plan(15)

test('Should allow custom AJV instance for querystring', async t => {
t.plan(1)
const customAjv = new AJV({ coerceTypes: false })
const server = Fastify()
test(
'Should allow custom AJV instance for querystring',
{ only: true },
async t => {
t.plan(1)
const customAjv = new AJV({ coerceTypes: false })
const server = Fastify()

server.register(plugin, {})
server.register(plugin, {})

server.get(
'/',
{
schema: {
querystring: {
msg: {
type: 'array',
items: {
type: 'string'
server.get(
'/',
{
schema: {
querystring: {
type: 'object',
properties: {
msg: {
type: 'array',
items: {
type: 'string'
}
}
}
}
},
config: {
schemaValidators: {
// TODO: normalize to query
querystring: customAjv
}
}
},
config: {
schemaValidators: {
// TODO: normalize to query
querystring: customAjv
}
}
},
(req, reply) => {}
)
(req, reply) => {}
)

try {
const res = await server.inject({
method: 'GET',
url: '/',
query: {
msg: ['hello world']
}
})
try {
const res = await server.inject({
method: 'GET',
url: '/',
query: {
msg: ['hello world']
}
})

t.equal(
res.statusCode,
400,
'Should coerce the single element array into string'
)
} catch (err) {
t.error(err)
t.equal(
res.statusCode,
400,
'Should coerce the single element array into string'
)
} catch (err) {
t.error(err)
}
}
})
)

test('Should allow custom AJV instance for body', async t => {
t.plan(2)
Expand Down Expand Up @@ -125,8 +132,11 @@ test('Should allow custom AJV instance for params', async t => {
{
schema: {
params: {
msg: {
type: 'integer'
type: 'object',
properties: {
msg: {
type: 'integer'
}
}
}
},
Expand Down Expand Up @@ -170,8 +180,11 @@ test('Should allow custom AJV instance for headers', async t => {
{
schema: {
headers: {
'x-type': {
type: 'integer'
type: 'object',
properties: {
'x-type': {
type: 'integer'
}
}
}
},
Expand Down Expand Up @@ -234,8 +247,11 @@ test('Should work with referenced schemas (querystring)', async t => {
{
schema: {
query: {
msg: {
$ref: 'some#'
type: 'object',
properties: {
msg: {
$ref: 'some#'
}
}
}
},
Expand Down Expand Up @@ -289,8 +305,11 @@ test('Should work with referenced schemas (params)', async t => {
{
schema: {
params: {
id: {
$ref: 'some#'
type: 'object',
properties: {
id: {
$ref: 'some#'
}
}
}
},
Expand Down Expand Up @@ -337,8 +356,11 @@ test('Should work with referenced schemas (headers)', async t => {
{
schema: {
headers: {
'x-id': {
$ref: 'some#'
type: 'object',
properties: {
'x-id': {
$ref: 'some#'
}
}
}
},
Expand Down Expand Up @@ -577,13 +599,19 @@ test('Should work with parent nested schemas', async t => {
{
schema: {
querystring: {
msg: {
$ref: 'some#'
type: 'object',
properties: {
msg: {
$ref: 'some#'
}
}
},
headers: {
'x-another': {
$ref: 'another#'
type: 'object',
properties: {
'x-another': {
$ref: 'another#'
}
}
}
},
Expand Down Expand Up @@ -659,13 +687,19 @@ test('Should handle parsing to querystring (query)', async t => {
{
schema: {
query: {
msg: {
$ref: 'some#'
type: 'object',
properties: {
msg: {
$ref: 'some#'
}
}
},
headers: {
'x-another': {
$ref: 'another#'
type: 'object',
properties: {
'x-another': {
$ref: 'another#'
}
}
}
},
Expand Down Expand Up @@ -756,13 +790,19 @@ test('Should use default plugin validator as fallback', async t => {
{
schema: {
query: {
msg: {
$ref: 'some#'
type: 'object',
properties: {
msg: {
$ref: 'some#'
}
}
},
headers: {
'x-another': {
$ref: 'another#'
type: 'object',
properties: {
'x-another': {
$ref: 'another#'
}
}
}
}
Expand Down Expand Up @@ -830,7 +870,7 @@ test('Should always cache schema to default plugin validator', async t => {
}
})

server.register(async (instance, opts, done) => {
server.register(async (instance, opts) => {
instance.addSchema({
$id: 'another',
type: 'integer'
Expand All @@ -843,13 +883,19 @@ test('Should always cache schema to default plugin validator', async t => {
{
schema: {
query: {
msg: {
$ref: 'some#'
type: 'object',
properties: {
msg: {
$ref: 'some#'
}
}
},
headers: {
'x-another': {
$ref: 'another#'
type: 'object',
properties: {
'x-another': {
$ref: 'another#'
}
}
}
},
Expand Down Expand Up @@ -922,13 +968,19 @@ test('Should use default provided validator as fallback', async t => {
{
schema: {
query: {
msg: {
$ref: 'some#'
type: 'object',
properties: {
msg: {
$ref: 'some#'
}
}
},
headers: {
'x-another': {
$ref: 'another#'
type: 'object',
properties: {
'x-another': {
$ref: 'another#'
}
}
}
}
Expand Down
Loading