标签: json-schema-defaults

如何使用 JSON Schema 有条件地指定默认值

我有外地身份

如果用户将作业设置为草稿状态,我不想要求描述字段 - 但我确实希望有一个默认的空字符串。

如果用户正在发布作业,那么我希望需要描述。

我无法弄清楚如何在“oneOf -草案”数组中设置描述的默认值。

这是我的架构

{
  "schema": "http://json-schema.org/draft-04/schema#",
  "$id": "http://company.com/schemas/job-update.json#",
  "title": "Job",
  "description": "Update job",
  "type": "object",
  "properties": {
    "title": { 
      "type": "string",
      "minLength": 2
    },
    "description": { 
      "type": "string"
     // Can't set default here - as it will apply for the publish status.
    },    
    "status": { 
      "enum": ["draft", "published", "onhold"],
      "default": "draft"
    }
  },
  "oneOf": [
        {
          "description": "Draft jobs do not require any validation",
          "properties": {
            "status": { "enum": ["draft"]}
          },
          "required": …
Run Code Online (Sandbox Code Playgroud)

jsonschema json-schema-validator json-schema-defaults

8
推荐指数
1
解决办法
2万
查看次数

从外部 JSON 模式导入所有定义

我一直在尝试使用JSON 指针来引用和重用JSON 模式

按照这些示例,我可以引用另一个 JSON 模式中声明的特定属性,并且一切都按预期进行,但我还没有找到一种方法,可以使用另一个基本模式的定义来扩展基本 JSON 模式,而不必显式引用每一个财产。

看起来这会很有用,但我还没有发现有迹象表明它可能或不可能。

想象一下基本模式things

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "id": "http://example.com/thing.json",
    "type": "object",
    "additionalProperties": false,
    "properties": {
        "url": {
            "id": "url",
            "type": "string",
            "format": "uri"
        },
        "name": {
            "id": "name",
            "type": "string"
        }
    },
    "required": ["name"]
}
Run Code Online (Sandbox Code Playgroud)

如果我想要一个更具体的person模式来重用thing我可以这样做的两个属性:

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "id": "http://example.com/thing/person.json",
    "type": "object",
    "additionalProperties": false,
    "properties": {
        "url": {
            "$ref": "http://example.com/thing.json#/properties/url",
        },
        "name": {
            "$ref": "http://example.com/thing.json#/properties/name",
        },
        "gender": {
            "id": "gender",
            "type": "string",
            "enum": …
Run Code Online (Sandbox Code Playgroud)

jsonschema linked-data json-schema-validator json-schema-defaults

6
推荐指数
1
解决办法
9013
查看次数