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.

Superstruct: Array attribute types error an array of errors instead of an object for errors

See original GitHub issue

Describe the bug

When using superstruct resolver with an array field type the the error object is an array instead of an object per the React Hook Form documentation.

To Reproduce Steps to reproduce the behavior:

  1. Go to https://codesandbox.io/s/competent-kalam-v98vd?file=/src/App.js
  2. Click on “Submit Query” button
  3. Observe the arrayField errors property is an array instead of an object. This is the unexpected behavior.
  4. Observe that nonArrayField errors property is an object as expected.

Expected behavior

Per the documentation the errors for a field should be an object with a message and type property and not an array.

Desktop (please complete the following information):

  • OS: MacOS 10.15.7
  • Browser: Firefox
  • Version 88.0b4

Additional context

In this instance it appears that parseErrorSchema produces an object like this:

  { "arrayField.0": { "message": "Expected one of `"John,"Jane"`, but received: "Bill"", "type": "enums" } }

which when passed to toNestError from @hookform/resovlers returns an Array

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
coreyjvcommented, Jun 17, 2021

@jorisre there’s no solution that I’m aware of besides checking if the error object is an array and handling it accordingly.

1reaction
jorisrecommented, May 30, 2021

@coreyjv Did you find a solution?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Errors - Superstruct
The error class that Superstruct uses for its validation errors. ... The key of the value when validating complex values like objects, arrays,...
Read more >
Types - Superstruct
Custom types take validator functions that return either true/false or an array of StructFailure objects, in case you want to build more helpful...
Read more >
FAQ - Superstruct
The object struct validates that a value matches a known object shape. Just like it's TypeScript counterpart, it does not allow unknown keys—this...
Read more >
Handling Errors - Superstruct
The error thrown by Superstruct is always the first validation failure that was encountered, because this makes for convenient and simple logic in...
Read more >
Introduction - Superstruct
import { assert, object, number, string, array } from 'superstruct' ... or nested objects, composing structs inside each other, returning errors instead of ......
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