0

We have fields: currency, amount, customerId. Let's think that our users input currencies can be USD, GBP, EUR. For USD we have to check amount (should be less than 1000) and customerId by regex. For GBP we only check customerId by regex. For EUR we only check amount.

Is switch(currency) and if statements okay for checking validations?

Sara Eva
  • 13
  • 2
  • Welcome to stack overflow. As a side note, since you're dealing with money I would urge you to read the following articles that basically state, don't use a float for storing money values. [Money](https://martinfowler.com/eaaCatalog/money.html) by Martin Fowler, [You better work in cents, not dollars](https://blog.agentrisk.com/you-better-work-in-cents-not-dollars-2edb52cdf308). – hooknc Mar 15 '22 at 17:59

3 Answers3

0

If you are using spring boot, I suppose that using annotation base validation is the cleanest and the most readable way. Use @Max(value=1000) for amount and also @Pattern for regex validation.For currencies, I suggest using Enum and handling Exception. For the situation EUR you can see this: https://stackoverflow.com/a/2783859/11072063[1]

m.bidkham
  • 1
  • 1
0

I would recommend looking into Java Bean Validation via Hibernate Validator.

Baeldung has a great tutorial on how to use validation inside of Spring-Boot.

When writing your validation I would urge you to consider the following validation levels:

  • Required, does the value have to exist.
  • Type, is the value of the right type? Meaning is it a string, boolean, or integer?
  • Date, does the value meet certain criteria? Is the value large enough, long enough, complex enough, etc...
  • Does the value meet any requirements that are associated with the database? Does the value have to be unique or does the value have to exist already in the database?

I wrote a fairly detailed answer a while ago that might be of use: How to perform custom validations in Spring MVC?

hooknc
  • 4,854
  • 5
  • 31
  • 60
-1

Since you have tagged Spring and Springboot and asked for cleanest way to validate user inputs, I would suggest Spring aop.

You can have a Validation Aspect.

This keeps your business logic clean with validation logic separated from it.

Cozimetzer
  • 662
  • 4
  • 12
  • May I know the reason for disliking this approach? Aspects are used for cross cutting concerns like validation, logging, etc. – Cozimetzer Mar 15 '22 at 17:36
  • 1
    I didn't down vote you, but spring-boot uses [Java Bean Validation](https://www.baeldung.com/javax-validation) via [Hibernate Validator](https://hibernate.org/validator/) by default. I would string urge/recommend using that framework instead of "rolling your own" using AOP. – hooknc Mar 15 '22 at 17:41
  • Looks good for field level validations! But Does it support the validation he require? Different validation based on currency? I prefer aop because I feel the validations can be kept away from business logic and models. Also it has more flexibility. For example, You can validate against values from database or an external source. – Cozimetzer Mar 15 '22 at 17:54
  • Java Validation via Hibernate Validators is completely customizable and the OP can write as many custom validators as she'll need. You can validate across multiple fields, make database calls, what ever is needed. We have done all of those and more in our code base. It does come at the cost of learning another new framework, but I personally believe that cost is normally well worth it. – hooknc Mar 15 '22 at 18:02
  • This framework looks interesting. I went through that link you gave. But somehow I still prefer AOP, may be I am biased, but in this example link the model is cluttered with annotations and is not looking clean. Validation is a cross cutting concern and in my personal opinion it should be separated out from business logic or from models. – Cozimetzer Mar 15 '22 at 18:11
  • To be clear, typically the model that is annotated is a model from the MVC design pattern and is not a domain model object. I feel that using domain model objects in views as form backing objects via command objects is a bad idea. Therefore, there shouldn't be any validation annotations in your (domain) model. – hooknc Mar 15 '22 at 18:43