jsonschema - 如何使用 JSON 模式验证器防止某些字段基于另一个字段值

标签 jsonschema json-schema-validator

根据用户选择的salaryRange,我需要通过要求某些字段并拒绝其他字段来进行不同的验证。我觉得它是 allOf 和 not 的组合,但我似乎不太明白。

场景#1

用户选择工资范围(每小时)

  • 需要每小时费率

  • 阻止提交字段 FeeOne 和 FeeTwo

场景#2

用户选择工资范围(0-50k 或 50-100k)

  • 需要 FeeOne 和 FeeTwo

  • 阻止提交字段hourlyRate

这是我的架构

{
  "schema": "http://json-schema.org/draft-04/schema#",
  "$id": "http://mysite/schemas/job.json#",
  "title": "Job",
  "description": "Create job",
  "type": "object",
  "properties": {
    "title": { "type": "string" },
    "description": { "type": "string" },
    "salaryRange": { "enum": ["0-50k", "50-100k", "100-150k", "150-200k", "200-300k", "300k+", "nonExempt", "Hourly"] },  
    "hourlyRate": { 
      "type": "number",
      "minimum": 0,
      "maximum": 300 
    },
    "feeOne": { 
      "type": "number", 
      "minimum": 0 
    },
    "feeTwo": { 
      "type": "number", 
      "minimum": 0 
    }
  }    ,
  "additionalProperties": false,  
  "required": [
    "title", 
    "description", 
    "salaryRange"
  ]
}

最佳答案

您可以使用 oneOfnot required 对所有可能的组合进行建模。

这是一个js中的例子: https://runkit.com/embed/cf8cra1mwvx3/

{
  "schema": "http://json-schema.org/draft-04/schema#",
  "$id": "http://mysite/schemas/job.json#",
  "title": "Job",
  "description": "Create job",
  "type": "object",
  "properties": {
    "title": { "type": "string" },
    "description": { "type": "string" },
    "salaryRange": { "enum": ["0-50k", "50-100k", "100-150k", "150-200k", "200-300k", "300k+", "nonExempt", "Hourly"] },  
    "hourlyRate": { 
      "type": "number",
      "minimum": 0,
      "maximum": 300 
    },
    "feeOne": { 
      "type": "number", 
      "minimum": 0 
    },
    "feeTwo": { 
      "type": "number", 
      "minimum": 0 
    }
  },
  "oneOf": [
    {
        "description": "Disallow fees for hourly salary",
        "properties": {
            "salaryRange": { "enum": ["Hourly"] }
        },
        "required": ["hourlyRate"],
        "allOf": [
            {"not":{"required":["feeOne"]}},
            {"not":{"required":["feeTwo"]}}
        ]
    },
    {
        "description": "Disallow hourly rate for 0-50k, 50-100k salaries",
        "properties": {
            "salaryRange": { "enum": ["0-50k", "50-100k"] }
        },
        "required": ["feeOne", "feeTwo"],
        "not":{"required":["hourlyRate"]}
    },
    {
        "description": "Allow other cases",
         "properties": {
            "salaryRange": { "not" : {"enum": ["Hourly", "0-50k", "50-100k"] } }
        }
    }
  ],
  "additionalProperties": false,  
  "required": [
    "title", 
    "description", 
    "salaryRange"
  ]
}

关于jsonschema - 如何使用 JSON 模式验证器防止某些字段基于另一个字段值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52727498/

相关文章:

json - 如何定义至少需要许多属性之一的 JSON 模式

arrays - 如何使用graph api在azure b2c中创建多个用户?

xsd - JSON 模式验证器 'id' 字段

node.js - aws lambda 上的 json 模式验证

值为对象数组的对象的 JSON 模式

jsonschema:对象数组中的唯一属性

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

java - 如何验证 REST 服务中的传入 JSON 数据?

JSON Schema 对象包含值

java - 如何从 java 加载 JSON 模式文件