fastify 和 ajv 模式验证
fastify and ajv schema validation
我正在尝试验证查询字符串参数 '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"}
很明显验证无效。我的代码中缺少什么?任何帮助将不胜感激。
这个模式结构解决了我的问题。以防万一有人想检查一下 运行 遇到类似问题。
const querySchema = {
schema: {
querystring: {
type: 'object',
properties: {
hccid: {
type: 'string'
}
},
required: ['hccid']
}
}
}
根据文档,您可以使用 querystring
或 query
来验证查询字符串,并使用 params
来验证路由参数。
参数为:
/api/garage/:id
其中 id
是可访问的参数 request.params.id
/api/garage/:plate
其中 plate
是可在 request.params.plate
处访问的参数
参数验证示例为:
const getItems = (req, reply) => {
const { plate } = req.params;
delete req.params.plate;
reply.send(plate);
};
const getItemsOpts = {
schema: {
description: "Get information about a particular vehicle present in garage.",
params: {
type: "object",
properties: {
plate: {
type: "string",
pattern: "^[A-Za-z0-9]{7}$",
},
},
},
response: {
200: {
type: "array",
},
},
},
handler: getItems,
};
fastify.get("/garage/:plate", getItemsOpts);
done();
查询/查询字符串为:
/api/garage/id?color=white&size=small
其中 color
和 size
是可在 request.query.color
或 request.query.size
处访问的两个查询字符串。
请参考上述答案作为查询验证的示例。
Validation
The route validation internally relies upon Ajv v6 which is a
high-performance JSON Schema validator. Validating the input is very
easy: just add the fields that you need inside the route schema, and
you are done!
The supported validations are:
body
: validates the body of the request if it is a POST, PUT, or PATCH method.
querystring
or query
: validates the query string.
params
: validates the route params.
headers
: validates the request headers.
[1] Fastify 验证:https://www.fastify.io/docs/latest/Validation-and-Serialization/#validation
[2] Ajv@v6: https://www.npmjs.com/package/ajv/v/6.12.6
[3] Fastify 请求:https://www.fastify.io/docs/latest/Request/
我正在尝试验证查询字符串参数 '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"}
很明显验证无效。我的代码中缺少什么?任何帮助将不胜感激。
这个模式结构解决了我的问题。以防万一有人想检查一下 运行 遇到类似问题。
const querySchema = {
schema: {
querystring: {
type: 'object',
properties: {
hccid: {
type: 'string'
}
},
required: ['hccid']
}
}
}
根据文档,您可以使用 querystring
或 query
来验证查询字符串,并使用 params
来验证路由参数。
参数为:
/api/garage/:id
其中id
是可访问的参数request.params.id
/api/garage/:plate
其中plate
是可在request.params.plate
处访问的参数
参数验证示例为:
const getItems = (req, reply) => {
const { plate } = req.params;
delete req.params.plate;
reply.send(plate);
};
const getItemsOpts = {
schema: {
description: "Get information about a particular vehicle present in garage.",
params: {
type: "object",
properties: {
plate: {
type: "string",
pattern: "^[A-Za-z0-9]{7}$",
},
},
},
response: {
200: {
type: "array",
},
},
},
handler: getItems,
};
fastify.get("/garage/:plate", getItemsOpts);
done();
查询/查询字符串为:
/api/garage/id?color=white&size=small
其中color
和size
是可在request.query.color
或request.query.size
处访问的两个查询字符串。
请参考上述答案作为查询验证的示例。
Validation
The route validation internally relies upon Ajv v6 which is a high-performance JSON Schema validator. Validating the input is very easy: just add the fields that you need inside the route schema, and you are done!
The supported validations are:
body
: validates the body of the request if it is a POST, PUT, or PATCH method.querystring
orquery
: validates the query string.params
: validates the route params.headers
: validates the request headers.
[1] Fastify 验证:https://www.fastify.io/docs/latest/Validation-and-Serialization/#validation
[2] Ajv@v6: https://www.npmjs.com/package/ajv/v/6.12.6
[3] Fastify 请求:https://www.fastify.io/docs/latest/Request/