Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON schema + relative JSON-pointers: how to verify "confirm password" field

Here's my 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"
}

Here's my data:

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

You can copy-paste those into this online validator to see what happens.

The confirmPassword field is failing validation with error message:

Value "asdfasdf" does not match const.

I believe there is a problem with my relative JSON pointer but I can't figure out what the correct syntax is.

AFAICT, 1/password means "go up one level, and then check the password property" but that doesn't appear to be the case. What's correct syntax?

The specific implementation I'm using is AJV which says it does support relative-JSON-pointers.

like image 872
mpen Avatar asked Sep 21 '17 17:09

mpen


1 Answers

Turns out the only problem was that I forgot to set the $data option to true. e.g.

const ajv = new Ajv({
    allErrors: true,
    $data: true,
});
like image 195
mpen Avatar answered Nov 11 '22 06:11

mpen