-
Notifications
You must be signed in to change notification settings - Fork 14
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
Support custom field name for validation #17
Comments
Tagging @mulias for follow-up discussion. |
Also, @jaycle we'd love to get a PR submitted with any changes for discussion if you are up for it. |
Hey @jaycle, thanks for pointing out this use case. The main project I've been using this tool on involves a pretty direct mapping from the input JSON to the objects I want to use, so this issue has not yet come up for me personally. First off, this is how I would solve your problem with the tools already provided by the library:
I understand that this isn't a perfect solution, and you're probably looking for something more robust because you have lots of fields to map. While your suggested syntax would be ideal for your situation, I hesitate to move forward with it for a few reasons. For one, adding the optional field name argument would allow the user to create decoders like this:
and then if you have an array, which of these would be the valid syntax? How do you communicate that in documentation?
These issues are solvable, but I'm seeing a lot of new complexity which doesn't look promising. In a situation like this the first place I'd look is an existing json combinator library. Here's what I'm thinking about right now, based on Elm's solution (http://package.elm-lang.org/packages/elm-lang/core/5.1.1/Json-Decode#map2). I don't love it, but maybe you'll see something worth pursuing?
For added ergonomics we could change I'm going to keep thinking about this for a bit, but I'm interested to hear your thoughts. If there's a direction you're feeling confident about I'd also love to see a PR. |
First, thank you for providing this library. It seems to be really well designed and thought out and I really appreciate you considering my use case and also wanting to balance it with how it fits into the overall design of the library. I'm fairly new to typescript and javascript but I've looked around the source and it certainly looked like a non-trivial request. I'd love to take a stab at an implementation but perhaps more discussion around the best (and most feasible/flexible) syntax should preclude that. On the feasibility piece I would certainly defer to you @mulias. I'm not opposed to the valueAt syntax but I'm with you in the sentiment that perhaps it could use some more thought. This isn't an urgent request for me and I'll try to investigate a bit further, too. Thanks for being willing to think through this with me. |
Hey @jaycle have you had a chance to use this library more on your project? Have you tried out using |
I've been using the field with their names as they come from the server at the moment. I didn't like the map solution because it meant I had duplicate fields varying only in style. I did a bit more playing around with destructing and found something a bit better along the same lines: const userDecoder: Decoder<User> = object({
first_name: string(),
last_name: string(),
username: string()
}).map(user => {
const { first_name, last_name, ...splitUser } = user;
return {
...splitUser,
firstName: first_name,
lastName: last_name
};
}); Still a bit cumbersome but workable. Other than just looking at the code, I haven't had much time to play around with real solutions within the library. I think |
Hi. Looks like an issue is a year old, but I've just discovered this library while looking for alternatives to bs-json in ReasonML and that's exactly the functionality I was looking for :)
Is there any reason to not have the string as a required parameter (rather than optional)? |
I think this would be nice. Since the @mulias would you like me to try to implement this? |
@mixvar That sounds fine to me -- the argument should be changed to something like @Yakimych Ultimately the What I proposed in my initial response was a different decoder, which I named The possible routes to take as I see them are:
At this point I'm in no rush to make design decisions for this library, so if anyone wants to advocate for a particular option or put together a PR go for it. |
Sometimes the convention from the server in json doesn't directly map to the styles on the front end. For example, data from the API may be in
snake_case
when the javascript is usingcamelCase
. Since this library is already parsing and converting raw json to correctly formed typescript compatible types, it seems this would be a great layer to adapt to these differences.For example, something like the following syntax would be ideal:
where the the string decoder has an optional string parameter.
The text was updated successfully, but these errors were encountered: