Created
December 25, 2018 14:11
-
-
Save udomsak/5a50b3acc58142ddb7a3b94a9d22fef5 to your computer and use it in GitHub Desktop.
fastify-validation not work.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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