Skip to content

Commit

Permalink
♻️ [refactor]: Rewrite decorator metadata props
Browse files Browse the repository at this point in the history
  • Loading branch information
brunotot committed Jan 26, 2024
1 parent a05de7d commit 3a345d7
Show file tree
Hide file tree
Showing 249 changed files with 1,143 additions and 618 deletions.
2 changes: 2 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
"@typescript-eslint/no-non-null-assertion": "off",
"no-proto": "off",
"valid-typeof": "off",
"@typescript-eslint/no-empty-interface": "off",
"@typescript-eslint/no-unused-vars": "warn",
"import/no-cycle": [
"error",
{
Expand Down
2 changes: 1 addition & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"arrowParens": "avoid",
"jsxSingleQuote": false,
"printWidth": 120,
"printWidth": 100,
"semi": true,
"singleQuote": false,
"tabWidth": 2
Expand Down
4 changes: 2 additions & 2 deletions packages/core/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions packages/core/src/decorators/data/structural/attribute.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { type FieldDecorator, createFieldDecorator } from "@decorators/factory/forField/createFieldDecorator";
import {
createFieldDecorator,
type FieldDecorator,
} from "@decorators/factory/forField/createFieldDecorator";
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { PrimitiveTypeOverride } from "@overrides";
import { type Objects, type Types } from "@utilities";

Expand Down Expand Up @@ -59,8 +63,10 @@ import { type Objects, type Types } from "@utilities";
* }
* }
*/
export function attribute<T extends Objects.Optional<object | object[]>>(clazz: Types.Class<any>): FieldDecorator<T> {
return createFieldDecorator<any>((meta, name) => {
export function attribute<Value extends Objects.Optional<object | object[]>, Class>(
clazz: Types.Class<any>
): FieldDecorator<Value, Class> {
return createFieldDecorator<Value, Class>((meta, name) => {
meta.getUntypedDescriptor(name).thisClass = clazz;
});
}
13 changes: 8 additions & 5 deletions packages/core/src/decorators/data/structural/foreach.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { type FieldDecorator, createFieldDecorator } from "@decorators/factory/forField/createFieldDecorator";
import {
createFieldDecorator,
type FieldDecorator,
} from "@decorators/factory/forField/createFieldDecorator";
import { type Arrays, type Types } from "@utilities";

/**
Expand All @@ -15,10 +18,10 @@ import { type Arrays, type Types } from "@utilities";
* }
* ```
*/
export function foreach<T extends NonNullable<Types.ArrayType | (() => Types.ArrayType)>>(
...validators: Array<FieldDecorator<Arrays.getArrayType<T>>>
): FieldDecorator<T> {
return createFieldDecorator<T>((meta, property, context) => {
export function foreach<T extends NonNullable<Types.ArrayType | (() => Types.ArrayType)>, Class>(
...validators: Array<FieldDecorator<Arrays.getArrayType<T>, Class>>
): FieldDecorator<T, Class> {
return createFieldDecorator<T, Class>((meta, property, context) => {
const validationProcessor = meta.getUntypedDescriptor(property);
validationProcessor.thisDefault = [];
validators.forEach(validator => {
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/decorators/data/structural/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export * from "./attribute";
export * from "./foreach";
export * from "./validateClassIf";
export * from "./validateFieldIf";
14 changes: 14 additions & 0 deletions packages/core/src/decorators/data/structural/validateClassIf.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {
createClassDecorator,
type ClassDecorator,
} from "@decorators/factory/forClass/createClassDecorator";
import { type DecoratorValidateIf } from "@decorators/helper";
import { type Types } from "@utilities";

export function validateClassIf<Class extends Types.Class>(
validateIf: DecoratorValidateIf<Types.UnwrapClass<Class>>
): ClassDecorator<Class> {
return createClassDecorator<Class>(meta => {
meta.validateIf = validateIf;
});
}
13 changes: 13 additions & 0 deletions packages/core/src/decorators/data/structural/validateFieldIf.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {
createFieldDecorator,
type FieldDecorator,
} from "@decorators/factory/forField/createFieldDecorator";
import { type DecoratorValidateIf } from "@decorators/helper";

export function validateFieldIf<Value, Class>(
validateIf: DecoratorValidateIf<Class>
): FieldDecorator<Value, Class> {
return createFieldDecorator<Value, Class>((meta, name) => {
meta.getUntypedDescriptor(name).validateIf = validateIf;
});
}
15 changes: 8 additions & 7 deletions packages/core/src/decorators/data/validators/any/Required.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { DecoratorKeys } from "@decorators/data/validators/DecoratorKeys";
import { type FieldDecorator, createFieldValidator } from "@decorators/factory/forField";
import { type DecoratorOptions, buildGroupsProp, buildKeyProp, buildMessageProp } from "@decorators/helper";
import { createFieldValidator, type FieldDecorator } from "@decorators/factory/forField";
import { buildDecoratorMeta, buildKeyProp, buildMessageProp, type DecoratorOptions } from "@decorators/helper";
import { translate } from "@localization/service/TranslationService";
import { type Objects } from "@utilities";

/**
* Checks if a value is not `null`, `undefined`, `false`, an empty array, an empty string, or an invalid Date.
*
* @typeParam T - The type of the value.
*/
function isRequiredValid<T>(value: T | undefined): value is NonNullable<typeof value> {
function isRequiredValid<T>(value: T | undefined): boolean {
return !(
value === undefined ||
value === null ||
Expand Down Expand Up @@ -58,13 +57,15 @@ function isRequiredValid<T>(value: T | undefined): value is NonNullable<typeof v
* }
* ```
*/
export function Required<T extends Objects.Optional>(options?: DecoratorOptions): FieldDecorator<T> {
return createFieldValidator<T>(
export function Required<This, Value extends Objects.Optional>(
options?: DecoratorOptions<This, Value>
): FieldDecorator<This, Value> {
return createFieldValidator<This, Value>(
(value, _context, locale) => ({
key: buildKeyProp(options, DecoratorKeys.REQUIRED),
valid: isRequiredValid(value),
message: buildMessageProp(options, locale, translate(locale, DecoratorKeys.REQUIRED)),
}),
buildGroupsProp(options)
buildDecoratorMeta(options)
);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DecoratorKeys } from "@decorators/data/validators/DecoratorKeys";
import { type FieldDecorator, createFieldValidator } from "@decorators/factory/forField";
import { type DecoratorOptions, buildGroupsProp, buildKeyProp, buildMessageProp } from "@decorators/helper";
import { createFieldValidator, type FieldDecorator } from "@decorators/factory/forField";
import { buildDecoratorMeta, buildKeyProp, buildMessageProp, type DecoratorOptions } from "@decorators/helper";
import { translate } from "@localization/service/TranslationService";
import { Objects } from "@utilities";

Expand Down Expand Up @@ -59,13 +59,16 @@ function isArrayContainsValid<K, T extends K[]>(value: T, contains: K): boolean
* }
* ```
*/
export function ArrayContains<K, T extends K[]>(contains: K, options?: DecoratorOptions): FieldDecorator<T> {
return createFieldValidator<T>(
export function ArrayContains<This, Item, Value extends Item[]>(
contains: Item,
options?: DecoratorOptions<This>
): FieldDecorator<This, Value> {
return createFieldValidator<This, Value>(
(array, _context, locale) => ({
key: buildKeyProp(options, DecoratorKeys.ARRAY_CONTAINS),
valid: isArrayContainsValid(array, contains),
message: buildMessageProp(options, locale, translate(locale, DecoratorKeys.ARRAY_CONTAINS, contains)),
}),
buildGroupsProp(options)
buildDecoratorMeta(options)
);
}
12 changes: 7 additions & 5 deletions packages/core/src/decorators/data/validators/array/ArrayEmpty.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DecoratorKeys } from "@decorators/data/validators/DecoratorKeys";
import { type FieldDecorator, createFieldValidator } from "@decorators/factory/forField";
import { type DecoratorOptions, buildGroupsProp, buildKeyProp, buildMessageProp } from "@decorators/helper";
import { createFieldValidator, type FieldDecorator } from "@decorators/factory/forField";
import { buildDecoratorMeta, buildKeyProp, buildMessageProp, type DecoratorOptions } from "@decorators/helper";
import { translate } from "@localization/service/TranslationService";
import { Objects } from "@utilities";

Expand Down Expand Up @@ -58,13 +58,15 @@ function isArrayEmptyValid(array: any[]): boolean {
* }
* ```
*/
export function ArrayEmpty<K, T extends K[]>(options?: DecoratorOptions): FieldDecorator<T> {
return createFieldValidator<T>(
export function ArrayEmpty<This, Item, Value extends Item[]>(
options?: DecoratorOptions<This>
): FieldDecorator<This, Value> {
return createFieldValidator<This, Value>(
(array, _context, locale) => ({
key: buildKeyProp(options, DecoratorKeys.ARRAY_EMPTY),
valid: isArrayEmptyValid(array),
message: buildMessageProp(options, locale, translate(locale, DecoratorKeys.ARRAY_EMPTY)),
}),
buildGroupsProp(options)
buildDecoratorMeta(options)
);
}
16 changes: 8 additions & 8 deletions packages/core/src/decorators/data/validators/array/ArrayEvery.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DecoratorKeys } from "@decorators/data/validators/DecoratorKeys";
import { type FieldDecorator, createFieldValidator } from "@decorators/factory/forField";
import { type DecoratorOptions, buildGroupsProp, buildKeyProp, buildMessageProp } from "@decorators/helper";
import { createFieldValidator, type FieldDecorator } from "@decorators/factory/forField";
import { buildDecoratorMeta, buildKeyProp, buildMessageProp, type DecoratorOptions } from "@decorators/helper";
import { translate } from "@localization/service/TranslationService";
import { Objects } from "@utilities";

Expand Down Expand Up @@ -59,16 +59,16 @@ function isArrayEveryValid<K, T extends K[]>(array: T, predicate: Objects.ArrayP
* }
* ```
**/
export function ArrayEvery<K, T extends K[]>(
predicate: Objects.ArrayPredicate<K>,
options?: DecoratorOptions
): FieldDecorator<T> {
return createFieldValidator<T>(
export function ArrayEvery<This, Item, Value extends Item[]>(
predicate: Objects.ArrayPredicate<Item>,
options?: DecoratorOptions<This>
): FieldDecorator<This, Value> {
return createFieldValidator<This, Value>(
(array, _context, locale) => ({
key: buildKeyProp(options, DecoratorKeys.ARRAY_EVERY),
valid: isArrayEveryValid(array, predicate),
message: buildMessageProp(options, locale, translate(locale, DecoratorKeys.ARRAY_EVERY)),
}),
buildGroupsProp(options)
buildDecoratorMeta(options)
);
}
16 changes: 8 additions & 8 deletions packages/core/src/decorators/data/validators/array/ArrayNone.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DecoratorKeys } from "@decorators/data/validators/DecoratorKeys";
import { type FieldDecorator, createFieldValidator } from "@decorators/factory/forField";
import { type DecoratorOptions, buildGroupsProp, buildKeyProp, buildMessageProp } from "@decorators/helper";
import { createFieldValidator, type FieldDecorator } from "@decorators/factory/forField";
import { buildDecoratorMeta, buildKeyProp, buildMessageProp, type DecoratorOptions } from "@decorators/helper";
import { translate } from "@localization/service/TranslationService";
import { Objects } from "@utilities";

Expand Down Expand Up @@ -59,16 +59,16 @@ function isArrayNoneValid<K, T extends K[]>(array: T, predicate: Objects.ArrayPr
* }
* ```
**/
export function ArrayNone<K, T extends K[]>(
predicate: Objects.ArrayPredicate<K>,
options?: DecoratorOptions
): FieldDecorator<T> {
return createFieldValidator<T>(
export function ArrayNone<This, Item, Value extends Item[]>(
predicate: Objects.ArrayPredicate<Item>,
options?: DecoratorOptions<This>
): FieldDecorator<This, Value> {
return createFieldValidator<This, Value>(
(array, _context, locale) => ({
key: buildKeyProp(options, DecoratorKeys.ARRAY_NONE),
valid: isArrayNoneValid(array, predicate),
message: buildMessageProp(options, locale, translate(locale, DecoratorKeys.ARRAY_NONE)),
}),
buildGroupsProp(options)
buildDecoratorMeta(options)
);
}
16 changes: 8 additions & 8 deletions packages/core/src/decorators/data/validators/array/ArrayOne.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DecoratorKeys } from "@decorators/data/validators/DecoratorKeys";
import { type FieldDecorator, createFieldValidator } from "@decorators/factory/forField";
import { type DecoratorOptions, buildGroupsProp, buildKeyProp, buildMessageProp } from "@decorators/helper";
import { createFieldValidator, type FieldDecorator } from "@decorators/factory/forField";
import { buildDecoratorMeta, buildKeyProp, buildMessageProp, type DecoratorOptions } from "@decorators/helper";
import { translate } from "@localization/service/TranslationService";
import { Objects } from "@utilities";

Expand Down Expand Up @@ -59,16 +59,16 @@ function isArrayOneValid<K, T extends K[]>(array: T, predicate: Objects.ArrayPre
* }
* ```
**/
export function ArrayOne<K, T extends K[]>(
predicate: Objects.ArrayPredicate<K>,
options?: DecoratorOptions
): FieldDecorator<T> {
return createFieldValidator<T>(
export function ArrayOne<This, Item, Value extends Item[]>(
predicate: Objects.ArrayPredicate<Item>,
options?: DecoratorOptions<This>
): FieldDecorator<This, Value> {
return createFieldValidator<This, Value>(
(array, _context, locale) => ({
key: buildKeyProp(options, DecoratorKeys.ARRAY_ONE),
valid: isArrayOneValid(array, predicate),
message: buildMessageProp(options, locale, translate(locale, DecoratorKeys.ARRAY_ONE)),
}),
buildGroupsProp(options)
buildDecoratorMeta(options)
);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DecoratorKeys } from "@decorators/data/validators/DecoratorKeys";
import { type FieldDecorator, createFieldValidator } from "@decorators/factory/forField";
import { type DecoratorOptions, buildGroupsProp, buildKeyProp, buildMessageProp } from "@decorators/helper";
import { createFieldValidator, type FieldDecorator } from "@decorators/factory/forField";
import { buildDecoratorMeta, buildKeyProp, buildMessageProp, type DecoratorOptions } from "@decorators/helper";
import { translate } from "@localization/service/TranslationService";
import { Objects } from "@utilities";

Expand Down Expand Up @@ -59,8 +59,11 @@ function isArraySizeExactValid(array: any[]): boolean {
* }
* ```
*/
export function ArraySizeExact<K, T extends K[]>(exact: number, options?: DecoratorOptions): FieldDecorator<T> {
return createFieldValidator<T>(
export function ArraySizeExact<This, Item, Value extends Item[]>(
exact: number,
options?: DecoratorOptions<This>
): FieldDecorator<This, Value> {
return createFieldValidator<This, Value>(
(array, _context, locale) => ({
key: buildKeyProp(options, DecoratorKeys.ARRAY_SIZE_EXACT),
valid: isArraySizeExactValid(array),
Expand All @@ -70,6 +73,6 @@ export function ArraySizeExact<K, T extends K[]>(exact: number, options?: Decora
translate(locale, DecoratorKeys.ARRAY_SIZE_EXACT, exact, (array ?? []).length)
),
}),
buildGroupsProp(options)
buildDecoratorMeta(options)
);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DecoratorKeys } from "@decorators/data/validators/DecoratorKeys";
import { type FieldDecorator, createFieldValidator } from "@decorators/factory/forField";
import { type DecoratorOptions, buildGroupsProp, buildKeyProp, buildMessageProp } from "@decorators/helper";
import { createFieldValidator, type FieldDecorator } from "@decorators/factory/forField";
import { buildDecoratorMeta, buildKeyProp, buildMessageProp, type DecoratorOptions } from "@decorators/helper";
import { translate } from "@localization/service/TranslationService";
import { Objects } from "@utilities";

Expand Down Expand Up @@ -59,8 +59,11 @@ function isArraySizeMaxValid(array: any[], max: number): boolean {
* }
* ```
*/
export function ArraySizeMax<K, T extends K[]>(max: number, options?: DecoratorOptions): FieldDecorator<T> {
return createFieldValidator<T>(
export function ArraySizeMax<This, Item, Value extends Item[]>(
max: number,
options?: DecoratorOptions<This>
): FieldDecorator<This, Value> {
return createFieldValidator<This, Value>(
(array, _, locale) => ({
key: buildKeyProp(options, DecoratorKeys.ARRAY_SIZE_MAX),
valid: isArraySizeMaxValid(array, max),
Expand All @@ -70,6 +73,6 @@ export function ArraySizeMax<K, T extends K[]>(max: number, options?: DecoratorO
translate(locale, DecoratorKeys.ARRAY_SIZE_MAX, max, (array ?? []).length)
),
}),
buildGroupsProp(options)
buildDecoratorMeta(options)
);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DecoratorKeys } from "@decorators/data/validators/DecoratorKeys";
import { type FieldDecorator, createFieldValidator } from "@decorators/factory/forField";
import { type DecoratorOptions, buildGroupsProp, buildKeyProp, buildMessageProp } from "@decorators/helper";
import { createFieldValidator, type FieldDecorator } from "@decorators/factory/forField";
import { buildDecoratorMeta, buildKeyProp, buildMessageProp, type DecoratorOptions } from "@decorators/helper";
import { translate } from "@localization/service/TranslationService";
import { Objects } from "@utilities";

Expand Down Expand Up @@ -59,8 +59,11 @@ function isArraySizeMinValid(array: any[], min: number): boolean {
* }
* ```
*/
export function ArraySizeMin<K, T extends K[]>(min: number, options?: DecoratorOptions): FieldDecorator<T> {
return createFieldValidator<T>(
export function ArraySizeMin<This, Item, Value extends Item[]>(
min: number,
options?: DecoratorOptions<This>
): FieldDecorator<This, Value> {
return createFieldValidator<This, Value>(
(array, _context, locale) => ({
key: buildKeyProp(options, DecoratorKeys.ARRAY_SIZE_MIN),
valid: isArraySizeMinValid(array, min),
Expand All @@ -70,6 +73,6 @@ export function ArraySizeMin<K, T extends K[]>(min: number, options?: DecoratorO
translate(locale, DecoratorKeys.ARRAY_SIZE_MIN, min, (array ?? []).length)
),
}),
buildGroupsProp(options)
buildDecoratorMeta(options)
);
}
Loading

0 comments on commit 3a345d7

Please sign in to comment.