question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Remove empty anyOf

See original GitHub issue

I have the following schema to add support for mongodb ObjectId type:

export const objectId = z.union([
  z.instanceof(ObjectId),
  z
    .string()
    .regex(/^[a-f0-9]{24}$/, { message: "This string does not look like an ObjectId()" })
    .transform((s) => new ObjectId(s))
]);

This translates to the following schema:

{
  anyOf: [ {}, { type: 'string', pattern: '^[a-f0-9]{24}$' } ]
}

The issue is that {} allows for any input. I think it would be more appropriate to imply that z.instanceOf() cannot be translated into a JSON Schema and therefore should never be able to match anything somehow.

Does that make any sense?

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
qraynaudcommented, Sep 23, 2022

Hey! This seems like a reasonable option at lest for the time being yes.

0reactions
StefanTerdellcommented, Sep 23, 2022

@qraynaud Hey, I just released 3.18.1 which adds a strictUnion option. Let me know if this solves your problem. If so I think we can close this

Read more comments on GitHub >

github_iconTop Results From Across the Web

Remove empty Objects from Array - javascript - Stack Overflow
I already use _.uniq from underscore.js to remove all duplicates from my array, which works fine. Though they are unique, one empty Object...
Read more >
Removing Array variable if it is empty
I have a series of Arrays that I am creating and adding into a compose to get them showing in a line by...
Read more >
Split a String removing any Empty Elements in JavaScript
To split a string and remove any empty elements from the array, call the `split()` method on the string to get an array...
Read more >
How To Remove Empty Array Elements In PHP | Lee Blue
Quick PHP tip: It is common to want to explode a string based on some delimiting character, then iterate over the array.
Read more >
Modifying data during validation - Ajv JSON schema validator
Ajv has several options that allow to modify data during validation: removeAdditional - to remove properties not defined in the schema object. useDefaults...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found