我正在尝试验证查询字符串参数“hccid”,如下所示。似乎验证对我不起作用。有人可以看到我缺少什么吗?
const fastify = require('fastify')({
ajv: {
removeAdditional: true,
useDefaults: true,
coerceTypes: true
}
});
const schema = {
querystring: {
hccid: { type: 'string' }
}
};
// Declare a route
fastify.get('/hello', {schema}, function (request, reply) {
const hccid = request.query.hccid;
reply.send({ hello: 'world' })
});
// Run the server!
fastify.listen(3000, function (err) {
if (err) throw err
console.log(`server listening on ${fastify.server.address().port}`)
});
因此,使用该代码,当我使用全新的查询参数调用服务时,我应该得到一个模式验证异常,abc
如下所示
http://localhost:3000/hello?abc=1
但没有错误。我得到了回复{"hello":"world"}
我还尝试一起删除查询参数http://localhost:3000/hello
我仍然得到{"hello":"world"}
所以显然验证不起作用。我的代码中缺少什么?任何帮助,将不胜感激。