首页 > 解决方案 > json 模式验证:对值的条件依赖

问题描述

我有以下 json 文件:

{
"REFERENCE": "genome", 
"END": "pair", 
"INPUT1": "in1.fq",
"INPUT2": "in2.fq",
"INPUT": "in.fq",
"GENOME": "genome_file.fa",
"ANNOTATION": "annotation_file.gff",
"TRANS" : "trans_file.txt"
}

条件依赖如下:

这是架构,但它不起作用(我尝试过 allOf、anyOf 但它不起作用)。没有设法使用如果,那么,否则。

{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "title",
"description": "description",
"type": "object",
"properties": {
  "END": {
    "type": "string",
    "enum": [
          "pair",
          "single",
        ]
  },
  "REFERENCE": {
    "type": "string",
    "enum": [
          "genome",
          "transcriptome",
        ],
    
  },
  "GENOME": {
    "type": "string"
  },
  "ANNOTATION": {
    "type": "string"
  },
  "TRANS": {
    "type": "string"
  },
  "INPUT1": {
    "type": "string"
  },
  "INPUT2": {
    "type": "string"
  },
  "INPUT": {
    "type": "string"
  }
},

"allOf": [
  {
        "properties": {
            "REFERENCE": {
                "const": "genome"
            }
        },
        "required": ["GENOME", "ANNOTATION"]
    },
    {
        "properties": {
            "REFERENCE": {
                "const": "transcriptome"
            }
        },
        "required": ["TRANS"]
    },
    {
        "properties": {
            "END": {
                "const": "pair"
            }
        },
        "required": ["INPUT1", "INPUT2"]
    },
    {
        "properties": {
            "END": {
                "const": "single"
            }
        },
        "required": ["INPUT"]
    }
]

}

标签: jsonjsonschema

解决方案


推荐阅读