JSON-SCHEMA:获取额外的Properties列表?

发布于 2025-01-20 13:51:50 字数 715 浏览 4 评论 0原文

是否可以获取 json-schema 找到的所有 additionalProperties 的列表?

例如,如果我的架构如下所示:

{
  "type": "object",
  "properties": {
    "firstName": {
      "type": "string",
    },
    "lastName": {
      "type": "string",
    },
    "age": {
      "type": "integer"
    }
  }
}

并且数据如下所示:

{
  "firstName": "John",
  "lastName": "Doe",
  "age": 21,
  "extraField": "some new data I was not expecting",
  "anotherExtraField": "another unexpected data point"
}

在这种情况下,我想要一个列表作为返回,而不是由于 additionalProperties: false 而导致 json-schema 异常: [额外字段,另一个额外字段]

Is it possible to get a list of all additionalProperties found by json-schema ?

For example, if my schema looks like this :

{
  "type": "object",
  "properties": {
    "firstName": {
      "type": "string",
    },
    "lastName": {
      "type": "string",
    },
    "age": {
      "type": "integer"
    }
  }
}

And data loooks like this :

{
  "firstName": "John",
  "lastName": "Doe",
  "age": 21,
  "extraField": "some new data I was not expecting",
  "anotherExtraField": "another unexpected data point"
}

In this case, instead of an exception from json-schema because of additionalProperties: false, I want a list in return, like : [extraField, anotherExtraField]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

誰認得朕 2025-01-27 13:51:50

如果您使用的是支持带有注释的2019-09或2020-12的实现,那么您很幸运! 附加Properties应产生其验证属性的注释结果( spec )。

如果添加附加Properties:true,则所有额外的属性通过并通过关键字进行验证,这意味着应在注释结果中列出这些额外的属性。

{
  "type": "object",
  "properties": {
    "firstName": {
      "type": "string"
    },
    "lastName": {
      "type": "string"
    },
    "age": {
      "type": "integer"
    }
  },
  "additionalProperties": true
}

这会产生(在详细输出格式中)

{
  "valid": true,
  "keywordLocation": "#",
  "instanceLocation": "#",
  "annotations": [
    {
      "valid": true,
      "keywordLocation": "#/properties",
      "instanceLocation": "#",
      "annotation": [
        "firstName",
        "lastName",
        "age"
      ]
    },
    {
      "valid": true,
      "keywordLocation": "#/additionalProperties",
      "instanceLocation": "#",
      "annotation": [
        "extraField",
        "anotherExtraField"
      ]
    }
  ]
}

您可以在

如果您不使用.NET,则可以浏览实现页面其他库。其中一些也可能支持注释,但我不确定哪个是。

If you're using an implementation that supports 2019-09 or 2020-12 with annotations, you're in luck! additionalProperties should produce an annotation result of the properties it validates (spec).

If you add additionalProperties: true, then all extra properties pass and are validated by the keyword, which means those extra properties should be listed in the annotation result.

{
  "type": "object",
  "properties": {
    "firstName": {
      "type": "string"
    },
    "lastName": {
      "type": "string"
    },
    "age": {
      "type": "integer"
    }
  },
  "additionalProperties": true
}

This yields (in the Detailed output format)

{
  "valid": true,
  "keywordLocation": "#",
  "instanceLocation": "#",
  "annotations": [
    {
      "valid": true,
      "keywordLocation": "#/properties",
      "instanceLocation": "#",
      "annotation": [
        "firstName",
        "lastName",
        "age"
      ]
    },
    {
      "valid": true,
      "keywordLocation": "#/additionalProperties",
      "instanceLocation": "#",
      "annotation": [
        "extraField",
        "anotherExtraField"
      ]
    }
  ]
}

You can try it on https://json-everything.net, which is powered by my validator, JsonSchema.Net.

If you're not using .Net, you can browse the implementations page for other libraries. Some of them may also support annotations, but I'm not sure which do.

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