Skip to content

undefined values in schemas are not ignored but throws TypeError #2578

@henhal

Description

@henhal

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 {<skip this>} 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

{strictSchema: false}

JSON Schema

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

Sample data

{foo:  "FOO"}

Your code

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions