-
Notifications
You must be signed in to change notification settings - Fork 995
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
Coerce form values to Prisma type #749
Conversation
OMG this is amazing, does it really work? Have you solved just about all outstanding issues with scaffold generation of non-string types?? |
displayFunction: '', | ||
}, | ||
Float: { | ||
validation: "{{ required: true, type: 'Float' }}", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How come this is the only one with an explicit type
prop set and not also Int
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When deciding what coercion to make, I look at two things; the name of the component used for the field, and the type
prop. For Int
I use a <NumberField>
component, and those will be coerced using parseInt
. For Float
I use a regular <TextField>
, for which no coercion is done at all by default. By adding type: 'Float'
the parseFloat
coercion will kick in.
Now, you might wonder why I use <TextField>
for floats, and the reason for that is that <NumberField>
is just a <input type="number">
under the hood, and those don't support decimals by default. There is a way around that, by setting the step
attribute to something < 1 (and > 0). For example step="0.01"
will allow you to enter floats with two decimals, but not three or more decimals. I didn't want to enforce how many decimals our scaffolded forms support, so that's why I went with <TextField>
. That way the user can enter however many decimals they want.
It might be a little misleading to have type
be part of validation
since I'm not actually using it for form validation. Should I move it to its own prop? And if so, maybe rename it to something like dataType
to not confuse it with the type
used on the underlying <input>
Another option might be to actually use it for validation by trying to build custom validation functions to pass to react-hook-form based on the type. If we want to go this route, could we do that as a separate PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense! I like that idea of changing it to dataType
so as not to confuse with regular type
on the input. And it should probably move out of validation
to keep it simple and we can pass that object in bulk to react-hook-form without having to tweak anything.
Yeah I think in the future we want to let users set dataType
themselves on any field they want, including passing functions for custom coercion. We can definitely do that as another PR.
51e6818
to
0e5ddbd
Compare
Love it! Ready to merge or did you want to add any more tweaks? |
I'd like to test the Json stuff, but ran into some problems, see
https://community.redwoodjs.com/t/error-unknown-type-json-did-you-mean-json/817/6
Would also like to add some kind of automated tests, but not really sure
how to do it. Any ideas?
…On Thu, Jun 25, 2020, 18:30 Rob Cameron ***@***.***> wrote:
Love it! Ready to merge or did you want to add any more tweaks?
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#749 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAAHQSKO74KILFZRSZINOILRYN3TPANCNFSM4OHFHJPQ>
.
|
Well, the extent of the generator testing is to create fixtures and compare with the output, but now all the work happens in that function so there won't be any code differences in the scaffold. Did you want to try and add tests to |
Hey @cannikin and @Tobbe This looks very cool! If I'm not mistaken To get the contexts and providers you need you can render using the For the actual tests I would:
Note that the |
0b3db14
to
a75991b
Compare
6f19e93
to
f03c26c
Compare
I haven't forgotten about this one, and now that #514 needs it too, I'll make sure to do more work on it. I'm pretty confident I can get some tests working, but I'm still not sure about the JSON stuff mentioned earlier. Anyone else have any thoughts on that? |
@Tobbe this is awesome. I ran into some of these issues you resolve here over the weekend too when I noticed what was happening in #514. I can start working on a PR for that and hold off for whatever you have here. If I get that PR up, technically it would still work minus the coercion on POST for a date field. |
@brianespinosa You could also base your work off of my branch, https://github.com/Tobbe/redwood/tree/tobbe-coerce-form-values and get it all working in one go. And then just rebase it on top of https://github.com/redwoodjs/redwood when this PR is merged. That way you can get all functionality in from the start, and you won't have any merge conflicts :) But, of course, do what works best for you 🙂 |
@Tobbe duh to me. Yes, I will do that. ;) |
b9b90ea
to
dc807d8
Compare
Please merge #800 first, so that we can see the tests I have added here passing when run by GitHub Actions as well, before merging this |
@RobertBroersma Please take a look at the tests. Do they look good to you? Anything you want me to change? |
Looks good to me! |
Used instead of "String" for default settings
Only reason is to get alphabetical order
dc807d8
to
6b3f653
Compare
I'm happy with this PR now @cannikin Please merge if you're happy with the way everything looks |
Thanks so much for this one @Tobbe! |
No description provided.