ajv-validator / ajv-validator/ajv

Bug in conditions `if/then`.

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

説明

**What version of Ajv are you using? Does the issue happen if you use the latest version?**
version: 8.17.1 (latest)

**Ajv options object**

```typescript
const ajv = new Ajv({
strict: true
})
```

**JSON Schema**

```javascript
const schema = {
type: 'object',
properties: {
name: {
type: 'string'
},
age: {
type: 'number'
}
},
required: [ 'name' ],
if: {
properties: {
name: { type: 'string', minLength: 3 }
}
},
then: {
properties: {
age: { type: 'number', minimum: 10 }
},
required: [ 'age' ]
}
}
```

**Sample data**
```json
const data = {
name: undefined,
age: undefined
}
```

**Validation result, data AFTER validation, error messages**

```
[
{
instancePath: '',
keyword: 'required',
message: 'must have required property `age`',
params: {
missingProperty: 'age'
},
schemaPath: '#/then/required'
}
]
```

**What results did you expect?**

```
[
{
instancePath: '',
keyword: 'required',
message: 'must have required property `name`',
params: {
missingProperty: 'name'
},
schemaPath: '#/then/required'
}
]
```

So, I'm trying validate the data by this `schema`. And faced with unexpected behavior. In this example I've got error with `age` field instead of `name`. So I expect that `name` field at first level in object must be validated before `age`.
By the way, in another example, if I've got an empty string in `name` field, I have a valid `data`.

I've tried to change the schema like this using `allOf`:

```javascript
const schema = {
type: 'object',
properties: {
name: {
type: 'string'
},
age: {
type: 'number'
}
},
required: [ 'name' ],
allOf: [
{
if: {
properties: {
name: { type: 'string', minLength: 3 }
}
},
then: {
properties: {
age: { type: 'number', minimum: 10 }
},
required: [ 'age' ]
}
}
]
}
```

Same result.

**Are you going to resolve the issue?**
I'm asking your comment and advice first of all.

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

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

評価

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

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

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