Skip to content

Commit

Permalink
♻️ [refactor]: Refactor EvaluatedStrategy and make Errors<T> wrapped …
Browse files Browse the repository at this point in the history
…inside Partial
  • Loading branch information
brunotot committed Sep 7, 2023
1 parent ab751ba commit 147d81a
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 22 deletions.
7 changes: 6 additions & 1 deletion packages/core/src/types/Errors.type.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import { EvaluatedStrategy } from "./EvaluatedStrategy";
import { $ } from "./namespace/Utility.ns";

export type Errors<T> = Partial<EvaluatedStrategy<T, string[]>>;
export type Errors<T> = EvaluatedStrategy<
T,
string[],
$.TArgGet<"partial">["enabled"]
>;
45 changes: 35 additions & 10 deletions packages/core/src/types/EvaluatedStrategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import { Strategy } from "./namespace/Strategy.ns";
import { $ } from "./namespace/Utility.ns";

// prettier-ignore
export type FieldStrategy<TActual, TKey extends $.Keys<TActual>, TStrat> =
export type FieldStrategy<
TActual,
TKey extends $.Keys<TActual>, TStrat,
TPartial extends $.TArg<"partial"> = "disabled"
> =

true extends Condition.isPrimitive<TActual[TKey]>
?Strategy.Primitive<TActual[TKey], TStrat>
Expand All @@ -12,22 +16,43 @@ export type FieldStrategy<TActual, TKey extends $.Keys<TActual>, TStrat> =
?Strategy.Function

:true extends Condition.isArray<TActual[TKey]>
?Strategy.Array<TActual[TKey], TStrat>
?Strategy.Array<TActual[TKey], TStrat, TPartial>

:true extends Condition.isObject<TActual[TKey]>
?Strategy.Object<TActual[TKey], TStrat>
?Strategy.Object<TActual[TKey], TStrat, TPartial>

:never;

export type StrategyOptional<TActual> = {
[TKey in $.WritableKeys<TActual>]?: FieldStrategy<TActual, TKey, $._>;
export type StrategyOptional<
TActual,
TPartial extends $.TArg<"partial"> = "disabled"
> = {
[TKey in $.WritableKeys<TActual>]?: FieldStrategy<
TActual,
TKey,
$._,
TPartial
>;
};

export type StrategyMandatory<TActual, TStrat> = {
[TKey in $.Keys<TActual>]-?: FieldStrategy<TActual, TKey, TStrat>;
export type StrategyMandatory<
TActual,
TStrat,
TPartial extends $.TArg<"partial"> = "disabled"
> = {
[TKey in $.WritableKeys<TActual>]-?: FieldStrategy<
TActual,
TKey,
TStrat,
TPartial
>;
};

// prettier-ignore
export type EvaluatedStrategy<T, V = $._> = true extends Condition.isUndefined<V>
? $.ExcludeNever<StrategyOptional<T>>
: $.ExcludeNever<StrategyMandatory<T, V>>;
export type EvaluatedStrategy<T, V = $._, TPartial extends $.TArg<"partial"> = "disabled"> = true extends Condition.isUndefined<V>
? TPartial extends $.TArgGet<"partial">["enabled"]
? Partial<$.ExcludeNever<StrategyOptional<T, TPartial>>>
: $.ExcludeNever<StrategyOptional<T, TPartial>>
: TPartial extends $.TArgGet<"partial">["enabled"]
? Partial<$.ExcludeNever<StrategyMandatory<T, V, TPartial>>>
: $.ExcludeNever<StrategyMandatory<T, V, TPartial>>;
28 changes: 19 additions & 9 deletions packages/core/src/types/namespace/Strategy.ns.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { EvaluatedStrategy } from "../EvaluatedStrategy";
import { Condition } from "./Condition.ns";
import { $ } from "./Utility.ns";

export type DeducedArray<TChild, TParent = TChild> = {
node: TParent;
Expand All @@ -11,25 +12,34 @@ type _PrimitiveStrategy<TActual, TReplacer> =
? NonNullable<TActual>
: TReplacer;

type _ObjectStrategy<TActual, TReplacer> = EvaluatedStrategy<
NonNullable<TActual>,
TReplacer
>;
type _ObjectStrategy<
TActual,
TReplacer,
TPartial extends $.TArg<"partial"> = "disabled"
> = EvaluatedStrategy<NonNullable<TActual>, TReplacer, TPartial>;

// prettier-ignore
type _ArrayStrategy<TActual, TReplacer> = TActual extends (infer U)[]
type _ArrayStrategy<TActual, TReplacer, TPartial extends $.TArg<"partial"> = "disabled"> = TActual extends (infer U)[]
? true extends Condition.isPrimitive<U>
? true extends Condition.isUndefined<TReplacer>
? TActual
: DeducedArray<TReplacer>
: true extends Condition.isUndefined<TReplacer>
? EvaluatedStrategy<U, TReplacer>[]
: DeducedArray<EvaluatedStrategy<U, TReplacer>, TReplacer>
? EvaluatedStrategy<U, TReplacer, TPartial>[]
: DeducedArray<EvaluatedStrategy<U, TReplacer, TPartial>, TReplacer>
: never;

export namespace Strategy {
export type Function = never;
export type Primitive<T, R> = _PrimitiveStrategy<T, R>;
export type Object<T, R> = _ObjectStrategy<T, R>;
export type Array<T, R> = _ArrayStrategy<T, R>;
export type Array<
T,
R,
TPartial extends $.TArg<"partial"> = "disabled"
> = _ArrayStrategy<T, R, TPartial>;
export type Object<
T,
R,
TPartial extends $.TArg<"partial"> = "disabled"
> = _ObjectStrategy<T, R, TPartial>;
}
9 changes: 9 additions & 0 deletions packages/core/src/types/namespace/Utility.ns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,13 @@ export namespace $ {
export type Values<T> = T[$.Keys<T>];
export type WritableKeys<T> = _WritableKeys<T>;
export type _ = undefined;
export type TArgGet<TParamKey extends keyof TypeParams> = TypeParams[TParamKey];
export type TArg<TParamKey extends keyof TypeParams> = $.Values<TArgGet<TParamKey>>;
}

type TypeParams = {
partial: {
disabled: "disabled";
enabled: "enabled";
};
};
3 changes: 1 addition & 2 deletions packages/react/src/hooks/useValidation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ export default function useValidation<TClass, TBody = TClass>(
): ns.UseValidationReturn<TClass, TBody> {
const defaultValue = config?.defaultValue;
const groups = config?.groups ?? [];
// prettier-ignore
const poc = useEntityProcessor(model, {groups, defaultValue});
const poc = useEntityProcessor(model, { groups, defaultValue });
const initialForm = defaultValue ?? poc.noArgsInstance;
const [form, setForm] = useState<TBody>(initialForm as TBody);
const [details, setDetails] = useState({} as DetailedErrors<TClass>);
Expand Down

0 comments on commit 147d81a

Please sign in to comment.