All files / src/validation numberValidation.ts

100% Statements 35/35
100% Branches 19/19
100% Functions 3/3
100% Lines 35/35

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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 431x     1x 1x 45x 45x 38x 38x   7x 7x 45x 45x 45x   1x 1x 40x 40x 32x 32x   8x 8x 40x 40x 40x   1x 1x 4x 4x 4x 1x 1x   3x 3x 3x 3x 3x  
import { validationMessage } from '@/constants/validationMessages';
import type { ValidationResult } from '@/contracts/validationResult';
 
export const minValue =
  (min: number, propName?: string) =>
  (value: number): ValidationResult => {
    if ((value ?? 0) >= min) {
      return { isValid: true };
    }
 
    return {
      isValid: false,
      errorMessage: validationMessage.minValue(propName ?? 'property', min),
    };
  };
 
export const maxValue =
  (max: number, propName?: string) =>
  (value: number): ValidationResult => {
    if ((value ?? 0) < max) {
      return { isValid: true };
    }
 
    return {
      isValid: false,
      errorMessage: validationMessage.maxValue(propName ?? 'property', max),
    };
  };
 
export const valueIsEqualTo =
  (...values: Array<number>) =>
  (value: number): ValidationResult => {
    const safeValue = value ?? 0;
    if (values.includes(safeValue)) {
      return { isValid: true };
    }
 
    return {
      isValid: false,
      errorMessage: validationMessage.unexpectedValue('value', safeValue, values),
    };
  };