-
Notifications
You must be signed in to change notification settings - Fork 89
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
Strip unknown keys from validated object #132
Comments
Hey, I've faced the same conundrum as you. Failing to realize that TypeScript being structurally typed, all objects that have the same (or more) properties will also pass the guard. Now, technicially speaking, I think that's OK, because that guard (for me) is supposed to be equivalent with what you would get with static typing. But you can write your own guards, and that's what I was thinking I'll end up doing. I mean, the way I think about this, is that |
Not sure if this is helpful, but I put together a gist that models this as a constraint function. It's a little janky because it needs currying to have access to the runtype it is validating, and only really covers records; however, my experience has been that records are the main thing this is needed for, for things like filtering data coming back from an API. https://gist.github.com/schicks/8a037fe7b3b4d1b0ceb505e8afcc9db3 |
I'd be nice to take advantage of the validate option in e.g.
Record
to remove unknown fields when giving an object with known fields and 'valid' values.Example:
I hoped to get away with overriding the Record type, but it seems that the validation goes from deepest object to the most outer object when using nested
RunType
s.EDIT:
I worked around the fields value, but it would be nice to take into account
rt.Array
types... I think that's too hacky to implement using this workaround.The text was updated successfully, but these errors were encountered: