Skip to content

Commit 51fd143

Browse files
committed
Refactored the code to be more simple
1 parent 9b6ca80 commit 51fd143

File tree

2 files changed

+54
-19
lines changed

2 files changed

+54
-19
lines changed

src/__tests__/validate.test.js

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,38 @@ const { validate, email, length, required } = require('../validate')
33
const defaultRules = {
44
fields: {
55
email: {
6-
validator: (value) => {
6+
validator: value => {
77
return length(value, { max: 75 }) && email(value)
88
},
99
message: 'The given email address is invalid',
1010
},
1111
password: {
12-
validator: (value) => {
12+
validator: value => {
1313
return length(value, { min: 4, max: 18 })
1414
},
1515
},
1616
subscription: {
17-
validator: (value) => {
17+
validator: value => {
1818
return required(value)
19-
}
20-
}
21-
}
19+
},
20+
},
21+
},
2222
}
2323

2424
it('initializes', () => {
2525
const fields = {}
2626
const rules = {}
27-
validate( fields, { rules, } )
27+
validate(fields, { rules })
28+
})
29+
30+
it('returns invalid when there are no rules', () => {
31+
const fields = {
32+
password: 'homepage'
33+
}
34+
const rules = {}
35+
36+
const { valid } = validate(fields, { rules })
37+
expect(valid).toEqual(false)
2838
})
2939

3040
it('calls the correct validation checks and calls them with the current value', () => {
@@ -65,10 +75,43 @@ it('uses the given rules to validate against the fields', () => {
6575
const fields = {
6676
6777
password: 'password',
68-
subscription: true
78+
subscription: true,
6979
}
7080

7181
const rules = defaultRules
72-
const { valid } = validate( fields, { rules, } )
82+
const { valid } = validate(fields, { rules })
7383
expect(valid).toEqual(true)
7484
})
85+
86+
it('validates to false when one one of the validation rules returns false', () => {
87+
const fields = {
88+
89+
password: 'password',
90+
}
91+
92+
const rules = defaultRules
93+
const { valid, results } = validate(fields, { rules })
94+
expect(valid).toEqual(false)
95+
expect(results).toMatchInlineSnapshot(`
96+
Array [
97+
Object {
98+
"field": "email",
99+
"message": "",
100+
"valid": true,
101+
"value": "[email protected]",
102+
},
103+
Object {
104+
"field": "password",
105+
"message": "",
106+
"valid": true,
107+
"value": "password",
108+
},
109+
Object {
110+
"field": "subscription",
111+
"message": "Validation failed for field 'subscription'",
112+
"valid": false,
113+
"value": undefined,
114+
},
115+
]
116+
`)
117+
})

src/validate.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
function validate(fields, { rules }) {
2-
if (isEmpty(fields)) {
3-
return { valid: false, results: [] }
4-
}
2+
const checked = []
53

6-
if (isEmpty(rules.fields)) {
4+
if (!fields || !rules || !rules.fields ) {
75
return { valid: false, results: [] }
86
}
97

10-
const checked = []
11-
128
Object.keys(rules.fields).forEach((field) => {
139
const value = fields[field]
1410
const rule = rules.fields[field]
@@ -33,10 +29,6 @@ function validate(fields, { rules }) {
3329
}
3430
}
3531

36-
function isEmpty(obj) {
37-
return Object.keys(obj).length === 0
38-
}
39-
4032
module.exports = {
4133
validate,
4234
...require('./validation/index.js'),

0 commit comments

Comments
 (0)