Skip to content

Instantly share code, notes, and snippets.

@udomsak
Created December 25, 2018 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save udomsak/5a50b3acc58142ddb7a3b94a9d22fef5 to your computer and use it in GitHub Desktop.
Save udomsak/5a50b3acc58142ddb7a3b94a9d22fef5 to your computer and use it in GitHub Desktop.
fastify-validation not work.
// Require the framework and instantiate it
const fastify = require('fastify')({
logger: true
})
const post_schema = {
method: 'GET',
schema: {
body: {
type: 'object',
properties: {
public_key: { type: 'string' }
},
required: ['public_key']
}
}
}
// Declare a route
fastify.post('/',{ schema: post_schema, attachValidation: true }, function (request, reply) {
if (request.validation) {
// `req.validationError.validation` contains the raw validation error
reply.code(400).send(request.validationError)
}else{
reply.send({ hello: request.body })
}
})
fastify.get('/',{ schema: post_schema, attachValidation: true }, function (request, reply) {
reply.send(fastify.getSchemas())
})
// Run the server!
fastify.listen(3000, function (err, address) {
if (err) {
fastify.log.error(err)
process.exit(1)
}
fastify.log.info(`server listening on ${address}`)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment