如何创建 2020-12 草案 JSON Schema 元架构的方言?

问题描述 投票:0回答:1

我想创建 2020-12 草案元模式的方言。我希望方言禁止使用“元数据”元模式中的任何属性(标题、描述、默认值等)。换句话说,我希望从“主”元模式中删除元数据词汇:

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

如何做到这一点?创建这种新的元模式方言需要哪些步骤? @Jason Desrosiers 写了一个很棒的步骤列表(https://stackoverflow.com/a/63797644/1248535),用于创建 Draft04 元模式的方言。我猜测创建 2020-12 草案元模式方言的步骤列表是完全不同的。

json jsonschema json-schema-validator
1个回答
0
投票

实际上有一个很好的例子在规范中

从 2019-09 开始,我们将元模式分成几个,每个词汇一个。首先要做的就是删除你不想要的东西。

从 2020-12 基本元架构开始。

{
  "$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
}

您不需要元数据词汇表,因此请删除对其的引用。您还应该更改

$id
,因为这是您现在的元架构,而不是 2020-12 草案元架构。

{
  "$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
}

这会删除这些关键字的定义,但并不禁止它们。 2020-12 草案允许包含未知关键字,因此任何人仍然可以添加

title
。此外,由于我们删除了
title
的定义,他们可以添加任何他们想要的值:

{
  "title": true
}

根据您的元架构,这将是一个有效的架构。

要禁止这些关键字,您有多种选择。

  • 仅禁止那些特定的关键字
  • 禁止所有未知关键字

仅禁止这些关键字

为此,您有多种选择,但我认为这可能是最简单的:

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

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

禁止所有未知关键字

对于这种方法,您需要将

unevaluatedProperties: false
添加到元架构中。这将禁止
allOf
中列出的元模式之一中未定义的任何关键字。

{
  // rest of meta-schema
  "unevaluatedProperties": false
}
© www.soinside.com 2019 - 2024. All rights reserved.