Logo

dev-resources.site

for different kinds of informations.

Validation in Spring REST Framework (SRF)

Published at
12/21/2024
Categories
spring
rest
validation
springboot
Author
birddevelper
Categories
4 categories in total
spring
open
rest
open
validation
open
springboot
open
Author
12 person written this
birddevelper
open
Validation in Spring REST Framework (SRF)

Validation is a critical aspect of API development to ensure data integrity and early invalid requests detection. In this post, we’ll learn how to implement field-level validation and custom validation logic in Spring REST Framework (SRF) using Dto objects during deserialization.

1. Field-Level Validation with Annotations

The SRF provides the @FieldValidation annotation for quick and declarative validation on DTO fields. You can enforce rules such as minimum/maximum length, numeric ranges, and more.

Example: Basic Field Validation

import io.github.nikanique.springrestframework.annotation.FieldValidation;
import io.github.nikanique.springrestframework.annotation.ReadOnly;
import io.github.nikanique.springrestframework.dto.Dto;
import lombok.Data;

@Data
public class StudentDto extends Dto {

    @ReadOnly
    private Long id;

    @FieldValidation(minLength = 2, maxLength = 50)
    private String name;

    @FieldValidation(allowNull = false)
    private String nationality;

    @FieldValidation(minValue = 1, maxValue = 12)
    private Integer grade;
}

Enter fullscreen mode Exit fullscreen mode

This declarative validation automatically applies during deserialization. If the incoming request fails these validations, an error response will be generated.

The complete list of supported attributes and their functionality:

1. maxLength and minLength

These attributes validate the length of a string field.

  • maxLength: The maximum allowed length of the string (default: Integer.MAX_VALUE, meaning no limit).
  • minLength: The minimum required length of the string (default: -1, meaning no restriction).

Example

@FieldValidation(minLength = 3, maxLength = 50)
private String name;
Enter fullscreen mode Exit fullscreen mode
  • Ensures the name field has a length between 3 and 50 characters.
  • If name is shorter than 3 or longer than 50, an error is returned.

2. blank

This attribute validates whether the field can be blank.

  • blank = true: Allows blank values.
  • blank = false: Disallows blank values (default).

Example

@FieldValidation(blank = false)
private String description;
Enter fullscreen mode Exit fullscreen mode
  • Ensures the description field cannot be blank.

3. minValue and maxValue

These attributes validate the numeric range of a field.

  • minValue: The minimum allowable numeric value (default: Integer.MIN_VALUE).
  • maxValue: The maximum allowable numeric value (default: Integer.MAX_VALUE).

Example

@FieldValidation(minValue = 1, maxValue = 12)
private Integer grade;
Enter fullscreen mode Exit fullscreen mode
  • Ensures the grade field contains a value between 1 and 12 (inclusive).

4. nullable

This attribute specifies whether the field can accept null values.

  • nullable = true: Allows null values (default).
  • nullable = false: Disallows null values.

Example

@FieldValidation(nullable = false)
private String nationality;
Enter fullscreen mode Exit fullscreen mode
  • Ensures the nationality field cannot be null.

5. minDate and maxDate

These attributes validate whether a date field falls within a specific range. It should be provided in yyyy-MM-dd or yyyy-MM-dd HH:mm:ss formats.

  • minDate: The earliest allowable date (default: "").
  • maxDate: The latest allowable date (default: "").

Example

@FieldValidation(minDate = "2000-01-01", maxDate = "2024-12-31")
private String dateOfBirth;
Enter fullscreen mode Exit fullscreen mode
  • Ensures the dateOfBirth is between January 1, 2000 and December 31, 2024.

Full Example Using All Field Validation Options:

Here’s an example DTO that uses various @FieldValidation attributes to validate input fields:

import io.github.nikanique.springrestframework.annotation.FieldValidation;
import io.github.nikanique.springrestframework.dto.Dto;
import lombok.Data;

@Data
public class StudentDto extends Dto {

    @FieldValidation(nullable = false, minLength = 2, maxLength = 50)
    private String name; // Must be between 2 and 50 characters, and cannot be null.

    @FieldValidation(blank = false)
    private String nationality; // Cannot be blank (empty or whitespace).

    @FieldValidation(minValue = 1, maxValue = 12)
    private Integer grade; // Must be between 1 and 12.

    @FieldValidation(minDate = "2000-01-01", maxDate = "2024-12-31")
    private String dateOfBirth; // Must be within the specified date range.
}
Enter fullscreen mode Exit fullscreen mode

If a request contains invalid data, SRF will return a structured response with detailed error messages. For example, if the name is too short and grade is out of range, the response will look like this:

Request (Invalid Data)

{
    "name": "A",
    "nationality": "",
    "grade": 13,
    "dateOfBirth": "1990-12-31"
}
Enter fullscreen mode Exit fullscreen mode

Response (Validation Errors)

{
    "errors": {
        "name": "Length must be between 2 and 50 characters.",
        "nationality": "Cannot be blank.",
        "grade": "Value must be between 1 and 12.",
        "dateOfBirth": "Date must be between 2000-01-01 and 2024-12-31."
    }
}
Enter fullscreen mode Exit fullscreen mode

Notice that fields marked as @ReadOnly will not be validated because they are excluded from input.

2. Overriding the validate Method for Custom Logic

For more complex scenarios, you can combine these annotations with custom validation logic by overriding the validate method in your DTO. Here’s an example of overriding the validate method of a Dto:

import io.github.nikanique.springrestframework.annotation.FieldValidation;
import io.github.nikanique.springrestframework.dto.Dto;
import lombok.Data;

import java.util.Map;

@Data
public class ProductDTO extends Dto {

    @ReadOnly
    private Integer id;

    private String model;

    @FieldValidation(minLength = 3)
    private String company;

    @Override
    public Map<String, String> validate(Boolean raiseValidationError) throws Throwable {

        Map<String, String> validationErrors = super.validate(false);

        if (company != null && company.equalsIgnoreCase("BMW")) {
            validationErrors.put("company", "Company cannot be BMW.");
        }

        // Raise validation error if specified
        if (!validationErrors.isEmpty() && raiseValidationError) {
            throw new ValidationException(validationErrors);
        }

        return validationErrors;
    }
}

Enter fullscreen mode Exit fullscreen mode

The super.validate(false) calls the default validation logic for @FieldValidation annotations to apply base validation logic for standard validations. Here I passed false to called validate method to let the overridden method to send complete validation error in the exception message.

Conclusion

The @FieldValidation annotation in Spring REST Framework (SRF) provides a powerful way to enforce input validation. It simplifies validation for common rules like length, range, nullability, and date constraints. For more complex scenarios, you can combine these annotations with custom validation logic by overriding the validate method in your DTO.

Also Read:

validation Article's
30 articles in total
Favicon
"yup" is the new extra virgin olive oil
Favicon
JavaScript Email Validation Regex: Ensuring Accuracy in User Inputs
Favicon
Simplifying JSON Validation with Ajv (Another JSON Validator)
Favicon
Dynamic string validation using go's text/template package
Favicon
Practical Email Validation Using JavaScript: Techniques for Web Developers
Favicon
Validation in Spring REST Framework (SRF)
Favicon
Terraform Validation Rules: Best Practices & Examples
Favicon
Zod for TypeScript Schema Validation: A Comprehensive Guide
Favicon
AI Image Validation Solutions: A Game-Changer for Quality Control
Favicon
Transform zod error into readable error response
Favicon
â›”Stop Excessive Validation, you are ruining my life hack!
Favicon
zod vs class-validator & class-transformer
Favicon
Animated Login Form with Validation Using HTML, CSS & JavaScript
Favicon
How to work with regular expressions
Favicon
User Form Validation in HTML CSS and JavaScript | JavaScript Form Validation
Favicon
Simplify Form Validation with FormGuardJS: A Lightweight and Flexible Solution
Favicon
moment.js Alternative - Luxon
Favicon
Validating REST requests in a NestJS application and displaying errors in Angular application forms
Favicon
Implement data validation in FastAPI
Favicon
The Definitive Guide to the Constraint Validation API
Favicon
Env-Core: An Easy Way to Validate Environment Variables in Node.js Projects
Favicon
File-Type Validation in Multer is NOT SAFE🙃
Favicon
Guaranteed GxP Compliance: Mastering Validation Testing Strategies
Favicon
Assuring Excellence: The Crucial Benefits of GxP Validation Testing
Favicon
7 Old-School Practices in HTML Should Be Avoided
Favicon
Implement data validation in Spring Boot
Favicon
Implement data validation in Express
Favicon
An easy way to validate DTO's using Symfony attributes
Favicon
Implement data validation in .NET
Favicon
Mastering Data Validation in NestJS: A Complete Guide with Class-Validator and Class-Transformer

Featured ones: