Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve error message for invalid JSON values (#224)
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: ```ts 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.
- Loading branch information