JSON 模式中的有效字符串序列

Valid string sequences in JSON Schema

谁能建议如何编写 JSON 模式文档来描述可以是三种可能序列之一的字符串?假设字符串“fruit”只能是以下几种:“apple”、“bananna”或“coconut”。

我认为可以使用正则表达式,但不确定如何在 JSON 架构中指示正则表达式约束。

https://json-schema.org/draft/2020-12/json-schema-core.html#rfc.section.6.1

这是我目前的情况:

{   
  "$id": "https://example.com/person.schema.json",   
  "$schema": "https://json-schema.org/draft/2020-12/schema",   
  "title": "TestSession",   
  "type": "object",   
  "properties": {
    "fruit": {
      "type": "string",
      "description": "only three legal possibilities: apple, banana, or coconut"
    }
}

您需要为此使用 enum 关键字。

The value of this keyword MUST be an array. This array SHOULD have
at least one element. Elements in the array SHOULD be unique.

An instance validates successfully against this keyword if its value is equal to one of the elements in this keyword's array value.

Elements in the array might be of any type, including null.

https://datatracker.ietf.org/doc/html/draft-bhutton-json-schema-validation-00#section-6.1.2

例如"enum": [ "apple", "bananna", "coconut" ].