Skip to content

ZodError when processing objects with undefined values #723

@nahoj

Description

@nahoj

Description

When passing an object with undefined properties to jq.run(), node-jq throws a ZodError instead of handling the case gracefully. This occurs with any jq filter.

Environment

  • TypeScript
  • node-jq version: 6.0.1
  • Node.js version: v22.15.1
  • Platform: Linux

Minimal Reproduction

import jq from "node-jq";

async function reproduceZodError() {
  const data = { x: undefined, y: "hello" };
  
  try {
    const result = await jq.run('.', data, { input: "json", output: "json" });
    console.log("Success:", result);
  } catch (error) {
    console.log("ZodError occurred:", error.message);
  }
}

reproduceZodError();

Expected Behavior

I suppose there are several behaviors that could be considered valid:

  1. Treating undefined as null
  2. Filtering out undefined properties like JSON.stringify()
  3. Specific error message

Actual Behavior

Throws a ZodError with validation errors:

ZodError: [
  {
    "code": "invalid_union",
    "unionErrors": [
      // ... lots of nested errors
    ],
    "path": ["json", "x"],
    "message": "Invalid input"
  }
]

Workaround

Remove undefined values before processing:

function removeUndefined(obj) {
  return JSON.parse(JSON.stringify(obj));
}

const cleanData = removeUndefined(originalData);
const result = await jq.run('.', cleanData, { input: "json", output: "json" });

PS: Part of this was written by Claude Code; it also helped identify the bug.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions