Fireorm supports validation by leveraging class-validator. The current implementation requires validation to be manually checked in each repository function. To improve code maintainability and readability, a Validate decorator should be created to handle this validation automatically.
Steps to Reproduce
Manually add validation checks in repository functions.
Notice the repeated validation code and the need for a more elegant solution.
Expected Behavior
A Validate decorator should handle model validation, reducing code repetition and improving maintainability.
Actual Behavior
Validation checks are manually implemented in repository functions, leading to repeated code and potential for errors.
Acceptance Criteria
Create a Validate decorator that checks if validation should be performed and returns a ValidationError.
Create unit tests to ensure the decorator functions correctly.
Replace the existing validation code with the new decorator.
Ensure each function that requires validation uses the Validate decorator.
Additional Context
October 17, 2020: Initial proposal to implement a Validate decorator.
February 23, 2021: Mention of a feature to optionally allow validatorOptions of class-validator.
Proposed API Changes
Create Validate Decorator:
Develop a Validate decorator to handle model validation.
import { validate } from 'class-validator';
import { ValidationError } from 'class-validator';
function Validate(target: any, propertyName: string, descriptor: TypedPropertyDescriptor<Function>) {
const method = descriptor.value;
descriptor.value = async function (...args: any[]) {
if (this.config.validateModels) {
const errors = await validate(args[0]);
if (errors.length) {
throw new ValidationError(errors);
}
}
return method.apply(this, args);
};
}
Unit Tests:
Create unit tests to validate the functionality of the Validate decorator.
Description
Fireorm supports validation by leveraging class-validator. The current implementation requires validation to be manually checked in each repository function. To improve code maintainability and readability, a
Validate
decorator should be created to handle this validation automatically.Steps to Reproduce
Expected Behavior
A
Validate
decorator should handle model validation, reducing code repetition and improving maintainability.Actual Behavior
Validation checks are manually implemented in repository functions, leading to repeated code and potential for errors.
Acceptance Criteria
Validate
decorator that checks if validation should be performed and returns aValidationError
.Validate
decorator.Additional Context
Validate
decorator.validatorOptions
of class-validator.Proposed API Changes
Create Validate Decorator:
Validate
decorator to handle model validation.Unit Tests:
Validate
decorator.Replace Existing Validation Code:
Validate
decorator instead of manual validation checks.Ensure Usage of Validate Decorator:
@Validate
.Example Implementation
Original Issue