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.

array field infers strange errors object

See original GitHub issue

Hi =) image

this kind of zod schema dont work as expected


const schema = z.object({
  title: z
    .string()
    .regex(/^[A-Za-z0-9_]+$/, "wrong pattern")
    .nonempty("Required"),

  someArray: z.array(z.string().nonempty()).min(1)
});

Steps to reproduce the behavior:

    1. fill title input
    1. press submit
    1. fill array input
    1. press submit
    1. clear array input
  • in console errors changes shape of field someArray from Object to Array

https://codesandbox.io/s/gifted-hopper-k4ndp?file=/src/App.tsx

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
jorisrecommented, Jun 18, 2021

@kerosan sorry for the late reply, I found the problem.

You’ve to update the onChange:

onChange={(e) => {
  onChange(e.target.value ? [e.target.value] : []);
}}

Because e.target.value is "" when you clear the input and zod validate the following value: [""].

@bluebill1049 Is that normal we can’t do the following: onChange(undefined), seems not working with undefined 🤔

Hope it solves your issue 😃

2reactions
jorisrecommented, Jun 16, 2021

Hi @kerosan I’ll have a look tonight 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why Compiler cannot infer the object array type?
Because the types in the array aren't specific object - they are 3 different types that are all subclasses of object.
Read more >
issue with clear array errors in FieldArray #1097 - GitHub
When "errors" is empty the array is not been removed in "errors" object. Steps to reproduce the behavior: remove value from the first...
Read more >
Arrays | Elasticsearch Guide [8.5] | Elastic
In Elasticsearch, there is no dedicated array data type. Any field can contain zero or more values by default, however, all values in...
Read more >
Arrays - The Modern JavaScript Tutorial
An array, just like an object, may end with a comma: let fruits = [ "Apple", "Orange", ... Let's say we want the...
Read more >
10 Most Common Mistakes That PHP Developers Make - Toptal
As a result, when we go through the second foreach loop, “weird stuff” ... The reason is that, unlike arrays, PHP always passes...
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