Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Request Validation via GrpcValidator Annotation and Custom Validators #1176

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Power-PP
Copy link

This pull request introduces a structured solution for handling request validation in a gRPC service. The solution involves decoupling the validation logic from the business logic layer to ensure that requests are validated before reaching the service layer. The key components of this solution include a custom annotation, a validator discovery mechanism, and a base validator.

Key Features:
GrpcValidator Annotation:
The GrpcValidator annotation associates a gRPC method with a specific request class and its corresponding validator.
It allows specifying the validator class and the validation method to be invoked for that request type.
This separation allows for clean and maintainable code by keeping validation logic isolated from the business logic of the service.
Example Usage:

@GrpcValidator(requestClass = MyRequest.class, validatorClass = MyRequestValidator.class, validatorMethod = "validateRequest")
public void myGrpcMethod(MyRequest request) {
// gRPC method implementation
}

GrpcValidatorDiscoverer:
This component is responsible for scanning the application during server startup to discover all the GrpcValidator annotations.
It registers the relationship between request classes and their corresponding validation methods, storing them for easy lookup at runtime. This enables dynamic invocation of custom validation methods for each request type.

Base Validator:
A base class that handles common validation tasks such as checking for required fields, regex pattern matching, and other basic validation rules. For custom validation, the base validator invokes the appropriate validation method discovered by the GrpcValidatorDiscoverer.

Flow:

  • During server startup, the GrpcValidatorDiscoverer scans and registers all methods annotated with @GrpcValidator.
  • Upon receiving a request, the system checks if a custom validator is configured for the request type and invokes it accordingly.
  • If validation fails, an exception (e.g., GrpcException) is thrown, and the client receives an appropriate error response.
  • If validation succeeds, the request proceeds to the business logic of the service method.

Benefits:

  • Separation of Concerns: Validation logic is decoupled from the business logic layer, resulting in cleaner and more maintainable code.
  • Reusability: The solution allows you to easily add new validators for different request types without changing the service methods.
  • Extensibility: You can easily extend the system to support more complex or custom validation rules by simply adding new validation methods in the corresponding validator classes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant