如何创建一个符合2020-12 JSON Schema元模式的方言?

huangapple go评论94阅读模式
英文:

How to create a dialect of the draft 2020-12 JSON Schema meta-schema?

问题

我想创建一个draft-2020-12元模式的方言。我希望这个方言禁止在“meta-data”元模式中使用任何属性(如标题、描述、默认值等)。换句话说,我希望从“main”元模式中删除“meta-data”词汇:

"$vocabulary": {
    ...
    "https://json-schema.org/draft/2020-12/vocab/meta-data": true
}

如何做到这一点?创建这个新的元模式方言需要哪些步骤?@Jason Desrosiers为创建draft04元模式方言提供了一份出色的步骤清单(https://stackoverflow.com/a/63797644/1248535),我猜想创建draft-2020-12元模式方言的步骤可能会有所不同。

英文:

I want to create a dialect of the draft-2020-12 meta-schema. I want the dialect to prohibit the use of any of the properties (title, description, default, etc.) in the "meta-data" meta-schema. In other words, I want the meta-data vocabulary deleted from the "main" meta-schema:

"$vocabulary": {
    ...
    "https://json-schema.org/draft/2020-12/vocab/meta-data": true

How to do that? What are the list of steps needed to create this new meta-schema dialect? @Jason Desrosiers wrote a terrific list of steps (https://stackoverflow.com/a/63797644/1248535) for creating a dialect of the draft04 meta-schema. I am guessing that the list of steps for creating a dialect of the draft-2020-12 meta-schema is quite different.

答案1

得分: 2

以下是翻译好的部分:

  1. 有一个在规范中的很好的示例 in the spec

  2. 从2019-09开始,我们将元模式分成了几个,每个用于不同的词汇。首先要做的是移除你不想要的部分。

  3. 从2020-12的基本元模式开始。

  4. 你不想要元数据词汇,所以要删除对它的引用。你还应该更改 $id,因为现在这是你的元模式,不是草案2020-12的元模式。

  5. 这删除了这些关键词的 定义,但不会禁止它们。2020-12草案允许包含未知关键词,因此任何人仍然可以添加 title。此外,由于我们已经删除了 title 的定义,他们可以添加任何他们想要的值。

  6. 要禁止这些关键词,你有几个选项。

  7. 仅禁止这些特定关键词

  8. 禁止所有未知关键词

  9. 仅禁止这些关键词的选项可能是最简单的。

  10. 它基本上表示这些属性的任何值都是无效的,实际上要求它们不存在。

  11. 对于这种方法,你可以在元模式中添加 unevaluatedProperties: false。这将禁止在 allOf 中列出的元模式中未定义的任何关键词。

英文:

There's actually a pretty good example in the spec.

Starting with 2019-09, we broke the meta-schema into several, one for each vocabulary. The first thing to do is remove the things you don't want.

Start with the 2020-12 base meta-schema.

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://json-schema.org/draft/2020-12/schema",
  "$vocabulary": {
    "https://json-schema.org/draft/2020-12/vocab/core": true,
    "https://json-schema.org/draft/2020-12/vocab/applicator": true,
    "https://json-schema.org/draft/2020-12/vocab/unevaluated": true,
    "https://json-schema.org/draft/2020-12/vocab/validation": true,
    "https://json-schema.org/draft/2020-12/vocab/meta-data": true,
    "https://json-schema.org/draft/2020-12/vocab/format-annotation": true,
    "https://json-schema.org/draft/2020-12/vocab/content": true
  },
  "$dynamicAnchor": "meta",

  "title": "Core and Validation specifications meta-schema",
  "allOf": [
    {"$ref": "meta/core"},
    {"$ref": "meta/applicator"},
    {"$ref": "meta/validation"},
    {"$ref": "meta/meta-data"},
    {"$ref": "meta/format-annotation"},
    {"$ref": "meta/content"}
  ],
  "type": ["object", "boolean"]
  // there's some other "deprecated" stuff in here that you likely don't need
}

You don't want the meta-data vocabulary, so remove references to that. You should also change the $id because this is your meta-schema now, not the draft 2020-12 meta-schema.

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://your.domain/meta-schema/or/something",
  "$vocabulary": {
    "https://json-schema.org/draft/2020-12/vocab/core": true,
    "https://json-schema.org/draft/2020-12/vocab/applicator": true,
    "https://json-schema.org/draft/2020-12/vocab/unevaluated": true,
    "https://json-schema.org/draft/2020-12/vocab/validation": true,
    "https://json-schema.org/draft/2020-12/vocab/format-annotation": true,
    "https://json-schema.org/draft/2020-12/vocab/content": true
  },
  "$dynamicAnchor": "meta",

  "title": "Core and Validation specifications meta-schema",
  "allOf": [
    {"$ref": "meta/core"},
    {"$ref": "meta/applicator"},
    {"$ref": "meta/validation"},
    {"$ref": "meta/format-annotation"},
    {"$ref": "meta/content"}
  ],
  "type": ["object", "boolean"]
  // there's some other "deprecated" stuff in here that you likely don't need
}

That removes the definition of those keywords, but it doesn't disallow them. Draft 2020-12 permits the inclusion of unknown keywords, so with this anyone could still just add title. What's more, since we've removed the definition for title, they can add it with whatever value they want:

{
  "title": true
}

This would be a valid schema according to your meta-schema.

To disallow those keywords, you have several options.

  • disallow only those specific keywords
  • disallow all unknown keywords

Disallow only those keywords

For this you have several options, but I think this is probably the simplest:

{
  // rest of meta-schema
  "properties": {
    "title": false,
    ...
  }
}

It basically says that no value for these properties is valid, effectively requiring that they're not present.

Disallow all unknown keywords

For this approach, you'll add unevaluatedProperties: false to the meta-schema. This will disallow any keyword not defined in one of the meta-schemas listed in the allOf.

{
  // rest of meta-schema
  "unevaluatedProperties": false
}

</details>



huangapple
  • 本文由 发表于 2023年8月5日 00:24:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76837690.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定