ajv-validator / ajv-validator/ajv

More specific error messages when validating mutual exclusive properties

オープン
#2,113 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る
enhancement
主要言語
TypeScript
スター
14.8k
フォーク
1k
PR マージ指標
30日以内にマージされた PR はありません

説明

I need to define a `range` object which has two set of mutual exclusive properties `(gt, gte)` and `(lt, lte)` meaning that if there is `gt` property then `gte` must not be present (and viceversa), and if there is `lt` property then `lte` property must not be present (and viceversa). This basically models a lower and upper range using operators `(<, <=, >, >=)`.

So far, I am using `dependencies` to model the mutual exclusive behaviour:

```ts
import Ajv from 'ajv';

const ajv = new Ajv();

const schema = {
type: 'object',
additionalProperties: false,
properties: {
gt: { type: 'string' },
gte: { type: 'string' },
lt: { type: 'string' },
lte: { type: 'string' },
},
dependencies: {
gt: { not: { required: ['gte'] } },
gte: { not: { required: ['gt'] } },
lt: { not: { required: ['lte'] } },
lte: { not: { required: ['lt'] } },
}
}

const validate = ajv.compile(schema);

console.log(validate({ gt: 'a', lte: 'z' }), validate.errors);
console.log();
console.log(validate({ gt: 'a', gte: 'b' }), validate.errors);
```

This works, but unfortunately the error message is not super helpful:

```
true null

false [
{
instancePath: '',
schemaPath: '#/dependencies/gt/not',
keyword: 'not',
params: {},
message: 'must NOT be valid'
}
]
```

This validation should be performed by an Api and ideally I would like to return to the client a more specific error like `gte must not be present if gt is specified` or something like this. Is there any way to model mutual exclusive set of properties like in this example and get more specific error messages?

コントリビューションガイド

コントリビューションガイドを開く

評価

この issue はまだ評価されていません。

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。