So I am debating what approach to follow. My scenario is : Will receive a POJO which I need to validate ( REST end point ) Now I can explicitly validate specific fields ( which I know / are stated in requirements that they are mandatory ) OR should I define the fields in a db table and define which are mandatory vs not and then read this table one time and then based on what is configured in db drive my validations .

Would the second config driven approach be an overkill ? ( I guess the answer would be 'depending' on the number of times such scenarios can arise where we would need to change the rules of what is mandatory )

apologies if I am not making sense


What is your goal?

  • Validating input.
  • Writing a quirky programming language to validate input.

If your goal is to just validate the input, I would hard-code it. It is unlikely that your service running in production will suddenly gain an appreciation for a different request format. That would indeed be quite startling.

If your goal is to permit custom validation, such as validating a web-survey. Then yes you'll need that quirky programming language. Just be aware that it is a programming language, that it is scripting your application, because it is changing your application's behaviour.

  • So how complex are these rules going to be?
  • Will you support arithmetic, branching, looping? Ouch a Turing complete language.
  • How will you debug those rules?
  • Error handling?

The rabbit hole goes on and on. If you do need this configurability split the validation into two tiers.

  1. Format validation - Hard code this as the format should not change.
  2. Content Validation - Hard Code the known and immutable parts, invoke a custom script to validate the rest.

If at all possible locate a scripting language that fits well into your code base, has a nice sandbox, a good syntax editor, and debugging tools. The custom validation script is passed a copy of the request object, and returns a true|false. You can store that script in a database row.

| improve this answer | |
  • thanks for your input . My goal is only to validate and it is not complex - basic validations such as mandatory fields , no branching looping . Debug and error handling need to think of - dont have an answer yet . Based on your answer I am leaning towards a hard code solution – akila Apr 3 '19 at 0:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.