test/validate.test.js   A
last analyzed

Complexity

Total Complexity 4
Complexity/F 1

Size

Lines of Code 50
Function Count 4

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 0
c 3
b 0
f 0
nc 1
dl 0
loc 50
rs 10
noi 0
wmc 4
mnd 0
bc 4
fnc 4
bpm 1
cpm 1

1 Function

Rating   Name   Duplication   Size   Complexity  
A validate.test.js ➔ ??? 0 6 1
1
"use strict";
2
const validate = require("../src/validate");
3
const JString = require("../src/types/string");
4
5
test("test validate not required", () => {
6
    const data = { "username": "oenstrom" };
7
    const schema = { "test": {} };
8
9
    expect(validate(data, schema)).toBeNull();
10
});
11
12
test("test validate required", () => {
13
    const data = {};
14
    const schema = { "test": new JString().required() };
15
16
    expect(validate(data, schema)).toEqual({ error: {
17
        type: "Any:required",
18
        field: "test",
19
        message: "'test' is required and can't be omitted."
20
    }});
21
});
22
23
test("test validate using type validate", () => {
24
    const schema = { "test": new JString().alphanum() };
25
    const data   = { "test": "test(/)!" };
26
    const data2  = { "test": "test" };
27
28
    expect(validate(data, schema, "json")).toEqual(
29
        JSON.stringify({ error: {
30
            type: "JString:alphanum",
31
            field: "test",
32
            message: "'test' only accepts alphanumeric characters."
33
        }}, null, 4)
34
    );
35
    expect(validate(data2, schema)).toBeNull();
36
});
37
38
test("test multiple fields", () => {
39
    const schema = {
40
        username: new JString().min(3).max(3),
41
        password: new JString().min(6).max(100)
42
    };
43
    const data = { password: "test" };
44
45
    expect(validate(data, schema)).toEqual({ error: {
46
        type: "JString:min",
47
        field: "password",
48
        message: "'password' has a minimum required length of '6'."
49
    }});
50
});
51