All files / src/validation baseValidation.ts

100% Statements 21/21
100% Branches 12/12
100% Functions 3/3
100% Lines 21/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28    1x   1x 1x 83x 84x 78x 78x   6x 6x 58x 84x 84x   1x 1x 25x 25x 62x 62x 62x   9x 9x  
import type { ValidationResult } from '@/contracts/validationResult';
 
export const noValidation = <T>(_: T): ValidationResult => ({ isValid: true });
 
export const notNull =
  (customErrMsg?: string) =>
  <T>(value: T): ValidationResult => {
    if (value !== null) {
      return { isValid: true };
    }
 
    return {
      isValid: false,
      errorMessage: customErrMsg ?? `Field shouldn't be empty`,
    };
  };
 
export const multiValidation =
  <T>(...validations: Array<(validationVal: T) => ValidationResult>) =>
  (value: T): ValidationResult => {
    for (const validation of validations) {
      const result = validation(value);
      if (result.isValid === false) return result;
    }
 
    return { isValid: true };
  };