ajv-validator / ajv-validator/ajv

undefined values in schemas are not ignored but throws TypeError

Aperta
#2,578 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub
bug report
Lingua principale
TypeScript
Stelle
14.8k
Fork
1k
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

I was struggling for hours with an error only happening in my deployed function, but not locally.
Turns out, since I generate the schema programmatically, I had some optional property that was undefined in the schema. This causes AJV to throw `TypeError: Cannot convert undefined or null to object`.

I logged my schema and data, copy pasted it in a local test program and spent HOURS not understanding what was happening. Why was it working locally but not when running in my deployed function?
Turns out the schema had an `undefined` in it, which was of course trimmed away silently when logging to cloud watch, hence not ending up in my local test program.

is there any reason Ajv could not ignore undefineds? I assume it does something like iterate properties using `Object.keys` or `for const k in obj`, and just checking `if obj[k] === undefined {}` seems harmless?

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

ajv 8.17.1 - latest

**Ajv options object**

```javascript
{strictSchema: false}

```

**JSON Schema**

```json
{
type: "object",
properties: {
foo: {type: "string"},
bar: undefined
}
}

```

**Sample data**

```json
{foo: "FOO"}

```

**Your code**

```javascript
new Ajv({strictSchema: false}).validate(schema, data);
```

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

```
TypeError: Cannot convert undefined or null to object
```

**What results did you expect?**

I expected `undefined` values in schemas to be ignored silently and the object to match the schema.

**Are you going to resolve the issue?**

I'm happy to attempt at a PR

Guida per i contributori

Apri la guida per i contributori

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.