Skip to content

Commit

Permalink
add unit test for ajv-formats (fastify#4187)
Browse files Browse the repository at this point in the history
  • Loading branch information
Uzlopak committed Aug 10, 2022
1 parent d0f8303 commit ea8aae9
Showing 1 changed file with 54 additions and 0 deletions.
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')
]
}
})

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

0 comments on commit ea8aae9

Please sign in to comment.