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

Implement Validate Decorator for Model Validation #23

Open
elersong opened this issue Jul 13, 2024 · 0 comments
Open

Implement Validate Decorator for Model Validation #23

elersong opened this issue Jul 13, 2024 · 0 comments
Labels
enhancement New feature or request from_willyovale An issue described in original project, but never implemented good first issue Good for newcomers

Comments

@elersong
Copy link
Owner

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

  1. Manually add validation checks in repository functions.
  2. 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

  1. 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);
      };
    }
  2. Unit Tests:

    • Create unit tests to validate the functionality of the Validate decorator.
    import { Validate } from './validate.decorator';
    
    class TestClass {
      @Validate
      async someMethod(item: any) {
        // method logic
      }
    }
    
    test('should throw validation errors', async () => {
      const instance = new TestClass();
      instance.config = { validateModels: true };
    
      await expect(instance.someMethod({})).rejects.toThrow(ValidationError);
    });
  3. Replace Existing Validation Code:

    • Refactor existing repository functions to use the Validate decorator instead of manual validation checks.
  4. Ensure Usage of Validate Decorator:

    • Ensure all functions requiring validation are decorated with @Validate.

Example Implementation

@Collection()
class User {
  @IsString()
  id: string;

  @IsString()
  display_name: string;

  @IsString()
  username: string;
}

// Repository instance
class UserRepository {
  @Validate
  async create(user: User) {
    // create logic
  }

  @Validate
  async update(user: User) {
    // update logic
  }
}

Original Issue

@elersong elersong added enhancement New feature or request good first issue Good for newcomers from_willyovale An issue described in original project, but never implemented labels Jul 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request from_willyovale An issue described in original project, but never implemented good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

1 participant