Skip to content

Commit 563c401

Browse files
authored
Merge pull request #3 from mrbrunelli/fix/empty-value
fix/empty-value
2 parents c71b4a5 + ce23730 commit 563c401

3 files changed

Lines changed: 23 additions & 7 deletions

File tree

lib/validate.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,15 @@ class Validate {
2424
const [splicedField] = splitedFields.splice(0, 1)
2525
const joinedFields = splitedFields.join('.')
2626

27+
const existsValue = value !== undefined
2728
const existsMoreFields = !!joinedFields
2829
const existsFieldInObject = splicedField in object
2930

3031
if (existsFieldInObject && existsMoreFields) {
3132
return Validate.isValid(object[splicedField], [[joinedFields, value]])
3233
}
3334

34-
const isNecessaryToCompareValues = !existsMoreFields && value
35+
const isNecessaryToCompareValues = !existsMoreFields && existsValue
3536
if (isNecessaryToCompareValues) return value === object[splicedField]
3637

3738
return existsFieldInObject && !existsMoreFields

lib/validate.spec.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@ const makeObjectStub = () => {
55
status: 200,
66
data: {
77
serviceName: 'DatasetSP.save',
8-
error: '',
8+
error: null,
99
status: '1',
10-
pendingPrinting: 'false',
10+
pendingPrinting: false,
1111
transactionId: '6788329C5763C2982A9537E6DD1D122D',
1212
responseBody: {
13-
total: '0',
13+
total: 0,
1414
entities: {
15-
entity: {}
15+
entity: {
16+
data: ''
17+
}
1618
}
1719
}
1820
}
@@ -94,7 +96,10 @@ describe('validate', () => {
9496
const isValid = sut.isValid(objectStub, [
9597
['status', 200],
9698
['data.status', '1'],
97-
['data.responseBody.total', '0']
99+
['data.error', null],
100+
['data.pendingPrinting', false],
101+
['data.responseBody.total', 0],
102+
['data.responseBody.entities.entity.data', '']
98103
])
99104
expect(isValid).toBeTruthy()
100105
})
@@ -112,4 +117,14 @@ describe('validate', () => {
112117
])
113118
expect(isValid).toBeFalsy()
114119
})
120+
121+
test('should return false if provided field value is empty string, 0 or null and dont match with object value', () => {
122+
const isValid = sut.isValid(objectStub, [
123+
['status', ''],
124+
['status', 0],
125+
['status', null],
126+
['status', false]
127+
])
128+
expect(isValid).toBeFalsy()
129+
})
115130
})

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@mrbrunelli/object-validator",
3-
"version": "2.0.0",
3+
"version": "2.0.1",
44
"description": "A simple object validator for tiny schemas",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)