Skip to content

Commit

Permalink
Refactor bypass validation
Browse files Browse the repository at this point in the history
  • Loading branch information
sebelga committed Jul 12, 2021
1 parent 080ed06 commit ac0a7fe
Showing 1 changed file with 21 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
FieldValidateResponse,
ValidationError,
FormData,
ValidationConfig,
} from '../types';
import { FIELD_TYPES, VALIDATION_TYPES } from '../constants';

Expand Down Expand Up @@ -212,6 +213,24 @@ export const useField = <T, FormType = FormData, I = T>(

cancelInflightValidation();

const doByPassValidation = ({
type: validationType,
isBlocking,
}: ValidationConfig<FormType, string, I>) => {
if (
typeof validationTypeToValidate !== 'undefined' &&
validationType !== validationTypeToValidate
) {
return true;
}

if (runOnlyBlockingValidations && isBlocking === false) {
return true;
}

return false;
};

const runAsync = async () => {
const validationErrors: ValidationError[] = [];

Expand All @@ -222,17 +241,9 @@ export const useField = <T, FormType = FormData, I = T>(
validator,
exitOnFail = true,
type: validationType = VALIDATION_TYPES.FIELD,
isBlocking = true,
} = validation;

if (
typeof validationTypeToValidate !== 'undefined' &&
validationType !== validationTypeToValidate
) {
continue;
}

if (runOnlyBlockingValidations && isBlocking === false) {
if (doByPassValidation(validation)) {
continue;
}

Expand Down Expand Up @@ -273,17 +284,9 @@ export const useField = <T, FormType = FormData, I = T>(
validator,
exitOnFail = true,
type: validationType = VALIDATION_TYPES.FIELD,
isBlocking = true,
} = validation;

if (
typeof validationTypeToValidate !== 'undefined' &&
validationType !== validationTypeToValidate
) {
continue;
}

if (runOnlyBlockingValidations && isBlocking === false) {
if (doByPassValidation(validation)) {
continue;
}

Expand Down

0 comments on commit ac0a7fe

Please sign in to comment.