Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot validate shape of Errors #2190

Open
tkelly-mgm opened this issue Mar 28, 2024 · 2 comments
Open

Cannot validate shape of Errors #2190

tkelly-mgm opened this issue Mar 28, 2024 · 2 comments

Comments

@tkelly-mgm
Copy link

Describe the bug

When I use Yup to try to check if an Error is an object, it seems to fail, even if all the fields exist.

To Reproduce

const schema = yup.object({
  name: yup.string()
});

const obj = Object.assign(new Error(), {
  name: "Jill"
});

await expect(schema.isValid(obj)).resolves.toBe(true);

Expected behavior

I would expect the given schema to detect the error.

Platform (please complete the following information):

Firefox 124

Additional context

My use case is that I want to build a schema to detect an error thrown by a specific library. The library seems to construct errors with Object.assign(new Error, { ... }), so I can't use instanceof to differentiate from other thrown errors.

@jquense
Copy link
Owner

jquense commented Mar 30, 2024

What is the failure here, what doesn't work specifically

@tkelly-mgm
Copy link
Author

tkelly-mgm commented Apr 1, 2024

I'm trying to figure out how to get Yup object shape checks to work on an Error object.

I ran a debugger, and it looks like it's failing in a call to isObject:

yup/src/object.ts

Lines 72 to 73 in 5c77e0d

let isObject = (obj: any): obj is Record<PropertyKey, unknown> =>
Object.prototype.toString.call(obj) === '[object Object]';

This is saying that new Error() is not an object because Object.prototype.toString.call(new Error()) is [object Error].

Is this intended? Is some other method needed to check the shape of an Error?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants