1

通过使用ajv,我如何引用foo来验证bar具有相同的值?

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

我试过使用(有类似的变化):

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

但这没有用。

4

2 回答 2

8

你可以:

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' } },
  }
};
于 2017-04-17T22:07:45.633 回答
2

@explorer,在这里

foo: {
    type: "string",
    not: {
        const: {
            $data: "1/baz"
        }
    }
}
于 2019-09-19T09:39:36.673 回答