Skip to content

Commit

Permalink
πŸ”„ [chore]: Automated commit
Browse files Browse the repository at this point in the history
  • Loading branch information
brunotot committed Sep 27, 2023
1 parent 93dc516 commit ba6431a
Show file tree
Hide file tree
Showing 54 changed files with 269 additions and 282 deletions.
8 changes: 5 additions & 3 deletions packages/core/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import makeValidator from "./src/decorators/decorator.facade";
import Decorator from "./src/decorators";
import DecoratorService from "./src/decorators/service/decorator.service";
import ValidatorService from "./src/decorators/service/validator.service";
import Localization from "./src/localization";
import CacheMap from "./src/models/cache.map";
import EntityProcessor from "./src/reflection/models/entity.processor";
Expand Down Expand Up @@ -33,7 +35,6 @@ import PrimitiveArrayStrat, {
import PrimitiveStrat from "./src/reflection/strategy/impl/primitive.strategy";
import ValidationStrategy from "./src/reflection/strategy/strategy";
import TdvCore from "./src/types";
import Decorator from "./src/types/namespace/decorator.namespace";
import Validation from "./src/types/namespace/validation.namespace";
import Class from "./src/types/validation/class.type";
import DetailedErrors from "./src/types/validation/detailed-errors.type";
Expand All @@ -53,6 +54,7 @@ export type {
CacheMap,
Class,
Decorator,
DecoratorService,
DescriptorProps,
DetailedErrors,
Errors,
Expand All @@ -76,6 +78,7 @@ export type {
StrategyOptional,
TdvCore,
Validation,
ValidatorService,
};

export {
Expand All @@ -90,6 +93,5 @@ export {
ValidationMetaService,
ValidationStrategy,
getClassFieldNames,
makeValidator,
validators,
};
39 changes: 0 additions & 39 deletions packages/core/src/decorators/decorator.facade.ts

This file was deleted.

43 changes: 0 additions & 43 deletions packages/core/src/decorators/decorator.factory.ts

This file was deleted.

13 changes: 8 additions & 5 deletions packages/core/src/decorators/decorator.utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Decorator from ".";
import Localization from "../localization";
import $ from "../types";
import Decorator from "../types/namespace/decorator.namespace";
import Objects from "../types/namespace/objects.namespace";
import Validation from "../types/namespace/validation.namespace";

Expand Down Expand Up @@ -61,9 +61,12 @@ export function extractGroups<T extends object>(
*
* @returns `true` if the value is null or undefined, or the result of `isValid` otherwise.
*/
export function evaluateNullableValidity<T>(
object: $.Objects.Optional<T>,
isValid: (value: T) => boolean
export function isValidNullable<T>(
nullableValue: T,
isValid: (nonNullableValue: NonNullable<T>) => boolean
) {
return !$.Objects.hasValue(object) ? true : isValid(object);
return (
!$.Objects.hasValue(nullableValue) ||
isValid(nullableValue as NonNullable<T>)
);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import ValidationMetaService from "../../reflection/service/impl/reflection.service.validation";
import Validation from "./validation.namespace";
import ValidationMetaService from "../reflection/service/impl/reflection.service.validation";
import Validation from "../types/namespace/validation.namespace";

/**
* @namespace Decorator
Expand All @@ -14,7 +14,7 @@ namespace Decorator {
* @description
* Type definition for a decorator function.
*/
export type Type<T = unknown> = (
export type Instance<T = unknown> = (
target: any,
context: Decorator.Context<T>
) => void;
Expand Down Expand Up @@ -86,8 +86,4 @@ namespace Decorator {
};
}

/**
* @description
* The default export for the `Decorator` namespace.
*/
export default Decorator;
47 changes: 47 additions & 0 deletions packages/core/src/decorators/service/decorator.service.ts
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;
43 changes: 43 additions & 0 deletions packages/core/src/decorators/service/validator.service.ts
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;
28 changes: 14 additions & 14 deletions packages/core/src/localization/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,38 @@ namespace Localization {
*/
export type Messages = Record<Locale, Record<string, string>>;

let locale: Localization.Locale = "en";
let locale: Locale = "en";

export function getLocale(): Localization.Locale {
export function getLocale(): Locale {
return locale;
}

export function setLocale(localeValue: Localization.Locale) {
export function setLocale(localeValue: Locale) {
locale = localeValue;
}

export namespace Resolver {
const DEFAULT_CONFIGURER: (
locale: Localization.Locale,
export type ResolverConfigurer = (
locale: Locale,
message: string
) => string = (_locale, message) => message;
) => string;

let configurer: (locale: Localization.Locale, message: string) => string =
DEFAULT_CONFIGURER;
const DEFAULT_CONFIGURER: ResolverConfigurer = (_, message) => message;

export function configure(
handler?: (locale: Localization.Locale, message: string) => string
) {
let configurer: ResolverConfigurer = DEFAULT_CONFIGURER;

export function configure(handler?: ResolverConfigurer) {
configurer = handler ?? DEFAULT_CONFIGURER;
}

export function resolve(locale: Locale, message: string) {
try {
return configurer(locale, message);
} catch (error) {
throw new Error(
`An error occurred while resolving \"${message}\" for locale \"${locale}\". To fix, check your Localization.Resolver.configure() implementation.\n\n${error}`
);
const title = `An error occurred while resolving \"${message}\" for locale \"${locale}\".`;
const descr = `To fix, check your Localization.Resolver.configure() implementation.`;
const stacktrace = `\n\n${error}`;
throw new Error(`${title} ${descr} ${stacktrace}`);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Decorator from "../../../decorators";
import { extractGroups } from "../../../decorators/decorator.utils";
import Decorator from "../../../types/namespace/decorator.namespace";
import Validation from "../../../types/namespace/validation.namespace";
import Class from "../../../types/validation/class.type";
import ReflectionDescriptor from "../../models/reflection.descriptor";
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/reflection/service/reflection.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Decorator from "../../types/namespace/decorator.namespace";
import Decorator from "../../decorators";
import Class from "../../types/validation/class.type";

/**
Expand Down
6 changes: 3 additions & 3 deletions packages/core/validators/any/Falsy.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import makeValidator from "../../src/decorators/decorator.facade";
import Decorator from "../../src/decorators";
import {
extractGroups,
extractMessage,
} from "../../src/decorators/decorator.utils";
import ValidatorService from "../../src/decorators/service/validator.service";
import TranslationService from "../../src/localization/service/translation.service";
import $ from "../../src/types";
import Decorator from "../../src/types/namespace/decorator.namespace";

/**
* Creates a validator decorator for falsy value validation.
Expand All @@ -32,7 +32,7 @@ import Decorator from "../../src/types/namespace/decorator.namespace";
export default function Falsy<T extends $.Objects.Optional>(
props?: Decorator.PartialProps
) {
return makeValidator<T>({
return ValidatorService.create<T>({
groups: extractGroups(props),
isValid: (value, _, locale) => ({
key: "Falsy",
Expand Down
6 changes: 3 additions & 3 deletions packages/core/validators/any/Required.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import makeValidator from "../../src/decorators/decorator.facade";
import Decorator from "../../src/decorators";
import {
extractGroups,
extractMessage,
} from "../../src/decorators/decorator.utils";
import ValidatorService from "../../src/decorators/service/validator.service";
import TranslationService from "../../src/localization/service/translation.service";
import $ from "../../src/types";
import Decorator from "../../src/types/namespace/decorator.namespace";

/**
* Creates a validator decorator for required value validation.
Expand All @@ -32,7 +32,7 @@ import Decorator from "../../src/types/namespace/decorator.namespace";
export default function Required<T extends $.Objects.Optional>(
props?: Decorator.PartialProps
) {
return makeValidator<T>({
return ValidatorService.create<T>({
groups: extractGroups(props),
isValid: (value, _, locale) => ({
key: "Required",
Expand Down
4 changes: 2 additions & 2 deletions packages/core/validators/any/Rule.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import makeValidator from "../../src/decorators/decorator.facade";
import ValidatorService from "../../src/decorators/service/validator.service";
import Validation from "../../src/types/namespace/validation.namespace";

/**
Expand Down Expand Up @@ -33,7 +33,7 @@ export default function Rule<T>(
groups?: Validation.GroupsParam;
}
) {
return makeValidator<T>({
return ValidatorService.create<T>({
isValid: "isValid" in props ? props.isValid : props,
groups: "isValid" in props ? props.groups : [],
});
Expand Down
Loading

0 comments on commit ba6431a

Please sign in to comment.