11

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.

Community
  • 1
  • 1
mpen
  • 272,448
  • 266
  • 850
  • 1,236

1 Answers1

13

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,
});
mpen
  • 272,448
  • 266
  • 850
  • 1,236