All files / src/validation arrayValidation.ts

100% Statements 26/26
100% Branches 8/8
100% Functions 2/2
100% Lines 26/26

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 321x   1x   1x 1x 3x 3x 3x 1x 1x   2x 2x 3x 3x 3x   1x 1x 4x 4x 4x 3x 3x   1x 1x 4x 4x 4x  
import { makeArrayOrDefault } from '@/helper/arrayHelper';
import type { ValidationResult } from '@/contracts/validationResult';
import { validationMessage } from '@/constants/validationMessages';
 
export const minItems =
  (minLength: number, itemName?: string) =>
  <T>(values: Array<T>): ValidationResult => {
    const safeArr = makeArrayOrDefault(values);
    if (safeArr.length >= minLength) {
      return { isValid: true };
    }
 
    return {
      isValid: false,
      errorMessage: validationMessage.minNumItems(itemName ?? 'item', minLength),
    };
  };
 
export const maxItems =
  (maxLength: number, itemName?: string) =>
  <T>(values: Array<T>): ValidationResult => {
    const safeArr = makeArrayOrDefault(values);
    if (safeArr.length < maxLength) {
      return { isValid: true };
    }
 
    return {
      isValid: false,
      errorMessage: validationMessage.maxNumItems(itemName ?? 'item', maxLength),
    };
  };