All files / dist/validators number.js

100% Statements 36/36
100% Branches 39/39
100% Functions 1/1
100% Lines 36/36

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  3x 3x 3x 3x               3x 265x 265x 185x   10x 4x   10x   11x 4x   11x   17x 7x   17x   7x 4x   7x   7x 4x   7x   13x 5x   13x   27x 9x   27x     80x 10x 5x       70x 13x     265x   3x  
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.handleNumberValidation = void 0;
var validators_1 = require("../utils/validators");
var constants_1 = require("../utils/constants");
/**
 * Handle Number Validations
 *
 * @param {String} field
 * @param {Object} validator
 * @param {Object} value
 */
var handleNumberValidation = function (field, validator, value) {
    var errors = [];
    if (value !== null && value !== undefined && (0, validators_1.isNumber)(value)) {
        switch (validator.type) {
            case constants_1.NUMBER_VALIDATOR_TYPES.EQUAL:
                if (value !== validator.value) {
                    errors.push({ field: field, message: validator.message });
                }
                break;
            case constants_1.NUMBER_VALIDATOR_TYPES.IN:
                if (!validator.value.includes(value)) {
                    errors.push({ field: field, message: validator.message });
                }
                break;
            case constants_1.NUMBER_VALIDATOR_TYPES.INTEGER:
                if (!(0, validators_1.isInteger)(value)) {
                    errors.push({ field: field, message: validator.message });
                }
                break;
            case constants_1.NUMBER_VALIDATOR_TYPES.LATITUDE:
                if (value < -90 || value > 90) {
                    errors.push({ field: field, message: validator.message });
                }
                break;
            case constants_1.NUMBER_VALIDATOR_TYPES.LONGITUDE:
                if (value < -180 || value > 180) {
                    errors.push({ field: field, message: validator.message });
                }
                break;
            case constants_1.NUMBER_VALIDATOR_TYPES.MAX:
                if (value > validator.value) {
                    errors.push({ field: field, message: validator.message });
                }
                break;
            case constants_1.NUMBER_VALIDATOR_TYPES.MIN:
                if (value < validator.value) {
                    errors.push({ field: field, message: validator.message });
                }
                break;
        }
    }
    else if (value !== null && value !== undefined && !(0, validators_1.isNumber)(value)) {
        if (validator.type == constants_1.NUMBER_VALIDATOR_TYPES.TYPE) {
            errors.push({ field: field, message: validator.message });
        }
    }
    else {
        if (validator.type == constants_1.NUMBER_VALIDATOR_TYPES.REQUIRED) {
            errors.push({ field: field, message: validator.message });
        }
    }
    return errors;
};
exports.handleNumberValidation = handleNumberValidation;