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

Capture validation errors without failing container schema #665

Open
bentheiii opened this issue Apr 15, 2024 · 0 comments
Open

Capture validation errors without failing container schema #665

bentheiii opened this issue Apr 15, 2024 · 0 comments

Comments

@bentheiii
Copy link

bentheiii commented Apr 15, 2024

It would be useful to be able to parse a list of input, while knowing some of them will not conform to a schema, for example, given a schema:

class S(Struct):
    a: int
    b: str

and the input [{"a": 1, "b": "foo"}, {"c": 2}, {"a": 3, "b": "bar"}], we want to be able to extract the first and third elements, even though the second is invalid. A bonus would be to be able to extract the errors that occurred. my current solution for this is as follows:

class S(Struct):
    a: int
    b: str

@dataclass
class Failure:
    error: Exception
    builtins: Any

class Maybe(ABC, Generic[T]):
    @classmethod
    def __subclasscheck__(cls, subclass): 
        return True

gen_alias = type(Maybe[int])

def dec_hook(type_, obj):
    if isinstance(type_, gen_alias) and type_.__origin__ is Maybe:
        try:
            return convert(obj, type_.__args__[0])
        except Exception as e:
            return Failure(error=e, builtins=obj)
    else:
        raise TypeError(f"Unknown type: {type_}")

d_invalid = Decoder(list[Maybe[S]], dec_hook=dec_hook)

raw_invalid = b'[{"a": 1, "b": "foo"}, {"c": 3}, {"a": 3, "b": "bar"}]'

lst = d_invalid.decode(raw_invalid)

for i, x in enumerate(lst):
    print(f"{i}: {x!r}")

# 0: S(a=1, b='foo')
# 1: Failure(error=ValidationError('Object missing required field `a`'), builtins={'c': 2})
# 2: S(a=3, b='bar')

however, this solution is both cumbersome and is between x4 and x10 slower than parsing a list[S], maybe a lower-level solution will be faster

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

1 participant