簡體   English   中英

JSON模式+相對JSON指針:如何驗證“確認密碼”字段

[英]JSON schema + relative JSON-pointers: how to verify “confirm password” field

這是我的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,它說它確實 支持相對JSON指針

原來唯一的問題是我忘了將$data選項設置為true 例如

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM