Skip to content
This repository was archived by the owner on Apr 19, 2022. It is now read-only.

Commit b0ee84a

Browse files
Hany El Nokalythetutlage
Hany El Nokaly
authored andcommitted
feat(rule): add string validation rule
* Add 'string' to Validations * Add test cases for 'string' validations * Add docs for 'string' schema validation * fix 'string' skipping test cases
1 parent 7fc770f commit b0ee84a

File tree

4 files changed

+123
-0
lines changed

4 files changed

+123
-0
lines changed

docs/schema-rules.md

+10
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ indicative
3939
- [in](#in)
4040
- [includes](#includes)
4141
- [integer](#integer)
42+
- [string](#string)
4243
- [ip](#ip)
4344
- [ipv4](#ipv4)
4445
- [ipv6](#ipv6)
@@ -234,6 +235,15 @@ the value of field under validation should be an integer
234235
}
235236
```
236237

238+
### string
239+
the value of field under validation should be a string
240+
241+
```javascript,line-numbers
242+
{
243+
username: 'string'
244+
}
245+
```
246+
237247
### ip
238248
the value of field under validation should be a valid ip address
239249

readme.md

+10
Original file line numberDiff line numberDiff line change
@@ -1356,6 +1356,7 @@ indicative
13561356
[in](#in)
13571357
[includes](#includes)
13581358
[integer](#integer)
1359+
[string](#string)
13591360
[ip](#ip)
13601361
[ipv4](#ipv4)
13611362
[ipv6](#ipv6)
@@ -1553,6 +1554,15 @@ the value of field under validation should be an integer
15531554
}
15541555
```
15551556

1557+
#### string
1558+
the value of field under validation should be a string
1559+
1560+
```javascript
1561+
{
1562+
username: 'string'
1563+
}
1564+
```
1565+
15561566
#### ip
15571567
the value of field under validation should be a valid ip address
15581568

src/Validations/index.js

+27
Original file line numberDiff line numberDiff line change
@@ -1286,6 +1286,33 @@ Validations.regex = function (data, field, message, args, get) {
12861286
})
12871287
}
12881288

1289+
/**
1290+
* @description makes sure field under validation is a string
1291+
* @method regex
1292+
* @param {Object} data
1293+
* @param {String} field
1294+
* @param {String} message
1295+
* @param {Array} args
1296+
* @param {Function} get
1297+
* @return {Object}
1298+
* @public
1299+
*/
1300+
Validations.string = function (data, field, message, args, get) {
1301+
return new Promise(function (resolve, reject) {
1302+
const fieldValue = get(data, field)
1303+
if (skippable(fieldValue)) {
1304+
resolve('validation skipped')
1305+
return
1306+
}
1307+
1308+
if (Raw.string(fieldValue)) {
1309+
resolve('validation passed')
1310+
return
1311+
}
1312+
reject(message)
1313+
})
1314+
}
1315+
12891316
/**
12901317
* aliases
12911318
*/

test/validations.spec.js

+76
Original file line numberDiff line numberDiff line change
@@ -3106,5 +3106,81 @@ describe('Validations', function() {
31063106
expect(passes).to.equal('validation skipped')
31073107
})
31083108
})
3109+
3110+
context('String', function () {
3111+
///////////////////
3112+
// test suite 205 //
3113+
///////////////////
3114+
it('should work fine when the confirmed field is string', function * () {
3115+
const data = { username: 'david' }
3116+
const field = 'username'
3117+
const message = 'Username should be a string'
3118+
const get = _.get
3119+
const args = []
3120+
const passes = yield Validations.string(data, field, message, args, get)
3121+
expect(passes).to.equal('validation passed')
3122+
})
3123+
3124+
///////////////////
3125+
// test suite 206 //
3126+
///////////////////
3127+
it('should throw an error when the confirmed field is a number', function * () {
3128+
const data = { username: 1234 }
3129+
const field = 'username'
3130+
const message = 'Username should be a string'
3131+
const get = _.get
3132+
const args = []
3133+
try {
3134+
const passes = yield Validations.string(data, field, message, args, get)
3135+
expect(passes).not.to.exist()
3136+
} catch(e) {
3137+
expect(e).to.equal(message)
3138+
}
3139+
})
3140+
3141+
///////////////////
3142+
// test suite 207 //
3143+
///////////////////
3144+
it('should throw an error when the confirmed field is a boolean', function * () {
3145+
const data = { username: true }
3146+
const field = 'username'
3147+
const message = 'Username should be a string'
3148+
const get = _.get
3149+
const args = []
3150+
try {
3151+
const passes = yield Validations.string(data, field, message, args, get)
3152+
expect(passes).not.to.exist()
3153+
} catch(e) {
3154+
expect(e).to.equal(message)
3155+
}
3156+
})
3157+
3158+
///////////////////
3159+
// test suite 208 //
3160+
///////////////////
3161+
it('should skip validation when field value is not defined', function * () {
3162+
const data = { }
3163+
const field = 'username'
3164+
const message = 'Username should be a string'
3165+
const get = _.get
3166+
const args = []
3167+
const passes = yield Validations.string(data, field, message, args, get)
3168+
expect(passes).to.equal('validation skipped')
3169+
})
3170+
3171+
///////////////////
3172+
// test suite 209 //
3173+
///////////////////
3174+
it('should skip validation when field value is undefined', function * () {
3175+
const data = { username: undefined }
3176+
const field = 'username'
3177+
const message = 'Username should be a string'
3178+
const get = _.get
3179+
const args = []
3180+
const passes = yield Validations.string(data, field, message, args, get)
3181+
expect(passes).to.equal('validation skipped')
3182+
})
3183+
})
3184+
31093185
})
31103186

0 commit comments

Comments
 (0)