All files / dist/validators date.js

100% Statements 25/25
100% Branches 21/21
100% Functions 1/1
100% Lines 25/25

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  3x 3x 3x 3x 3x               3x 99x 99x 68x   9x 4x   9x   10x 4x   10x   10x 2x   10x     31x 4x 2x       27x 8x     99x   3x  
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.handleDateValidation = void 0;
var moment = require("moment");
var validators_1 = require("../utils/validators");
var constants_1 = require("../utils/constants");
/**
 * Handle Date Validations
 *
 * @param {String} field
 * @param {Object} validator
 * @param {Object} value
 */
var handleDateValidation = function (field, validator, value) {
    var errors = [];
    if (value && (0, validators_1.isDate)(value)) {
        switch (validator.type) {
            case constants_1.DATE_VALIDATOR_TYPES.EQUAL:
                if (value !== validator.value) {
                    errors.push({ field: field, message: validator.message });
                }
                break;
            case constants_1.DATE_VALIDATOR_TYPES.AFTER:
                if (moment(value).isBefore(moment(validator.value))) {
                    errors.push({ field: field, message: validator.message });
                }
                break;
            case constants_1.DATE_VALIDATOR_TYPES.BEFORE:
                if (moment(value).isAfter(moment(validator.value))) {
                    errors.push({ field: field, message: validator.message });
                }
                break;
        }
    }
    else if (value && !(0, validators_1.isDate)(value)) {
        if (validator.type == constants_1.DATE_VALIDATOR_TYPES.TYPE) {
            errors.push({ field: field, message: validator.message });
        }
    }
    else {
        if (validator.type == constants_1.DATE_VALIDATOR_TYPES.REQUIRED) {
            errors.push({ field: field, message: validator.message });
        }
    }
    return errors;
};
exports.handleDateValidation = handleDateValidation;