All files / src/components/utils/validation datePicker.ts

53.84% Statements 7/13
50% Branches 13/26
100% Functions 1/1
53.84% Lines 7/13

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 43 44 45 46 47 48 49 50 51 52 53                                        28x 28x 28x 28x 28x   28x                                               28x    
import type {DateTime} from '@gravity-ui/date-utils';
 
import type {ExtractFunctionType} from '../../types';
 
import {i18n} from './i18n';
 
export interface ValidationResult {
    isInvalid: boolean;
    errors: string[];
}
 
export function getValidationResult(
    value: DateTime | null | undefined,
    minValue: DateTime | undefined,
    maxValue: DateTime | undefined,
    isDateUnavailable: ((v: DateTime) => boolean) | undefined,
    timeZone: string,
    valueTitle = 'Value',
    t: ExtractFunctionType<typeof i18n> = i18n,
): ValidationResult {
    const rangeOverflow = value && maxValue && maxValue.isBefore(value);
    const rangeUnderflow = value && minValue && value.isBefore(minValue);
    const isUnavailable = (value && isDateUnavailable?.(value)) || false;
    const isInvalid = rangeOverflow || rangeUnderflow || isUnavailable;
    const errors = [];
 
    Iif (isInvalid) {
        if (rangeUnderflow && minValue) {
            errors.push(
                t('Value must be {minValue} or later.', {
                    minValue: minValue.timeZone(timeZone).format(),
                    value: valueTitle,
                }),
            );
        }
 
        if (rangeOverflow && maxValue) {
            errors.push(
                t('Value must be {maxValue} or earlier.', {
                    maxValue: maxValue.timeZone(timeZone).format(),
                    value: valueTitle,
                }),
            );
        }
 
        if (isUnavailable) {
            errors.push(t('Selected date unavailable.'));
        }
    }
 
    return {isInvalid, errors};
}