10

这是我的JSON 架构

{
  "required": [
    "username",
    "password",
    "confirmPassword"
  ],
  "properties": {
    "username": {
      "minLength": 3,
      "type": "string"
    },
    "password": {
      "minLength": 6,
      "type": "string"
    },
    "confirmPassword": {
      "const": {
        "$data": "1/password"
      },
      "type": "string"
    }
  },
  "type": "object"
}

这是我的数据:

{
  "username": "abc",
  "password": "asdfasdf",
  "confirmPassword": "asdfasdf"
}

你可以将它们复制粘贴到这个在线验证器中,看看会发生什么。

confirmPassword字段验证失败并显示错误消息:

值“asdfasdf”与 const 不匹配。

我相信我的相对 JSON 指针有问题,但我不知道正确的语法是什么。

AFAICT,1/password意思是“上一层,然后检查password属性”,但似乎并非如此。什么是正确的语法?

我正在使用的具体实现是 AJV,它表示它确实 支持 relative-JSON-pointers

4

1 回答 1

12

原来唯一的问题是我忘记将$data选项设置为true. 例如

const ajv = new Ajv({
    allErrors: true,
    $data: true,
});
于 2017-09-21T18:00:14.860 回答