jcrist / msgspec

A fast serialization and validation library, with builtin support for JSON, MessagePack, YAML, and TOML
https://jcristharif.com/msgspec/
BSD 3-Clause "New" or "Revised" License
2.43k stars 75 forks source link

Collect all ValidationErrors instead of failing fast #721

Closed chuckwondo closed 3 months ago

chuckwondo commented 3 months ago

Description

It would be nice to be able to identify multiple ValidationErrors at a time rather than failing fast on the first ValidationError encountered.

For example, given this Struct:

class Position(msgspec.Struct, array_like=True):
    longitude: Annotated[float, msgspec.Meta(ge=-180.0, le=180.0)]
    latitude: Annotated[float, msgspec.Meta(ge=-90.0, le=90.0)]

If we decode data where both values are out of range, we get only one validation error:

>>> msgspec.json.decode('[-200, 100]', type=Position)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
msgspec.ValidationError: Expected `float` >= -180.0 - at `$[0]`

It would be helpful for both errors to be identified.

@jcrist, I've opened a few issues recently, but I'm more than happy to dig into the code to attempt creating some PRs, but I will likely need some pointers.

provinzkraut commented 3 months ago

Non-authoritative response, but @jcrist has made a statement about not wanting to support this before.

chuckwondo commented 3 months ago

Non-authoritative response, but @jcrist has made a statement about not wanting to support this before.

@provinzkraut, thank you for pointing that out. I couldn't manage to find prior, related discussions, so I appreciate your pointing me in the right direction. I had a feeling the answer would be along the lines covered in that thread, so I'll close this issue.