Skip to content

Commit

Permalink
feat: add method to check for a validator (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
crapStone authored Nov 7, 2024
1 parent d9d8ff3 commit b8e2153
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
9 changes: 9 additions & 0 deletions packages/platform/src/lib/form-field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
computeState,
computeValidateState,
computeValidators,
hasValidator,
InvalidDetails,
ValidationErrors,
ValidationState,
Expand Down Expand Up @@ -40,6 +41,7 @@ export type FormField<Value = unknown> = {
markAsDirty: () => void;
reset: () => void;
hasError: (errorKey: string) => boolean;
hasValidator: (validator: Validator) => boolean;
errorMessage: (errorKey: string) => string | undefined,
registerOnReset: (fn: (value: Value) => void) => void;
};
Expand Down Expand Up @@ -158,6 +160,13 @@ export function createFormField<Value>(
markAsTouched: () => touchedStateSignal.set('TOUCHED'),
markAsDirty: () => dirtyStateSignal.set('DIRTY'),
hasError: (errorKey: string) => !!errorsSignal()[errorKey],
hasValidator: (validator: Validator) => {
if (finalOptions !== undefined) {
return hasValidator(finalOptions.validators, validator);
} else {
return false;
}
},
errorMessage: (errorKey: string) => errorsArraySignal().find(e => e.key === errorKey)?.message,
registerOnReset: (fn: (value: Value) => void) => (onReset = fn),
reset: () => {
Expand Down
19 changes: 14 additions & 5 deletions packages/platform/src/lib/form-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
computeState,
computeValidateState,
computeValidators,
hasValidator,
InvalidDetails,
ValidationState,
Validator,
Expand All @@ -27,10 +28,10 @@ import {
export type FormGroup<Fields extends FormGroupCreatorOrSignal = {}> = {
__type: 'FormGroup';
value: Signal<UnwrappedFormGroup<Fields>>;
controls: Fields extends WritableSignal<FormGroup<infer G>[]>
? FormGroupFields<Fields> & WritableSignal<FormGroup<G>[]>
: Fields extends WritableSignal<infer F>
? FormGroupFields<Fields> & WritableSignal<F>
controls: Fields extends WritableSignal<FormGroup<infer G>[]>
? FormGroupFields<Fields> & WritableSignal<FormGroup<G>[]>
: Fields extends WritableSignal<infer F>
? FormGroupFields<Fields> & WritableSignal<F>
: FormGroupFields<Fields>;
valid: Signal<boolean>;
state: Signal<ValidationState>;
Expand All @@ -41,6 +42,7 @@ export type FormGroup<Fields extends FormGroupCreatorOrSignal = {}> = {
errors: Signal<{}>;
errorsArray: Signal<InvalidDetails[]>;
hasError: (errorKey: string) => boolean;
hasValidator: (validator: Validator) => boolean;
errorMessage: (errorKey: string) => string | undefined;
markAllAsTouched: () => void;
reset: () => void;
Expand Down Expand Up @@ -179,6 +181,13 @@ export function createFormGroup<FormFields extends FormGroupCreator>(
return myErrors.concat(...childErrors);
}),
hasError: (errorKey: string) => !!errorsSignal()[errorKey],
hasValidator: (validator: Validator) => {
if (options !== undefined) {
return hasValidator(options.validators, validator);
} else {
return false;
}
},
errorMessage: (errorKey: string) => errorsArraySignal().find(e => e.key === errorKey)?.message,
dirtyState: dirtyStateSignal,
dirty: dirtySignal,
Expand All @@ -205,7 +214,7 @@ export function createFormGroup<FormFields extends FormGroupCreator>(
(formFieldsMapOrSignal as WritableSignal<any[]>).set([
...initialArrayControls,
]);

for (const ctrl of initialArrayControls) {
ctrl.reset();
}
Expand Down
4 changes: 4 additions & 0 deletions packages/platform/src/lib/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,7 @@ export function computeState(validateSignal: Signal<ValidateState[]>) {
return 'VALID';
});
}

export function hasValidator(validators: Validator<any>[] | undefined, validator: Validator): boolean {
return Array.isArray(validators) ? validators.includes(validator) : validators === validator;
}

0 comments on commit b8e2153

Please sign in to comment.