ajv-validator / ajv-validator/ajv

modifying custom keyword not working. parentData and parentDataProperty are undefined.

未关闭
#1,433 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
enhancement limitation
主要语言
TypeScript
星标
14.8k
派生
1k
PR 合并指标
30 天内没有已合并 PR

描述

**What version of Ajv are you using? Does the issue happen if you use the latest version?**
7.0.4

**Ajv options object**

```javascript
{} // default options
```

**JSON Schema**

```json
{
"type": "array",
"filterItems": { "type": "string" }
}
```

**Sample data**

```json
["a", "b", 1, 2, "c", 3]
```

**Your code**

```javascript
ajv.addKeyword({
keyword: 'filterItems',
type: 'array',
modifying: true,
errors: false,
valid: true,
validate: (schema: SchemaObject, data: unknown[], _, dataCxt?: DataValidationCxt): boolean => {
if (!dataCxt) {
return false;
}
const { parentData, parentDataProperty } = dataCxt;
parentData[parentDataProperty] = data.filter((item) => ajv.validate(schema, item));
return true;
},
});
```

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

```
Error: Cannot set property 'undefined' of undefined
```

**What results did you expect?**
Items on array not matching the filterItems schema to be removed, without raising errors.

**Are you going to resolve the issue?**
I don't know how to solve this issue.

dataCxt.parentData and dataCxt.parentDataProperty are set to undefined. I was not expecting that. I have checked on https://github.com/ajv-validator/ajv/blob/33722772d89b428ff167730cd7955adff3cc8a0d/spec/keyword.spec.ts#L1279 to see how I am supposed to modify data, and seems like this is the proper way. I have also tried with a compile property for keyword instead of validate function, but I get the same error.

贡献指南

打开贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。