Stryker

validate-data.js - Stryker report

File / Directory
Mutation score
# Killed
# Survived
# Timeout
# No coverage
# Runtime errors
# Transpile errors
Total detected
Total undetected
Total mutants
validate-data.js
84.21 %
84.21 48 9 0 0 0 0 48 9 57
Expand all
import { getValueByName } from './get-data';

/**
 * Check rule for dependencies
 * @param {*} params - validation params
 * @param {object} data - form data
 * @returns {*} - validation params or null if rule is not checkable
 */
export function isCheckable(params, data) 0{
    if (123!params) 4{
        return null;
    }

    let checkable = 5true;
    let param = params;

    if (67Array.isArray(params)) 8{
        let dependencies = [];
        [param, ...dependencies] = params;
        if (91011!param) 12{
            return null;
        }

        try 13{
            checkable = dependencies.reduce(
                (required, dependency) =>
                    141516required && (171819typeof dependency === 20'function' ? dependency(data) : 21!22!data[dependency]),
                checkable,
            );
        } catch (e) 23{
            if (242526process.env.NODE_ENV === 27'development') 28{
                console.warn(29`Dependency function error: ${e.toString()}`);
            }
        }
    }

    return checkable ? param : null;
}

/**
 * Validate field
 * @param {object} rules - object with rules for validation
 * @param {object} methods - validation methods
 * @param {string|FileList|Array} value - input value
 * @param {string} name - input name
 * @param {object} errorMessages - object with error messages
 * @param {object} data - form data
 * @param {function} translate - translation function
 * @returns {Array.<string>} - array of field errors
 */
export function validateField(rules, methods, value, name, errorMessages, data, translate) 30{
    const isEmpty = 31!methods.required.func(value);

    const isRequired = isCheckable(rules.required, data);

    if (323334isEmpty && isRequired) 35{
        return 36[translate(errorMessages[name].required)];
    }

    if (3738isEmpty) 39{
        return [];
    }

    return Object.keys(rules).reduce((errors, method) => 40{
        const params = isCheckable(rules[method], data);

        if (414243params === null) 44{
            return errors;
        }

        if (4546methods[method]) 47{
            const valid = methods[method].func(value, params);

            if (484950!valid) 51{
                errors.push(translate(errorMessages[name][method]));
            }
        } else 52{
            errors.push(53`Method "${method}" not found`); // todo translation
        }

        return errors;
    }, []);
}

/**
 * Validate data object
 * @param {object} rules - object with rules for validation
 * @param {object} methods - validation methods
 * @param {object} data - data object
 * @param {object} errorMessages - object with error messages
 * @param {function} translate - translation function
 * @returns {object.<string, Array.<string>>} - object of fields error arrays
 */
export function validateData(rules, methods, data, errorMessages, translate) 54{
    return Object.keys(rules).reduce((obj, name) => 55{
        const value = getValueByName(name, data);
        const errors = validateField(rules[name], methods, value, name, errorMessages, data, translate); // eslint-disable-line max-len
        return 56{
            ...obj,
            [name]: errors.length ? errors : undefined,
        };
    }, {});
}
# Mutator State Location Original Replacement
0 Block Killed 8 : 42 { ... ; } {}
1 IfStatement Killed 9 : 8 !
2 IfStatement Killed 9 : 8 !
3 PrefixUnaryExpression Killed 9 : 8 !
4 Block Killed 9 : 17 { ... } {}
5 BooleanSubstitution Killed 13 : 20
6 IfStatement Killed 16 : 8 . ... )
7 IfStatement Killed 16 : 8 . ... )
8 Block Killed 16 : 31 { ... } {}
9 IfStatement Killed 19 : 12 !
10 IfStatement Killed 19 : 12 !
11 PrefixUnaryExpression Killed 19 : 12 !
12 Block Killed 19 : 20 { ... } {}
13 Block Killed 23 : 12 { ... } {}
14 BinaryExpression Killed 26 : 20 && ( ... ]) || ( ... ])
15 ConditionalExpression Killed 26 : 20 && ( ... ])
16 ConditionalExpression Killed 26 : 20 && ( ... ])
17 BinaryExpression Killed 26 : 33 === ' ' !== ' '
18 ConditionalExpression Killed 26 : 33 === ' '
19 ConditionalExpression Killed 26 : 33 === ' '
20 StringLiteral Killed 26 : 55 ' ' ""
21 PrefixUnaryExpression Killed 26 : 87 !! ... ] ! ... ]
22 PrefixUnaryExpression Killed 26 : 88 ! ... ] [ ]
23 Block Survived 29 : 20 { ... } {}
24 IfStatement Survived 30 : 16 . .... '
25 IfStatement Survived 30 : 16 . .... '
26 BinaryExpression Survived 30 : 16 . .... ' . .... '
27 StringLiteral Survived 30 : 41 ' ' ""
28 Block Survived 30 : 56 { ... } {}
29 StringLiteral Survived 31 : 29 ` ... ()}` ""
30 Block Killed 50 : 91 { ...]); } {}
31 PrefixUnaryExpression Killed 51 : 20 ! ... ) . ... )
32 IfStatement Killed 55 : 8 &&
33 IfStatement Killed 55 : 8 &&
34 BinaryExpression Killed 55 : 8 && ||
35 Block Killed 55 : 31 { ... } {}
36 ArrayLiteral Killed 56 : 15 [ ... )] []
37 IfStatement Survived 59 : 8
38 IfStatement Killed 59 : 8
39 Block Survived 59 : 17 { ... } {}
40 Block Killed 63 : 57 { ... } {}
41 IfStatement Killed 66 : 12 ===
42 IfStatement Killed 66 : 12 ===
43 BinaryExpression Killed 66 : 12 === !==
44 Block Killed 66 : 29 { ... } {}
45 IfStatement Killed 70 : 12 [ ]
46 IfStatement Killed 70 : 12 [ ]
47 Block Killed 70 : 29 { ... } {}
48 IfStatement Killed 73 : 16 !
49 IfStatement Killed 73 : 16 !
50 PrefixUnaryExpression Killed 73 : 16 !
51 Block Killed 73 : 24 { ... } {}
52 Block Killed 76 : 15 { ... } {}
53 StringLiteral Killed 77 : 24 ` ... ` ""
54 Block Killed 93 : 77 { ...}); } {}
55 Block Killed 94 : 52 { ... } {}
56 ObjectLiteral Killed 97 : 15 { ... } {}