-
-
Notifications
You must be signed in to change notification settings - Fork 499
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
Add ability to validate/modify property values as they are set #3567
Comments
You can use custom mapped types for this, or no? https://mikro-orm.io/docs/custom-types Or when exactly do you want to validate things?
If you mean validating the keys, that is a bit out of scope, just define your own funciton that validates the keys. For validating the values, mapped types should do the job. |
I considered custom types. I use one already (they're fantastic btw) but if I have an entity with 20 string properties that I want to validate, that would call for 20 custom types. Here's an example, just to make sure we're on the same page. If you feel it's out of scope, I would be disappointed but life goes on.
I tried to code this directly on the entity class but the hydrator overwrites the properties. |
If you want to validate them in 20 different ways, yes. Otherwise one type will do the job just fine. You can even have multiple instances of a single type class, with different parameters. Btw for this purpose you can use enums too, which will give you schema level validation, much better if you ask me.
Provide some code, I dont know what you tried. You can have a property defined as get/set pair. |
I think I know what you mean with the setters. If you have a value in database, its hydrated as a value - and that is expected behaviour. Calling the setting with internal value seems to be wrong idea (imagine a setter that adds something to the string, it would always produce update queries), and we can't set the value and have it defined as a setter at the same time - thats simply not possible in JS. What you can do is to have a regular property to store value, and a virtual get/set pair to work with it. But its all very quirky, you should go with custom types (or maybe with enums). I will close this, the example you provided is the exact use case for them. Only difference is that the validation/mutation takes place on flush and not when setting the value, which should be fine. |
The back-story here is that we're switching from Sequelize and sequelize models have this capability. I see your point about calling the setter during hydration. Fairly sure Sequelize doesn't do that either. I'm fairly confident there's a valid use case here but I need to better define how it would work. Thanks for the feedback. I'll continue to think about this. |
Decided to go with custom types for validation and it's working well with one exception. One side effect, which is probably a good thing, is that I'm now getting validation on custom types used in criteria. The problem I'm having is with Like to get your thoughts on this, and thanks! |
|
This is what I'm after. A way to know when to skip validation in a custom type, or when to throw an exception when someone is trying to use a
|
I don't like to add more parameters in there, in fact, since v5.5 we dont even need the |
That would be fantastic. Thanks again (and again)! |
One potential open issue. You said that platform is available as
I believe you refer to this problem in your V6 Ideas post but wanted to make sure I'm not creating an issue for myself today. |
I was wrong, there is no |
|
Depends on when you check, they are added during discovery. They will be there only for runtime convertion methods. |
Ah.. that was added in 5.5.0. Fantastic! I'm blocked from upgrading to 5.5.0 because of a regression that you've already fixed for the next release. |
You can use dev version, every commit to master is published to NPM. |
Is your feature request related to a problem? Please describe.
When assigning values to an entity instance, we'd like to be able to provide a validation function that can alter the supplied value if it wants. Most of the time it'll be something like making sure some fields are uppercase and other times we might want to reject a value if it doesn't pass some validation.
Describe the solution you'd like
My current thinking is to have a validation function in the @Property decorator that accepts and returns the value.
Describe alternatives you've considered
I spent some time trying to code my way out of this with get and set functions but they are overwritten during hydration.
Additional context
None
The text was updated successfully, but these errors were encountered: