3

By using ajv, how can I reference foo to validate that bar has the same value?

var Ajv = require('ajv');
var ajv = new Ajv({allErrors: true});

var schema = {
  'properties': {
    'foo': { 'type': 'string', 'minLength': 3 },
    'bar': { 'type': 'string', ###HERE### },
  }
};

data = {
  'foo': 'abc',
  'bar': 'ab',
};

ajv.validate(schema, data);   // <-- should return false, foo !== bar

I've tried using (with similar variations):

...
'bar': { 'type': 'string', 'format': { '$data': 'foo' } },
...

But that didn't work.

DKo
  • 820
  • 1
  • 9
  • 19

2 Answers2

9

You can:

var ajv = new Ajv({allErrors: true, $data: true}); // for version >=5.0.0

var schema = {
  properties: {
    foo: { type: 'string', minLength: 3 },
    bar: { const: { $data: '1/foo' } },
  }
};
esp
  • 7,314
  • 6
  • 49
  • 79
3

@explorer, here it is

foo: {
    type: "string",
    not: {
        const: {
            $data: "1/baz"
        }
    }
}
BrDaHa
  • 5,138
  • 5
  • 32
  • 47
SirPhemmiey
  • 585
  • 6
  • 7