javascript - 如何使用 AJV 根据输入值验证数据?

标签 javascript jsonschema ajv

我正在使用 AJV 尝试验证一些数据并根据另一个属性的值动态地要求属性。

我想要验证的是: - 始终需要enabled, - 如果 enabled = true 则仅使用其他属性之一(realtimethresholddigest) 必须提供,

示例有效负载和预期结果:

应该通过

{
  "notifications": {
    "enabled": false
  }
}

应该通过

{
  "notifications": {
    "enabled": true,
    "realtime": true
  }
}

应该通过

{
  "notifications": {
    "enabled": true,
    "digest": true
  }
}

应该通过

{
  "notifications": {
    "enabled": true,
    "threshold": {}
  }
}

应该会失败,因为 enabled = true 但未设置其他属性。

{
  "notifications": {
    "enabled": true
  }
}

应该失败,因为 enabled = true 并且设置了多个其他属性。

{
  "notifications": {
    "enabled": true,
    "threshold": {},
    "digest: true
  }
}

这是我正在使用的验证架构:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "Settings",
  "type": "object",
  "properties": {
    "notifications": {
      "type": "object",
      "properties": {
        "enabled": {
          "type": "boolean"
        },
        "realtime": {
          "type": "boolean"
        },
        "threshold": {
          "type": "object",
          "properties": {
            "detections": {
              "type": "number"
            },
            "elapsed": {
              "type": "number"
            }
          },
          "required": ["detections", "elapsed"]
        },
        "digest": {
          "type": "boolean"
        }
      },
      "required": ["enabled"],
      "if": {
        "properties": { "enabled": true }
      },
      "then": {
        "oneOf": [
          { "required": [ "realtime" ] },
          { "required": [ "threshold" ] },
          { "required": [ "digest" ] }
        ]
      }
    }
  }
}

谢谢!

最佳答案

结果我已经非常接近了,问题是如何将 if 语句评估为 true。以下是按预期验证(通过和失败)上述所有示例的正确架构:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "Settings",
  "type": "object",
  "properties": {
    "notifications": {
      "type": "object",
      "properties": {
        "enabled": {
          "type": "boolean"
        },
        "realtime": {
          "type": "boolean"
        },
        "threshold": {
          "type": "object",
          "properties": {
            "detections": {
              "type": "number"
            },
            "elapsed": {
              "type": "number"
            }
          },
          "required": ["detections", "elapsed"]
        },
        "digest": {
          "type": "boolean"
        }
      },
      "required": ["enabled"],
      "if": {
        "enabled": {
          "const": true
        }
      },
      "then": {
        "oneOf": [
          {
            "required": ["realtime"]
          },
          {
            "required": ["threshold"]
          },
          {
            "required": ["digest"]
          }
        ]
      }
    }
  }
}

这部分成功了:

"if": {
    "properties": {
        "enabled": {
            "const": true
        }
    }
}

关于javascript - 如何使用 AJV 根据输入值验证数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59317574/

相关文章:

javascript - simpleCart.js - 使用 beforeAdd() 调用购物车中单个商品的数量;

javascript - Ajax POST 数据未发送到 ASP.NET 中的 Page_Load 方法

javascript - 将变量传递给 escape_javascript

javascript - 是否可以将 typescript 接口(interface)导出为方法的输出?

validation - 带有完美消息的 JSON 模式验证

javascript - 为什么 $data 引用在此示例中不起作用?

javascript - 无法将 jQuery jqModal 分配给动态添加的链接

json - JSON 模式中 "description"字段的用途是什么?

Angular6 模式表单动态选择

javascript - 如何使用 JsonSchema 进行客户端实时验证?