ajv-validator / ajv-validator/ajv

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

Offen
#1,433 2 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
enhancement limitation
Vorherrschende Sprache
TypeScript
Sterne
14.8k
Forks
1k
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

**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.

Beitragsleitfaden

Beitragsleitfaden öffnen

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.