Skip to content
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

add unit test for ajv-formats #4187

Merged
merged 1 commit into from
Aug 10, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
54 changes: 54 additions & 0 deletions test/schema-examples.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,60 @@ test('should return custom error messages with ajv-errors', t => {
})
})

test('should be able to handle formats of ajv-formats when added by plugins option', t => {
t.plan(3)

const fastify = Fastify({
ajv: {
plugins: [
require('ajv-formats')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is actually added by @fastify/ajv-compiler.

The only exception is when you apply your own ajv-formats, it will override the ajv-formats in @fastify/ajv-compiler

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had no idea ajv-formats was included with Fastify via @fastify/ajv-compiler — that's great! I don't see it mentioned in the Validation documentation, but I'd be happy to open a PR that adds this in.

As far as I can tell, ajv-formats wasn't included with Fastify v3 — is that correct?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can tell, ajv-formats wasn't included with Fastify v3 — is that correct?

No, ajv-formats is embedded inside ajv@6.
It is extracted in ajv@7 and we need to explicitly add it through ajv-formats.

See More: https://github.com/ajv-validator/ajv/releases/tag/v7.0.0

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh that's right, I forgot about that. And Fastify went from Ajv v6 (formats embedded) -> v8 (formats in ajv-formats)?

]
}
})

const schema = {
body: {
type: 'object',
properties: {
id: { type: 'string', format: 'uuid' },
email: { type: 'string', format: 'email' }
},
required: ['id', 'email']
}
}

fastify.post('/', { schema }, function (req, reply) {
reply.code(200).send(req.body.id)
})

fastify.inject({
method: 'POST',
payload: {
id: '254381a5-888c-4b41-8116-e3b1a54980bd',
email: 'info@fastify.io'
},
url: '/'
}, (_err, res) => {
t.equal(res.body, '254381a5-888c-4b41-8116-e3b1a54980bd')
t.equal(res.statusCode, 200)
})

fastify.inject({
method: 'POST',
payload: {
id: 'invalid',
email: 'info@fastify.io'
},
url: '/'
}, (_err, res) => {
t.same(JSON.parse(res.payload), {
statusCode: 400,
error: 'Bad Request',
message: 'body/id must match format "uuid"'
})
})
})

test('should return localized error messages with ajv-i18n', t => {
t.plan(3)

Expand Down