Skip to content

Commit

Permalink
fix(if-validator): update logic if-validator (#47)
Browse files Browse the repository at this point in the history
* Update logic if-validator

* Update if-validator.ts

* format with nx format
  • Loading branch information
tieppt authored Sep 15, 2023
1 parent ebab6b1 commit f02b1ad
Showing 1 changed file with 5 additions and 11 deletions.
16 changes: 5 additions & 11 deletions libs/ngxtension/if-validator/src/if-validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
AsyncValidatorFn,
FormControl,
ValidatorFn,
Validators,
} from '@angular/forms';
import { of } from 'rxjs';

Expand All @@ -17,17 +18,10 @@ export function ifValidator(
if (!validatorFn || !condition(<FormControl>control)) {
return null;
}

if (validatorFn instanceof Array) {
for (let i = 0; i < validatorFn.length; i++) {
const result = validatorFn[i](control);
if (result) return result;
}

return null;
}

return validatorFn(control);
const validatorFns = Array.isArray(validatorFn)
? (validatorFn as ValidatorFn[])
: [validatorFn];
return Validators.compose(validatorFns)?.(control) ?? null;
};
}

Expand Down

0 comments on commit f02b1ad

Please sign in to comment.