Skip to content

Commit c71b4a5

Browse files
authored
Merge pull request #2 from mrbrunelli/feature/validate-value
feature/validate-value
2 parents 54f2f1e + 2ff8327 commit c71b4a5

4 files changed

Lines changed: 83 additions & 26 deletions

File tree

README.md

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ A Javascript object validator for tiny schemas.
66

77
![Test](https://github.com/mrbrunelli/object-validator/actions/workflows/test.yml/badge.svg)
88

9-
109
## Get started
10+
1111
```sh
1212
yarn add @mrbrunelli/object-validator
1313
```
@@ -25,6 +25,32 @@ const objectExample = {
2525
}
2626
}
2727

28-
const isValid = Validate.isValid(objectExample, ['foo', 'foo.bar.message'])
28+
const isValid = Validate.isValid(objectExample, [
29+
['foo'],
30+
['foo.bar.message', 'Hello!']
31+
])
2932
console.log(isValid) // returns true
3033
```
34+
35+
#### Why
36+
37+
Validating multiple fields can be very tiring in old Node versions.
38+
39+
```js
40+
// Work only Node 14 or >
41+
if (objectExample?.foo?.bar?.message === 'Hello!') // anything...
42+
```
43+
44+
Node 12 require massive validations.
45+
46+
```js
47+
if (objectExample && objectExample.foo && objectExample.foo.bar && objectExample.foo.bar.example === 'Hello!') // anything...
48+
```
49+
50+
In this case, using a validator is better and safe.
51+
52+
```js
53+
if (Validator.isValid(objectExample, [['foo.bar.example', 'Hello!']])) // anything...
54+
```
55+
56+
Now imagine validating multiple fields and values ​​from a single object in a old version of Node. Very tiring and verbose.

lib/validate.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@ class Validate {
22
/**
33
* @example
44
* const object = { foo: { bar: { message: 'Hello!' } } }
5-
* const isValid = Validate.isValid(object, ['foo', 'foo.bar.message'])
5+
* const isValid = Validate.isValid(object, [['foo'], ['foo.bar.message']])
66
* console.log(isValid) // returns true
77
*
8+
* @example
9+
* const object = { foo: { bar: { message: 'Hello!' } } }
10+
* const isValid = Validate.isValid(object, [['foo.bar.message', 'Wrong!']])
11+
* console.log(isValid) // returns false
12+
*
813
* @param {object} object Accepts an object schema.
914
* @param {string[]} fields Accepts string array. It is used to loop provided object and validate fields.
1015
*/
@@ -14,7 +19,7 @@ class Validate {
1419
object && typeof object === 'object' && Object.keys(object).length > 0
1520
if (!hasFields || !hasObject) return false
1621

17-
return fields.every((field) => {
22+
return fields.every(([field, value]) => {
1823
const splitedFields = field.split('.')
1924
const [splicedField] = splitedFields.splice(0, 1)
2025
const joinedFields = splitedFields.join('.')
@@ -23,9 +28,12 @@ class Validate {
2328
const existsFieldInObject = splicedField in object
2429

2530
if (existsFieldInObject && existsMoreFields) {
26-
return Validate.isValid(object[splicedField], [joinedFields])
31+
return Validate.isValid(object[splicedField], [[joinedFields, value]])
2732
}
2833

34+
const isNecessaryToCompareValues = !existsMoreFields && value
35+
if (isNecessaryToCompareValues) return value === object[splicedField]
36+
2937
return existsFieldInObject && !existsMoreFields
3038
})
3139
}

lib/validate.spec.js

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const makeObjectStub = () => {
1010
pendingPrinting: 'false',
1111
transactionId: '6788329C5763C2982A9537E6DD1D122D',
1212
responseBody: {
13-
total: '1',
13+
total: '0',
1414
entities: {
1515
entity: {}
1616
}
@@ -35,24 +35,24 @@ describe('validate', () => {
3535

3636
test('should return true if provided fields match with object', () => {
3737
const isValid = sut.isValid(objectStub, [
38-
'status',
39-
'data.status',
40-
'data.responseBody.entities.entity'
38+
['status'],
39+
['data.status'],
40+
['data.responseBody.entities.entity']
4141
])
4242
expect(isValid).toBeTruthy()
4343
})
4444

4545
test('should return true if repeated fields is provided to math with object', () => {
4646
const isValid = sut.isValid(objectStub, [
47-
'data.status',
48-
'data.status',
49-
'data.status'
47+
['data.status'],
48+
['data.status'],
49+
['data.status']
5050
])
5151
expect(isValid).toBeTruthy()
5252
})
5353

5454
test('should return false if provided fields dont exists in object', () => {
55-
const isValid = sut.isValid(objectStub, ['invalid', 'invalid.invalid'])
55+
const isValid = sut.isValid(objectStub, [['invalid'], ['invalid.invalid']])
5656
expect(isValid).toBeFalsy()
5757
})
5858

@@ -62,31 +62,54 @@ describe('validate', () => {
6262
})
6363

6464
test('should return false if provided object is empty', () => {
65-
const isValid = sut.isValid({}, ['any'])
65+
const isValid = sut.isValid({}, [['any']])
6666
expect(isValid).toBeFalsy()
6767
})
6868

6969
test('should return false if a unique field of provided fields not exists in object', () => {
7070
const isValid = sut.isValid(objectStub, [
71-
'status',
72-
'data.responseBody.total',
73-
'invalid'
71+
['status'],
72+
['data.responseBody.total'],
73+
['invalid']
7474
])
7575
expect(isValid).toBeFalsy()
7676
})
7777

7878
test('should calls isValid 4x with correctly params', () => {
7979
const spy = jest.spyOn(sut, 'isValid')
8080
sut.isValid(objectStub, [
81-
'status',
82-
'data.status',
83-
'data.responseBody.total'
81+
['status'],
82+
['data.status'],
83+
['data.responseBody.total']
8484
])
8585
expect(spy).toBeCalledTimes(4)
8686
expect(spy).toBeCalledWith(objectStub, [
87-
'status',
88-
'data.status',
89-
'data.responseBody.total'
87+
['status'],
88+
['data.status'],
89+
['data.responseBody.total']
9090
])
9191
})
92+
93+
test('should return true if provided field value match with object value', () => {
94+
const isValid = sut.isValid(objectStub, [
95+
['status', 200],
96+
['data.status', '1'],
97+
['data.responseBody.total', '0']
98+
])
99+
expect(isValid).toBeTruthy()
100+
})
101+
102+
test('should return false if provided field value dont match with object value', () => {
103+
const isValid = sut.isValid(objectStub, [['status', 400]])
104+
expect(isValid).toBeFalsy()
105+
})
106+
107+
test('should return false if 1 of 3 provided field value dont match with object value', () => {
108+
const isValid = sut.isValid(objectStub, [
109+
['status', 200],
110+
['data.status', '1'],
111+
['data.responseBody.total', 'wrong']
112+
])
113+
expect(isValid).toBeFalsy()
114+
})
92115
})

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
{
22
"name": "@mrbrunelli/object-validator",
3-
"version": "1.0.0",
3+
"version": "2.0.0",
44
"description": "A simple object validator for tiny schemas",
55
"main": "index.js",
66
"scripts": {
77
"test": "jest --runInBand --verbose --passWithNoTests",
8-
"test:watch": "yarn test --watch",
8+
"tdd": "yarn test --watch",
99
"test:ci": "jest --runInBand --verbose --coverage",
1010
"prepare": "husky install"
1111
},
@@ -14,7 +14,7 @@
1414
"url": "git+https://github.com/mrbrunelli/object-validator.git"
1515
},
1616
"keywords": ["validator", "schema", "object-validator", "schema-validator"],
17-
"author": "",
17+
"author": "Matheus R. Brunelli <matheus.brunelli@gmail.com>",
1818
"license": "MIT",
1919
"bugs": {
2020
"url": "https://github.com/mrbrunelli/object-validator/issues"

0 commit comments

Comments
 (0)