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

Improve error message for invalid JSON values #224

Merged
merged 3 commits into from
Dec 10, 2024

Conversation

Mrtenz
Copy link
Member

@Mrtenz Mrtenz commented Dec 10, 2024

Currently we use assertStruct in the JsonStruct, to ensure that the value is JSON-serialisable before coercing it. assertStruct returns a generic AssertionError, and Superstruct doesn't have any information about where the error was thrown (such as the path). Given the following struct for example:

const ExampleStruct = object({
  value: JsonStruct,
});

An invalid value would result in an AssertionError with the following message:

Assertion failed: Expected a value of type JSON, but received: undefined.

After this change, a StructError is thrown instead, with the following message:

At path: value -- Expected a value of type JSON, but received: undefined.

This makes it more clear that the error happens at value, and it also makes more sense to throw a StructError in this case.

@Mrtenz Mrtenz merged commit f3d602a into main Dec 10, 2024
17 checks passed
@Mrtenz Mrtenz deleted the mrtenz/improve-json-struct-error branch December 10, 2024 12:28
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

Successfully merging this pull request may close these issues.

2 participants