如何从查询中删除非SCHEMA参数

发布于 2025-02-13 07:13:27 字数 1299 浏览 0 评论 0原文

根据 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

染火枫林 2025-02-20 07:13:27

使用removeAdtitional:true您仍然必须设置其他properties:false在您的架构上:

querystring: {
  type: "object",
  properties: { … },
  additionalProperties: 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 set additionalProperties: false on your schema:

querystring: {
  type: "object",
  properties: { … },
  additionalProperties: false
}

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 set additionalProperties: false on your schema.

See about halfway down the page here: https://www.fastify.io/docs/latest/Reference/Validation-and-Serialization/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文