How to validate objects before insert/update/delete

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

How to validate objects before insert/update/delete

work monitored
Is there a recommended way to validate objects before insert/update/delete? Typical validations I would like to do is check sizes and make sure the column uses one of the allowed values before the insert/update. Also I'd like to put more complex validation such as not allowing delete if children exist or delete children first. 

I am also interested in not allowing update capability on certain tables. Thanks.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How to validate objects before insert/update/delete

Ray Sprinkle

What's wrong with using database permissions and triggers?

On Jan 16, 2015 5:26 PM, "work monitored" <[hidden email]> wrote:
Is there a recommended way to validate objects before insert/update/delete? Typical validations I would like to do is check sizes and make sure the column uses one of the allowed values before the insert/update. Also I'd like to put more complex validation such as not allowing delete if children exist or delete children first. 

I am also interested in not allowing update capability on certain tables. Thanks.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How to validate objects before insert/update/delete

Richard Yee
Use bean validation annotations

Sent from my iPhone

On Jan 18, 2015, at 5:00 AM, Ray Sprinkle <[hidden email]> wrote:

What's wrong with using database permissions and triggers?

On Jan 16, 2015 5:26 PM, "work monitored" <[hidden email]> wrote:
Is there a recommended way to validate objects before insert/update/delete? Typical validations I would like to do is check sizes and make sure the column uses one of the allowed values before the insert/update. Also I'd like to put more complex validation such as not allowing delete if children exist or delete children first. 

I am also interested in not allowing update capability on certain tables. Thanks.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How to validate objects before insert/update/delete

Brian Pontarelli
We do most of our validation in Java code. Translating database exceptions and error codes to useful error messages for users is really challenging. 

We also chucked bean validation via annotations because it we often hard to perform validations across fields and access the database to perform checks against existing data. We ended up writing so many custom validators, which require a significant amount of code.

Since we switched everything to Java 8, we wrote a cool little API using Java 8 lambdas to do the validation. It reduced the overall code and made multi-field and database validations much simpler. Here’s an example:

return new Validator()
.ifTrue(create, (validator) -> validator.notBlank(user.password, "user.password"))
.ifTrue(user.password != null, (validator) -> validator.notBlank(user.confirmPassword, "user.confirmPassword"))
.ifTrue(user.password != null, (validator) -> validator.custom(user.password.equals(user.confirmPassword), "user.confirmPassword", "[doNotMatch]"))
.ifTrue(user.password != null, (validator) -> validator.custom(user.password.length() > 5, "user.password", "[size]"))
.ifTrue(user.email != null, (validator) -> validator.custom(user.email.length() < 255, "user.email", "[size]"))
.ifTrue(user.email != null, (validator) -> validator.notDuplicated(user, "user.email", userMapper::retrieveExisting, user.email))
.notBlank(user.email, "user.email")
.ifTrue(user.email != null, (validator) -> validator.email(user.email, "user.email"))
.ifTrue(rolesInUser, (validator) -> validator.mustNotBeEmpty(user.getRoles(), "user.roles"))
.ifTrue(!rolesInUser, (validator) -> validator.mustNotBeEmpty(user.getRoles(), "roles"))
.done();

Hopefully that colorized code comes through via Google Groups.

-- Brian


On Jan 18, 2015, at 9:42 AM, Richard Yee <[hidden email]> wrote:

Use bean validation annotations

Sent from my iPhone

On Jan 18, 2015, at 5:00 AM, Ray Sprinkle <[hidden email]> wrote:

What's wrong with using database permissions and triggers?

On Jan 16, 2015 5:26 PM, "work monitored" <[hidden email]> wrote:
Is there a recommended way to validate objects before insert/update/delete? Typical validations I would like to do is check sizes and make sure the column uses one of the allowed values before the insert/update. Also I'd like to put more complex validation such as not allowing delete if children exist or delete children first. 

I am also interested in not allowing update capability on certain tables. Thanks.


--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How to validate objects before insert/update/delete

work monitored
Assuming I am not a fan of Bean validation and do not have JDK 8, and even had JDK 8, how do I tell MyBatis to validate or run a custom method before insert/update? The mappers are interfaces in MyBatis. 

On Sunday, 18 January 2015 10:09:10 UTC-7, Brian Pontarelli wrote:
We do most of our validation in Java code. Translating database exceptions and error codes to useful error messages for users is really challenging. 

We also chucked bean validation via annotations because it we often hard to perform validations across fields and access the database to perform checks against existing data. We ended up writing so many custom validators, which require a significant amount of code.

Since we switched everything to Java 8, we wrote a cool little API using Java 8 lambdas to do the validation. It reduced the overall code and made multi-field and database validations much simpler. Here’s an example:

return new Validator()
.ifTrue(create, (validator) -> validator.notBlank(user.password, "user.password"))
.ifTrue(user.password != null, (validator) -> validator.notBlank(user.confirmPassword, "user.confirmPassword"))
.ifTrue(user.password != null, (validator) -> validator.custom(user.password.equals(user.confirmPassword), "user.confirmPassword", "[doNotMatch]"))
.ifTrue(user.password != null, (validator) -> validator.custom(user.password.length() > 5, "user.password", "[size]"))
.ifTrue(user.email != null, (validator) -> validator.custom(user.email.length() < 255, "user.email", "[size]"))
.ifTrue(user.email != null, (validator) -> validator.notDuplicated(user, "user.email", userMapper::retrieveExisting, user.email))
.notBlank(user.email, "user.email")
.ifTrue(user.email != null, (validator) -> validator.email(user.email, "user.email"))
.ifTrue(rolesInUser, (validator) -> validator.mustNotBeEmpty(user.getRoles(), "user.roles"))
.ifTrue(!rolesInUser, (validator) -> validator.mustNotBeEmpty(user.getRoles(), "roles"))
.done();

Hopefully that colorized code comes through via Google Groups.

-- Brian


On Jan 18, 2015, at 9:42 AM, Richard Yee <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="lhZsyf-PYR4J" rel="nofollow" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;">richar...@...> wrote:

Use bean validation annotations

Sent from my iPhone

On Jan 18, 2015, at 5:00 AM, Ray Sprinkle <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="lhZsyf-PYR4J" rel="nofollow" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;">rdspr...@...> wrote:

What's wrong with using database permissions and triggers?

On Jan 16, 2015 5:26 PM, "work monitored" <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="lhZsyf-PYR4J" rel="nofollow" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;">work.mo...@...> wrote:
Is there a recommended way to validate objects before insert/update/delete? Typical validations I would like to do is check sizes and make sure the column uses one of the allowed values before the insert/update. Also I'd like to put more complex validation such as not allowing delete if children exist or delete children first. 

I am also interested in not allowing update capability on certain tables. Thanks.


--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="lhZsyf-PYR4J" rel="nofollow" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;">mybatis-user...@googlegroups.com.
For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href='https://groups.google.com/d/optout';return true;" onclick="this.href='https://groups.google.com/d/optout';return true;">https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="lhZsyf-PYR4J" rel="nofollow" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;">mybatis-user...@googlegroups.com.
For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href='https://groups.google.com/d/optout';return true;" onclick="this.href='https://groups.google.com/d/optout';return true;">https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="lhZsyf-PYR4J" rel="nofollow" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;">mybatis-user...@googlegroups.com.
For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href='https://groups.google.com/d/optout';return true;" onclick="this.href='https://groups.google.com/d/optout';return true;">https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.