0

I have around 500 fields where I need to check values coming for the fields from json in spring rest endpoint are having valid values

ex:

Class Customer {

    long id;

    String gender; // acceptable values: MALE || FEMALE 

    int validated; // acceptable values: 0 || 1

    String plan;// acceptable values: PLAN1 || PLAN2 || PLAN3 || PLAN4

}

it will be harder to write a custom method to check each field, is there any generic approach where we dont need to write method validations for each field or class.

I have tried this Java String validation using enum values and annotation but this is not throwing error when the value is not in the acceptable values. Basically it should be throwing exception if value is other than acceptable value

Vassopoli
  • 41
  • 7
  • I think you simply want to test your Json against [a Json Schema](https://www.baeldung.com/introduction-to-json-schema-in-java), throwing an exception if the validation collects one or multiple messages (with the exception containing the validation result). P.s. I have been using [this validator](https://github.com/networknt/json-schema-validator) and it's very powerful. – Matteo NNZ Jun 14 '22 at 21:37
  • why don't you just use real enum value for string? – Ferry Jun 14 '22 at 22:22

0 Answers0