Skip to content

Commit

Permalink
chore: code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
victorgarciaesgi committed Dec 9, 2024
1 parent 3af232e commit ad35cab
Show file tree
Hide file tree
Showing 32 changed files with 364 additions and 655 deletions.
2 changes: 1 addition & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"printWidth": 100,
"printWidth": 120,
"tabWidth": 2,
"trailingComma": "es5",
"singleQuote": true,
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,10 @@ TODO
### For v1.0

- [x] Unit tests
- [ ] E2E tests (in progress)
- [x] E2E tests
- [ ] Type tests
- [ ] Simplify/reduce core code
- [ ] `tip` on createRule
- [ ] Example repo, Reproduction repl

### Next iterations
Expand Down
1 change: 0 additions & 1 deletion packages/core/src/core/createRule/createRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import type {
InferRegleRule,
RegleRuleInit,
RegleRuleMetadataDefinition,
RegleRuleTypeReturn,
RegleUniversalParams,
} from '../../types';
import { defineRuleProcessors } from './defineRuleProcessors';
Expand Down
28 changes: 4 additions & 24 deletions packages/core/src/core/defaultValidators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,35 +51,15 @@ export type DefaultValidators = {
>;
integer: RegleRuleDefinition<string | number, [], false, boolean, string | number>;
ipAddress: RegleRuleDefinition<string, [], false, boolean, string>;
macAddress: RegleRuleWithParamsDefinition<
string,
[separator?: string | undefined],
false,
boolean
>;
maxLength: RegleRuleWithParamsDefinition<
string | any[] | Record<PropertyKey, any>,
[count: number],
false,
boolean
>;
macAddress: RegleRuleWithParamsDefinition<string, [separator?: string | undefined], false, boolean>;
maxLength: RegleRuleWithParamsDefinition<string | any[] | Record<PropertyKey, any>, [count: number], false, boolean>;
maxValue: RegleRuleWithParamsDefinition<number, [count: number], false, boolean>;
minLength: RegleRuleWithParamsDefinition<
string | any[] | Record<PropertyKey, any>,
[count: number],
false,
boolean
>;
minLength: RegleRuleWithParamsDefinition<string | any[] | Record<PropertyKey, any>, [count: number], false, boolean>;
minValue: RegleRuleWithParamsDefinition<number, [count: number], false, boolean>;
numeric: RegleRuleDefinition<string | number, [], false, boolean, string | number>;
regex: RegleRuleWithParamsDefinition<string, [regexp: RegExp], false, boolean>;
required: RegleRuleDefinition<unknown, []>;
sameAs: RegleRuleWithParamsDefinition<
unknown,
[target: unknown, otherName?: string],
false,
boolean
>;
sameAs: RegleRuleWithParamsDefinition<unknown, [target: unknown, otherName?: string], false, boolean>;
startsWith: RegleRuleWithParamsDefinition<string, [part: Maybe<string>], false, boolean>;
url: RegleRuleDefinition<string, [], false, boolean, string>;
};
12 changes: 2 additions & 10 deletions packages/core/src/core/defineRegleConfig.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import type { EmptyObject } from 'type-fest';
import type {
AllRulesDeclarations,
RegleBehaviourOptions,
RegleShortcutDefinition,
} from '../types';
import type { AllRulesDeclarations, RegleBehaviourOptions, RegleShortcutDefinition } from '../types';
import { createUseRegleComposable, type useRegleFn } from './useRegle';
import { createInferRuleHelper, type inferRulesFn } from './useRegle/inferRules';

Expand All @@ -22,11 +18,7 @@ export function defineRegleConfig<
useRegle: useRegleFn<TCustomRules, TShortcuts>;
inferRules: inferRulesFn<TCustomRules>;
} {
const useRegle = createUseRegleComposable<TCustomRules, TShortcuts>(
rules,
modifiers,
shortcuts as any
);
const useRegle = createUseRegleComposable<TCustomRules, TShortcuts>(rules, modifiers, shortcuts as any);
const inferRules = createInferRuleHelper<TCustomRules>();

return { useRegle, inferRules };
Expand Down
12 changes: 3 additions & 9 deletions packages/core/src/core/useRegle/guards/rule.status.guards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ export function isNestedRulesStatus(rule: $InternalRegleStatusType): rule is $In
return isObject(rule) && '$fields' in rule;
}

