-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
54 changed files
with
269 additions
and
282 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import Decorator from ".."; | ||
import ValidationMetaService from "../../reflection/service/impl/reflection.service.validation"; | ||
|
||
namespace DecoratorService { | ||
/** | ||
* Creates a new decorator using the provided supplier function. | ||
* | ||
* @typeParam T - The type of the value being validated. | ||
* | ||
* @param supplier - A function that defines the behavior of the decorator. It takes the property name, an instance of `ValidationMetaService`, and the decorator context. | ||
* | ||
* @returns A decorator function that can be applied to class properties. | ||
* | ||
* @remarks | ||
* This function serves as a factory for creating new decorators. It uses dependency injection to get an instance of `ValidationMetaService`. | ||
* | ||
* The function handles both TypeScript's Stage 2 decorators and the current decorators. It determines the stage based on the type of the `context` parameter. | ||
* | ||
* The `supplier` function is responsible for adding any necessary metadata or validation logic via the `ValidationMetaService`. | ||
* | ||
* @example | ||
* ```typescript | ||
* const MyDecorator = makeDecorator<number>((key, metaService, context) => { | ||
* metaService.addValidator(key, (value) => value > 0); | ||
* }); | ||
* | ||
* class MyClass { | ||
* //@MyDecorator | ||
* public myValue: number; | ||
* } | ||
* ``` | ||
*/ | ||
export function create<T>( | ||
supplier: Decorator.Supplier<T> | ||
): Decorator.Instance<T> { | ||
return function (target, context) { | ||
const isStage2 = typeof context === "string"; | ||
const nameEval = isStage2 ? context : context.name; | ||
const strategyEval = isStage2 ? target.constructor : context; | ||
const contextEval = isStage2 ? { name: context, metadata: {} } : context; | ||
const metaService = ValidationMetaService.inject(strategyEval); | ||
supplier(nameEval, metaService, contextEval as any); | ||
}; | ||
} | ||
} | ||
|
||
export default DecoratorService; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import Decorator from ".."; | ||
import Validation from "../../types/namespace/validation.namespace"; | ||
import DecoratorService from "./decorator.service"; | ||
|
||
namespace ValidatorService { | ||
/** | ||
* Creates a new validator function using the provided validation builder options. | ||
* | ||
* @typeParam T - The type of the value being validated. | ||
* | ||
* @param groups - An array of group names that this validator belongs to. Validators in the same group can be executed together. | ||
* @param isValid - A function that performs the actual validation logic. It takes a value of type `T` and returns a boolean indicating whether the value is valid. | ||
* | ||
* @returns A decorator function that can be applied to class properties to add the validation logic. | ||
* | ||
* @remarks | ||
* This function leverages the `makeDecorator` function to create a new decorator. | ||
* It uses the `validationMetaService` to add the new validator to the metadata for the property it decorates. | ||
* | ||
* @example | ||
* ```typescript | ||
* const IsPositive = ValidatorService.create<number>({ | ||
* groups: ['group1'], | ||
* isValid: (value) => value > 0 | ||
* }); | ||
* | ||
* class MyClass { | ||
* //@IsPositive | ||
* public myValue: number; | ||
* } | ||
* ``` | ||
*/ | ||
export function create<T>({ | ||
groups, | ||
isValid, | ||
}: Validation.Builder<T>): Decorator.Instance<T> { | ||
return DecoratorService.create<T>((key, validationMetaService) => | ||
validationMetaService.addValidator(key, isValid, groups) | ||
); | ||
} | ||
} | ||
|
||
export default ValidatorService; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/core/src/reflection/service/impl/reflection.service.validation.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.