Passed
Push — master ( b81e0e...d03e63 )
by Olof
02:06
created

base.js ➔ ???   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
cc 1
c 6
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
1
"use strict";
2
3
class Base {
4
    /**
5
     * Base constructor
6
     * @param {Boolean} strict Treat the type strictly.
7
     */
8
    constructor(strict = false) {
9
        this.strict = strict;
10
        return this;
0 ignored issues
show
Bug introduced by
The constructor does not have a meaningful return value. Are you sure this is correct?
Loading history...
11
    }
12
13
14
    format(format) {
15
        const args = Array.prototype.slice.call(arguments, 1);
16
17
        return format.replace(/{(\d+)}/g, (match, number) => {
18
            return typeof args[number] != 'undefined'
19
                ? args[number]
20
                : match;
21
        });
22
    }
23
24
25
    /**
26
     * Return the error.
27
     *
28
     * @param {String} errorType The type of error
29
     * @param {String} keyName The key where there is error
30
     *
31
     * @returns {Object} As the error object
32
     */
33
    error(errorType, keyName) {
34
        const messages = {
35
            "Any:required": this.format("'{0}' is required and can't be omitted.", keyName),
36
            "JString:strict": this.format("'{0}' is not the correct type.", keyName),
37
            "JString:min": this.format("'{0}' has a minimum required length of '{1}'.", keyName, this.minValue),
38
            "JString:max": this.format("'{0}' has a maximum length of '{1}'.", keyName, this.maxValue),
39
            "JString:email": this.format("'{0}' needs to be a valid email.", keyName),
40
            "JString:alphanum": this.format("'{0}' only accepts alphanumeric characters.", keyName)
41
        };
42
43
        return {
44
            error: {
45
                type: errorType,
46
                field: keyName,
47
                message: messages[errorType]
48
            }
49
        };
50
    }
51
52
53
    /**
54
     * Set the key to be required.
55
     */
56
    required() {
57
        this.isRequired = true;
58
        return this;
59
    }
60
61
62
    /**
63
     * Set the minimum value required for the key.
64
     * @param {*} minValue The minimum value for the key.
65
     */
66
    min(minValue) {
67
        this.minValue = minValue;
68
        return this;
69
    }
70
71
72
    /**
73
     * Set the maximum value required for the key.
74
     * @param {*} maxValue The maximum value for the key.
75
     */
76
    max(maxValue) {
77
        this.maxValue = maxValue;
78
        return this;
79
    }
80
}
81
82
module.exports = Base;
83