【问题标题】:JSON schema + relative JSON-pointers: how to verify "confirm password" fieldJSON模式+相对JSON指针:如何验证“确认密码”字段
【发布时间】:2018-03-03 03:22:58
【问题描述】:

这是我的JSON Schema

{
  "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"
}

您可以将这些复制粘贴到this online validator 以查看会发生什么。

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

值“asdfasdf”与 const 不匹配。

我认为我的relative JSON pointer 有问题,但我不知道正确的语法是什么。

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

我正在使用的具体实现是 AJV,它说 确实 support relative-JSON-pointers.

【问题讨论】:

  • @Amy 您链接的规范仅适用于 absolute JSON 指针,不是吗?

标签: javascript jsonschema ajv


【解决方案1】:

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

const ajv = new Ajv({
    allErrors: true,
    $data: true,
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-09
    • 2017-09-15
    • 2022-10-21
    • 1970-01-01
    • 2012-12-08
    相关资源
    最近更新 更多