-2

I'm building a REST api which take some inputs like id,customer_id,firstname,lastname,contact,email,updated_time,created_time. Before processing these inputs, i have added validations to check if

  • id and customer is number
  • firstname is not a null and string,
  • created and updated time are not null and empty string
  • email id is in valid format
  • contact field is number
  • check for email and contact already exist in database

And few more validations also made. But it made the code redundant and reduced code readability and maintainability. Is it a good practice to include these much validations in a single api call?

Tried making a common structure for api response. But I want to produce different error response in each conditions.

1 Answer 1

1

I don't know what programming language you're using, but most of this can be handled by a good MVC framework.

For example using Spring MVC (Java): https://www.baeldung.com/spring-boot-bean-validation

In that example, the author returns an HTTP response with status 400 for errors, and returns a JSON structure with the error message per field in the body.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm using nodejs. Currently almost 9 if conditions and corresponding error response have been made. As I made unit test, these validations are required.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.