All files / src/lib/forms/models AnjunaValidators.ts

100% Statements 41/41
100% Branches 26/26
100% Functions 8/8
100% Lines 40/40

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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108                  1x   1x 8x 2x     6x     6x 1x   5x 1x     4x     6x 2x   4x               1x 12x 2x   10x               1x 4x 1x     3x 3x   3x 2x   1x               1x 1x 4x 1x     3x 1x           2x         1x 1x 4x 1x     3x 1x           2x       1x  
import {AbstractControl, ValidatorFn} from '@angular/forms';
 
import {RequiredCheckResult} from '../interfaces/RequiredCheckResult';
import {DateValidationResult} from '../interfaces/DateValidationResult';
import {MinDateValidationResult} from '../interfaces/MinDateValidationResult';
import {MaxDateValidationResult} from '../interfaces/MaxDateValidationResult';
import {UniqueValidationResult} from '../interfaces/UniqueValidationResult';
 
/** @dynamic */
export abstract class AnjunaValidators {
 
    public static date(c: AbstractControl): DateValidationResult | null {
        if (!c.value) {
            return;
        }
 
        let isValid = true;
 
        // Handle obvious Invalid Date
        if (c.value.toString() === 'Invalid Date') {
            isValid = false;
            // Handle Moment Date
        } else if (typeof c.value._isValid === 'boolean') {
            isValid = c.value._isValid;
            // Handle Native Date
        } else {
            isValid = !isNaN(new Date(c.value).getTime());
        }
 
        if (isValid || c.value === '') {
            return null;
        } else {
            return {
                date: {
                    valid: false
                }
            };
        }
    }
 
    public static requiredCheck(c: AbstractControl): RequiredCheckResult | null {
        if (c.value) {
            return null;
        } else {
            return {
                required: {
                    valid: false
                }
            };
        }
    }
 
    public static unique(c: AbstractControl): UniqueValidationResult | null {
        if (!c.value || !Array.isArray(c.value)) {
            return;
        }
 
        const testArray = c.value.filter(Boolean);
        const isValid = testArray.length === Array.from(new Set(testArray)).length;
 
        if (isValid) {
            return null;
        } else {
            return {
                unique: {
                    valid: false
                }
            };
        }
    }
 
    public static minDate = (min: Date): ValidatorFn => {
        return (c: AbstractControl): MinDateValidationResult | null => {
            if (!c.value) {
                return;
            }
 
            if (c.value < min) {
                return {
                    min: {
                        valid: false
                    }
                };
            } else {
                return null;
            }
        };
    }
 
    public static maxDate = (max: Date): ValidatorFn => {
        return (c: AbstractControl): MaxDateValidationResult | null => {
            if (!c.value) {
                return;
            }
 
            if (c.value > max) {
                return {
                    max: {
                        valid: false
                    }
                };
            } else {
                return null;
            }
        };
    }
}