All files / dist/validators array.js

100% Statements 24/24
100% Branches 23/23
100% Functions 1/1
100% Lines 24/24

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  3x 3x 3x 3x 3x               3x 104x 104x 66x   10x 2x   10x   9x 4x   9x   16x 6x       38x 10x 6x       28x 4x     104x   3x  
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.handleArrayValidation = void 0;
var _ = require("lodash");
var validators_1 = require("../utils/validators");
var constants_1 = require("../utils/constants");
/**
 * Handle Array Validations
 *
 * @param {String} field
 * @param {Validator} validator
 * @param {Object} value
 */
var handleArrayValidation = function (field, validator, value) {
    var errors = [];
    if (value !== null && value !== undefined && (0, validators_1.isArray)(value)) {
        switch (validator.type) {
            case constants_1.ARRAY_VALIDATOR_TYPES.CONTAINS:
                if (!value.includes(validator.value)) {
                    errors.push({ field: field, message: validator.message });
                }
                break;
            case constants_1.ARRAY_VALIDATOR_TYPES.EQUAL:
                if (!_.isEqual(_.sortBy(value), _.sortBy(validator.value))) {
                    errors.push({ field: field, message: validator.message });
                }
                break;
            case constants_1.ARRAY_VALIDATOR_TYPES.SIZE:
                if (value.length !== validator.value) {
                    errors.push({ field: field, message: validator.message });
                }
        }
    }
    else if (value !== null && value !== undefined && !(0, validators_1.isArray)(value)) {
        if (validator.type == constants_1.ARRAY_VALIDATOR_TYPES.TYPE) {
            errors.push({ field: field, message: validator.message });
        }
    }
    else {
        if (validator.type == constants_1.ARRAY_VALIDATOR_TYPES.REQUIRED) {
            errors.push({ field: field, message: validator.message });
        }
    }
    return errors;
};
exports.handleArrayValidation = handleArrayValidation;