#spring api validation

1 messages · Page 1 of 1 (latest)

thorny laurel
#

where does validation belong in a spring rest api?

i have my dtos that get converted to my internal objects which has business logic

i use annotations to so basic validation on my dtos, but where should more complex validation go?

should my dtos be validated before converting to my internal objects?

do i need preconditions in my internal objects as well to ensure they're constructed with the correct constraints?

verbal falconBOT
#

<@&1004656351647117403> please have a look, thanks.

#

Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#

the DTOs before they are converted to internal objects. This basic validation can include checking for required fields, data types, and length constraints using annotations like @NotNull, @Size, @Pattern, etc.

For more complex business logic validation that involves multiple fields or interactions between objects, it is recommended to perform this validation in the service layer of your application. This ensures that the validation logic is separate from the presentation layer and can be reused across different controllers or endpoints.

You can create custom validation annotations and validators in Spring to handle these more complex validations. These custom validators can be applied at the service layer before processing the request further.

Overall, it is important to have a combination of basic validation using annotations on DTOs and more complex business logic validation in the service layer to ensure data integrity and consistency in your Spring REST API.

lost linden
#

there is a module for validations in spring

#

spring-boot-starter-validation