How typestack/class-validator works
Overall Project Type and Purpose
• This is not a full-stack application but a utility library focused on validation.
• Its purpose is to provide a rich set of validation decorators and functions that developers apply to model classes in TypeScript/JavaScript.
• The library supports custom validations, conditional validations, inheritance of decorators, and even integration with a service container.
Main Architectural Components and Their Locations
a. Public API and Entry Points
– Files: src/index.ts, register-decorator.ts, and the container (src/container.ts)
– These expose the core functions like validate, validateOrReject, and the custom decorator registration.
– Represent this as the “Public API Layer” which client code calls.
b. Decorators and Custom Validation Definitions
– Located in src/decorator (and its subfolders: common, array, date, number, string, typechecker, etc.)
– Each set of decorators encapsulates specific validation logic (e.g., IsEmail, MinLength, Contains, etc.).
– Also note the custom validators in the sample directory (e.g., sample4-custom-validator, sample6-custom-decorator) serve as examples for extending this mechanism.
c. Metadata Storage and Transformation
– Located in src/metadata and src/validation-schema.
– These modules are responsible for collecting and storing validation metadata that is registered through decorators.
– The “Metadata Storage” component holds information such as the constraints, target class details, and validation arguments.
d. Validation Engine
– Located in src/validation (which includes files like ValidationExecutor.ts, ValidationError.ts, and Validator.ts).
– This component is responsible for executing the validation rules, processing the metadata, and returning structured errors.
– Represent this as the “Validation Engine” that receives a class instance (populated with constraints from decorators) and then iterates through the metadata.
e. Utility Modules
– Located in src/utils.
– These components provide helper functions such as converting values to arrays, checking promises, and other generic utilities.
f. Test and Sample Modules
– The sample directory (sample/sample1-simple-validation, sample/sample2-using-groups, etc.) contains concrete usage examples.
– The test folder contains automated tests for various functionalities.
Relationships and Data Flow
• The validation engine then consults the “Metadata Storage” that was populated by the decorator modules.
• If a service container is used (via src/container.ts and the integration with typedi), illustrate this dependency injection feeding into the Validation Engine and custom validators.
• Interactions from custom decorators (and custom validators) that plug into the Validation Engine via the public API.
Important Architectural Patterns & Design Principles
• Emphasis on metadata registration via decorators.
• Separation of concerns: the validation logic is separated from metadata management and actual execution.
• Extensibility: separated modules to allow developers to define custom decorators and constraints without altering core logic.
• Reusability: utility modules and a clearly defined API allow the library to be integrated into various ecosystems.