如何从查询中删除非SCHEMA参数
根据 fastify 4.0 docs ,默认选项,用于ajv包括removeAdcitional:true
。我有一个querystring
架构,当我发送带有架构中的param的查询时,我希望该参数被删除。但是没有这样的事情发生。模式本身是我的一些访问量的相当普通
querystring: {
type: "object",
properties: { … }
}
的类型数组,我也想删除任何其他值(通过enum
指定的值之外。如何 了吗
我实现
{
'$schema': 'https://json-schema.org/draft/2020-12/schema',
'$id': 'https://example.com/treatments.schema.json',
title: 'Treatments',
summary: 'Fetches treatments',
description: 'Treatments are…',
response: {},
querystring: {
type: 'object',
additionalProperties: false,
properties: {
treatmentId: {
type: "string",
maxLength: 32,
minLength: 32,
description: "A 32 character string: treatmentId=388D179E0D564775C3925A5B93C1C407"
},
… <snipped> …
}
},
tags: [ 'zenodeo' ]
}
? http:// localhost:3010/v3/verresments?presenthing = foo
给出一个错误{“ statuscode”:400,“错误”:“糟糕请求”,“不良请求”,“消息”:“ queryString:“ /presenthID不得少于32个字符“}
(那很好
according to the fastify 4.0 docs, the default options for Ajv include removeAdditional: true
. I have a queryString
schema, and when I send a query with a param that is not in the schema, I expect that param to be removed. But no such thing happens. The schema itself is pretty run-of-the-mill
querystring: {
type: "object",
properties: { … }
}
Some of my params are of type array, and there too I would like any additional values (outside of those specified via enum
to be removed. How can I achieve this?
update: I am adding the actual schema below (with some info snipped for brevity)
{
'$schema': 'https://json-schema.org/draft/2020-12/schema',
'$id': 'https://example.com/treatments.schema.json',
title: 'Treatments',
summary: 'Fetches treatments',
description: 'Treatments are…',
response: {},
querystring: {
type: 'object',
additionalProperties: false,
properties: {
treatmentId: {
type: "string",
maxLength: 32,
minLength: 32,
description: "A 32 character string: treatmentId=388D179E0D564775C3925A5B93C1C407"
},
… <snipped> …
}
},
tags: [ 'zenodeo' ]
}
I know the schema is being validated because http://localhost:3010/v3/treatments?treatmentId=foo
gives an error {"statusCode":400,"error":"Bad Request","message":"querystring/treatmentId must NOT have fewer than 32 characters"}
(that is good ????????) but the additionalProperties: false
constraint is still not working because http://localhost:3010/v3/treatments?foo=bar
gives no error and foo
is also not removed from request.query
(that is not good ????????)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
removeAdtitional:true
您仍然必须设置其他properties:false
在您的架构上:请参阅 https://ajv.js.org/json-schema.html#additionalproperties
您还可以将默认行为更改为
removeAdtitional:'ash hast''
在这种情况下, t必须在模式上设置其他Properties:false
。请参阅此处的页面中的大约一半: https://wwww.fastify .io/doc/最新/参考/验证和序列化/
With
removeAdditional: true
you must still setadditionalProperties: false
on your schema:See https://ajv.js.org/json-schema.html#additionalproperties
You can also change the default behavior to
removeAdditional: 'all'
in which case you don't have to setadditionalProperties: false
on your schema.See about halfway down the page here: https://www.fastify.io/docs/latest/Reference/Validation-and-Serialization/