export function isCollectionRulesStatus(
rule: $InternalRegleStatusType
): rule is $InternalRegleCollectionStatus {
export function isCollectionRulesStatus(rule: $InternalRegleStatusType): rule is $InternalRegleCollectionStatus {
return !!rule && '$each' in rule;
}

Expand All @@ -30,14 +28,10 @@ export function isRuleStatus(rule: unknown): rule is $InternalRegleRuleStatus {

// -- ExternalErrors

export function isNestedExternalErrorStatus(
rule: $InternalRegleErrors
): rule is RegleExternalErrorTree<any> {
export function isNestedExternalErrorStatus(rule: $InternalRegleErrors): rule is RegleExternalErrorTree<any> {
return !!rule && '$each' in rule;
}

export function isCollectionExternalErrorStatus(
rule: $InternalRegleErrors
): rule is RegleCollectionErrors<any> {
export function isCollectionExternalErrorStatus(rule: $InternalRegleErrors): rule is RegleCollectionErrors<any> {
return !!rule && '$each' in rule;
}
18 changes: 4 additions & 14 deletions packages/core/src/core/useRegle/guards/ruleDef.guards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@ export function isNestedRulesDef(
state: Ref<unknown>,
rules: Ref<$InternalFormPropertyTypes>
): rules is Ref<$InternalReglePartialRuleTree> {
return (
isObject(state.value) &&
isObject(rules.value) &&
!Object.entries(rules.value).some((rule) => isRuleDef(rule))
);
return isObject(state.value) && isObject(rules.value) && !Object.entries(rules.value).some((rule) => isRuleDef(rule));
}

export function isCollectionRulesDef(
Expand All @@ -28,25 +24,19 @@ export function isCollectionRulesDef(
return (!!rules.value && '$each' in rules.value) || Array.isArray(state.value);
}

export function isValidatorRulesDef(
rules: Ref<$InternalFormPropertyTypes>
): rules is Ref<$InternalRegleRuleDecl> {
export function isValidatorRulesDef(rules: Ref<$InternalFormPropertyTypes>): rules is Ref<$InternalRegleRuleDecl> {
return !!rules.value && isObject(rules.value);
}

export function isRuleDef(rule: unknown): rule is RegleRuleDefinition<any, any> {
return isObject(rule) && '_validator' in rule;
}

export function isFormRuleDefinition(
rule: Ref<unknown>
): rule is Ref<RegleRuleDefinition<any, any>> {
export function isFormRuleDefinition(rule: Ref<unknown>): rule is Ref<RegleRuleDefinition<any, any>> {
return !(typeof rule.value === 'function');
}

export function isFormInline(
rule: Ref<unknown>
): rule is Ref<InlineRuleDeclaration<any, any[], any>> {
export function isFormInline(rule: Ref<unknown>): rule is Ref<InlineRuleDeclaration<any, any[], any>> {
return typeof rule.value === 'function';
}

Expand Down
16 changes: 3 additions & 13 deletions packages/core/src/core/useRegle/inferRules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@ import type { NoInferLegacy, PrimitiveTypes, Unwrap } from '../../types/utils';
export interface inferRulesFn<TCustomRules extends Partial<AllRulesDeclarations>> {
<
TState extends Record<string, any>,
TRules extends ReglePartialRuleTree<
Unwrap<TState>,
Partial<AllRulesDeclarations> & TCustomRules
> &
TValid,
TRules extends ReglePartialRuleTree<Unwrap<TState>, Partial<AllRulesDeclarations> & TCustomRules> & TValid,
TValid = isDeepExact<
NoInferLegacy<TRules>,
ReglePartialRuleTree<Unwrap<TState>, Partial<AllRulesDeclarations> & TCustomRules>
Expand All @@ -26,21 +22,15 @@ export interface inferRulesFn<TCustomRules extends Partial<AllRulesDeclarations>
state: MaybeRef<TState> | DeepReactiveState<TState>,
rulesFactory: TRules
): TRules;
<TState extends PrimitiveTypes, TRules extends RegleRuleDecl>(
state: MaybeRef<TState>,
rulesFactory: TRules
): TRules;
<TState extends PrimitiveTypes, TRules extends RegleRuleDecl>(state: MaybeRef<TState>, rulesFactory: TRules): TRules;
}

export function createInferRuleHelper<
TCustomRules extends Partial<AllRulesDeclarations>,
>(): inferRulesFn<TCustomRules> {
function inferRules(
state: Record<string, any>,
rulesFactory:
| Record<string, any>
| (() => Record<string, any>)
| ComputedRef<Record<string, any>>
rulesFactory: Record<string, any> | (() => Record<string, any>) | ComputedRef<Record<string, any>>
) {
return rulesFactory;
}
Expand Down
11 changes: 2 additions & 9 deletions packages/core/src/core/useRegle/useRegle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@ export type useRegleFn<
TShortcuts extends RegleShortcutDefinition<any> = never,
> = <
TState extends Record<string, any>,
TRules extends ReglePartialRuleTree<
Unwrap<TState>,
Partial<AllRulesDeclarations> & TCustomRules
> &
TValid,
TRules extends ReglePartialRuleTree<Unwrap<TState>, Partial<AllRulesDeclarations> & TCustomRules> & TValid,
TValidationGroups extends Record<string, RegleValidationGroupEntry[]>,
TValid = isDeepExact<
NoInferLegacy<TRules>,
Expand Down Expand Up @@ -59,10 +55,7 @@ export function createUseRegleComposable<

function useRegle(
state: MaybeRef<Record<string, any>> | DeepReactiveState<Record<string, any>>,
rulesFactory:
| Record<string, any>
| (() => Record<string, any>)
| ComputedRef<Record<string, any>>,
rulesFactory: Record<string, any> | (() => Record<string, any>) | ComputedRef<Record<string, any>>,
options?: Partial<DeepMaybeRef<RegleBehaviourOptions>> &
LocalRegleBehaviourOptions<Record<string, any>, Record<string, any>, any>
): Regle<Record<string, any>, Record<string, any>, any, any> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import type { Ref } from 'vue';
import { toRef } from 'vue';
import type {
$InternalFormPropertyTypes,
$InternalRegleErrors,
$InternalRegleStatusType,
RegleCollectionRuleDeclKeyProperty,
} from '../../../../types';
import { randomId } from '../../../../utils';
import type { CommonResolverOptions, StateWithId } from '../common/common-types';
import { createReactiveChildrenStatus } from './../createReactiveNestedStatus';

interface CreateCollectionElementArgs extends CommonResolverOptions {
$id: string;
index: number;
stateValue: Ref<StateWithId>;
rules: $InternalFormPropertyTypes & RegleCollectionRuleDeclKeyProperty;
externalErrors: Ref<$InternalRegleErrors[] | undefined> | undefined;
initialState: any[] | undefined;
}

export function createCollectionElement({
$id,
path,
index,
options,
storage,
stateValue,
customMessages,
rules,
externalErrors,
initialState,
shortcuts,
fieldName,
}: CreateCollectionElementArgs): $InternalRegleStatusType | null {
const $fieldId = rules.$key ? rules.$key : randomId();
let $path = `${path}.${String($fieldId)}`;

if (typeof stateValue.value === 'object' && stateValue.value != null) {
if (!stateValue.value.$id) {
Object.defineProperties(stateValue.value, {
$id: {
value: $fieldId,
enumerable: false,
configurable: false,
writable: false,
},
});
} else {
$path = `${path}.${stateValue.value.$id}`;
}
}

const $status = createReactiveChildrenStatus({
state: stateValue,
rulesDef: toRef(() => rules),
customMessages,
path: $path,
storage,
options,
externalErrors: toRef(externalErrors?.value ?? [], index),
initialState: initialState?.[index],
shortcuts,
fieldName,
});

if ($status) {
const valueId = stateValue.value?.$id;
$status.$id = valueId ?? String($fieldId);
storage.addArrayStatus($id, $status.$id, $status);
}

return $status;
}
Loading

0 comments on commit ad35cab

Please sign in to comment.