Skip to content

Commit f9908c0

Browse files
committed
Rewrite to support requird values
1 parent 9a1c316 commit f9908c0

File tree

5 files changed

+63
-45
lines changed

5 files changed

+63
-45
lines changed

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
11
# Validate
22

33
```js
4-
const { validate, email, length } = require('validate')
4+
const { validate, email, length, required } = require('validate')
55

66
const rules = {
77
fields: {
88
email: {
9-
validator: () => {
9+
validator: (value) => {
1010
return length(value, { max: 75 }) && email(value)
1111
},
1212
message: 'The given email address is invalid',
1313
},
1414
password: {
15-
validator: () => {
15+
validator: (value) => {
1616
return length(value, { min: 18, max: 45 })
1717
},
18+
},
19+
subscription: {
20+
validator: (value) => {
21+
return required(value)
22+
}
1823
}
1924
}
2025
}

src/__tests__/validate.test.js

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,30 @@
1-
const { validate } = require('../validate')
1+
const { validate, email, length, required } = require('../validate')
22

3-
it('initializes', () => {
4-
const callback = jest.fn()
3+
const defaultRules = {
4+
fields: {
5+
email: {
6+
validator: (value) => {
7+
return length(value, { max: 75 }) && email(value)
8+
},
9+
message: 'The given email address is invalid',
10+
},
11+
password: {
12+
validator: (value) => {
13+
return length(value, { min: 4, max: 18 })
14+
},
15+
},
16+
subscription: {
17+
validator: (value) => {
18+
return required(value)
19+
}
20+
}
21+
}
22+
}
523

24+
it('initializes', () => {
625
const fields = {}
726
const rules = {}
8-
9-
validate(
10-
fields,
11-
{
12-
rules,
13-
},
14-
callback,
15-
)
27+
validate( fields, { rules, } )
1628
})
1729

1830
it('calls the correct validation checks and calls them with the current value', () => {
@@ -49,26 +61,14 @@ it('calls the correct validation checks and calls them with the current value',
4961
expect(passwordValidator).toBeCalledWith(password)
5062
})
5163

52-
it('shows a message when a field is entered but there are no rules for it', () => {
64+
it('uses the given rules to validate against the fields', () => {
5365
const fields = {
54-
66+
67+
password: 'password',
68+
subscription: true
5569
}
5670

57-
const { valid, results } = validate(fields, {
58-
rules: {
59-
fields: {},
60-
},
61-
})
62-
63-
expect(valid).toEqual(false)
64-
expect(results).toMatchInlineSnapshot(`
65-
Array [
66-
Object {
67-
"field": "email",
68-
"message": "No validation rules for field 'email'",
69-
"valid": false,
70-
"value": "[email protected]",
71-
},
72-
]
73-
`)
71+
const rules = defaultRules
72+
const { valid } = validate( fields, { rules, } )
73+
expect(valid).toEqual(true)
7474
})

src/validate.js

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,17 @@ function validate(fields, { rules }) {
33
return { valid: false, results: [] }
44
}
55

6+
if (isEmpty(rules.fields)) {
7+
return { valid: false, results: [] }
8+
}
9+
610
const checked = []
711

8-
Object.keys(fields).forEach(field => {
12+
Object.keys(rules.fields).forEach((field) => {
13+
const value = fields[field]
914
const rule = rules.fields[field]
1015

11-
if (!rule) {
12-
checked.push({
13-
field,
14-
value: fields[field],
15-
valid: false,
16-
message: `No validation rules for field '${field}'`,
17-
})
18-
return
19-
}
20-
21-
if (rule.validator(fields[field])) {
16+
if (rule.validator(value)) {
2217
checked.push({ field, value: fields[field], valid: true, message: '' })
2318
} else {
2419
checked.push({
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const { required } = require('../index')
2+
3+
it('validates that the value is not undefined or null', () => {
4+
expect(required(null)).toEqual(false)
5+
expect(required(undefined)).toEqual(false)
6+
})
7+
8+
it('validates to true when a string, number boolean or object is given', () => {
9+
expect(required({ test: true })).toEqual(true)
10+
expect(required('test')).toEqual(true)
11+
expect(required(10)).toEqual(true)
12+
expect(required(false)).toEqual(true)
13+
})

src/validation/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,13 @@ function length(x, { min, max }) {
2929
return false
3030
}
3131

32+
function required(x) {
33+
return x !== undefined && x !== null
34+
}
35+
3236
module.exports = {
3337
email,
3438
length,
3539
number,
40+
required
3641
}

0 commit comments

Comments
 (0)