Skip to content

Commit 433b335

Browse files
committed
First draft ready for testing in example app
0 parents  commit 433b335

File tree

11 files changed

+3801
-0
lines changed

11 files changed

+3801
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/node_modules

REAMD.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
```js
2+
3+
const rules = {
4+
fields: {
5+
email: {
6+
validator: () => {},
7+
},
8+
password: {
9+
validator: () => {},
10+
}
11+
}
12+
}
13+
14+
const callback = (messages) => {
15+
16+
}
17+
18+
validate({
19+
rules,
20+
}, callback)
21+
```

jest.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
testEnvironment: "jest-environment-node"
3+
}

package.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "validate",
3+
"version": "0.0.1",
4+
"main": "index.js",
5+
"repository": "[email protected]:stackkit/validate.git",
6+
"author": "larsvankleef <[email protected]>",
7+
"license": "MIT",
8+
"scripts": {
9+
"test": "jest --coverage",
10+
"test:watch": "jest --watch",
11+
"test:open": "open coverage/lcov-report/index.html"
12+
},
13+
"devDependencies": {
14+
"jest": "^26.2.2"
15+
}
16+
}

src/__tests__/validate.test.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
const { validate } = require('../validate')
2+
3+
it('initializes', () => {
4+
const callback = jest.fn()
5+
6+
const fields = {}
7+
const rules = {}
8+
9+
validate(fields, {
10+
rules,
11+
}, callback)
12+
})
13+
14+
it('calls the correct validation checks and calls them with the current value', () => {
15+
const email = '[email protected]'
16+
const password = 'password'
17+
18+
const fields = {
19+
email,
20+
password
21+
}
22+
23+
const emailValidator = jest.fn(() => true)
24+
const passwordValidator = jest.fn(() => true)
25+
26+
const rules = {
27+
fields: {
28+
email: {
29+
validator: emailValidator
30+
},
31+
password: {
32+
validator: passwordValidator
33+
}
34+
}
35+
}
36+
37+
const { valid } = validate(fields, {
38+
rules,
39+
})
40+
41+
expect(valid).toEqual(true)
42+
expect(emailValidator).toBeCalledTimes(1)
43+
expect(emailValidator).toBeCalledWith(email)
44+
expect(passwordValidator).toBeCalledTimes(1)
45+
expect(passwordValidator).toBeCalledWith(password)
46+
})
47+
48+
it('shows a message when a field is entered but there are no rules for it', () => {
49+
const fields = {
50+
51+
}
52+
53+
const { valid, results } = validate(fields, {
54+
rules: {
55+
fields: {},
56+
}
57+
})
58+
59+
const expected = [
60+
{
61+
field: 'email',
62+
valid: false,
63+
message: 'No validation rules for field email',
64+
}
65+
]
66+
67+
expect(valid).toEqual(false)
68+
expect(results).toEqual(expected)
69+
})

src/validate.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
function validate (fields, { rules }) {
2+
if (isEmpty(fields)) {
3+
return false
4+
}
5+
6+
const checked = []
7+
8+
Object.keys(fields).forEach((field) => {
9+
const rule = rules.fields[field]
10+
11+
if (!rule) {
12+
checked.push({ field, valid: false, message: `No validation rules for field ${field}`})
13+
return
14+
}
15+
16+
if (rule.validator(fields[field])) {
17+
checked.push({ field, valid: true, message: '' })
18+
} else {
19+
checked.push({ field, valid: false, message: rules.fields[field].message || 'Validation failed for field' })
20+
}
21+
})
22+
23+
return {
24+
valid: checked.every((current) => current.valid === true),
25+
results: checked
26+
}
27+
}
28+
29+
function isEmpty(obj) {
30+
return Object.keys(obj).length === 0;
31+
}
32+
33+
module.exports = {
34+
validate
35+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const { email } = require('../index')
2+
3+
it('returns true when a valid email is given', () => {
4+
const result = email('[email protected]')
5+
6+
expect(result).toEqual(true)
7+
})
8+
9+
it('returns false when a invalid email is given', () => {
10+
expect(email('info.example.com')).toEqual(false)
11+
expect(email('info@@example.com')).toEqual(false)
12+
expect(email('info...example.com')).toEqual(false)
13+
expect(email('[email protected]@asdf')).toEqual(false)
14+
})
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
const { length } = require('../index')
2+
3+
const string = 'string'
4+
5+
it('checks for the length of a min value', () => {
6+
const result = length(string, {
7+
min: 4
8+
})
9+
10+
expect(result).toEqual(true)
11+
})
12+
13+
it('checks for the length of a max value', () => {
14+
const result = length(string, {
15+
max: 7
16+
})
17+
18+
expect(result).toEqual(true)
19+
})
20+
21+
it('returns false when the min length is not reached', () => {
22+
const result = length(string, {
23+
min: 100,
24+
})
25+
26+
expect(result).toEqual(false)
27+
})
28+
29+
it('returns false when the max length is not reached', () => {
30+
const result = length(string, {
31+
max: 1,
32+
})
33+
34+
expect(result).toEqual(false)
35+
})
36+
37+
it('works with a min and a max value', () => {
38+
const result = length('strings', {
39+
min: 4,
40+
max: 5
41+
})
42+
43+
expect(result).toEqual(false)
44+
})
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const { number } = require('../index')
2+
3+
it('validates that a given value is a number', () => {
4+
expect(number(10)).toEqual(true)
5+
expect(number('10')).toEqual(false)
6+
})
7+
8+
it('validates that a given number is a lower then the max value', () => {
9+
const value = 10
10+
11+
expect(number(value, { max: 11 })).toEqual(true)
12+
expect(number(value, { max: 9 })).toEqual(false)
13+
})
14+
15+
it('validates that a given number is higher then the min value', () => {
16+
const value = 10
17+
18+
expect(number(value, { min: 9 })).toEqual(true)
19+
expect(number(value, { min: 11 })).toEqual(false)
20+
})

src/validation/index.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
function number (x, { max, min } = {}) {
2+
if (min || max) {
3+
if (Number(x) === x) {
4+
if (x >= min && x <= max) return true
5+
if (x >= min && max === undefined) return true
6+
if (x <= max && min === undefined) return true
7+
}
8+
}
9+
10+
if (Number(x) === x && (min === undefined && max === undefined)) {
11+
return true
12+
}
13+
14+
return false
15+
}
16+
17+
function email (x) {
18+
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
19+
return emailRegex.test(x)
20+
}
21+
22+
function length (x, { min, max }) {
23+
const length = x.length
24+
25+
if (length >= min && length <= max) return true
26+
if (length >= min && max === undefined) return true
27+
if (length <= max && min === undefined) return true
28+
29+
return false
30+
}
31+
32+
module.exports = {
33+
email,
34+
length,
35+
number,
36+
}

0 commit comments

Comments
 (0)