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

Commit

Permalink
feat(rule): add string validation rule
Browse files Browse the repository at this point in the history
* Add 'string' to Validations

* Add test cases for 'string' validations

* Add docs for 'string' schema validation

* fix 'string' skipping test cases
  • Loading branch information
Hany El Nokaly authored and thetutlage committed Sep 18, 2016
1 parent 7fc770f commit b0ee84a
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 0 deletions.
10 changes: 10 additions & 0 deletions docs/schema-rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ indicative
- [in](#in)
- [includes](#includes)
- [integer](#integer)
- [string](#string)
- [ip](#ip)
- [ipv4](#ipv4)
- [ipv6](#ipv6)
Expand Down Expand Up @@ -234,6 +235,15 @@ the value of field under validation should be an integer
}
```

### string
the value of field under validation should be a string

```javascript,line-numbers
{
username: 'string'
}
```

### ip
the value of field under validation should be a valid ip address

Expand Down
10 changes: 10 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -1356,6 +1356,7 @@ indicative
[in](#in)
[includes](#includes)
[integer](#integer)
[string](#string)
[ip](#ip)
[ipv4](#ipv4)
[ipv6](#ipv6)
Expand Down Expand Up @@ -1553,6 +1554,15 @@ the value of field under validation should be an integer
}
```

#### string
the value of field under validation should be a string

```javascript
{
username: 'string'
}
```

#### ip
the value of field under validation should be a valid ip address

Expand Down
27 changes: 27 additions & 0 deletions src/Validations/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1286,6 +1286,33 @@ Validations.regex = function (data, field, message, args, get) {
})
}

/**
* @description makes sure field under validation is a string
* @method regex
* @param {Object} data
* @param {String} field
* @param {String} message
* @param {Array} args
* @param {Function} get
* @return {Object}
* @public
*/
Validations.string = function (data, field, message, args, get) {
return new Promise(function (resolve, reject) {
const fieldValue = get(data, field)
if (skippable(fieldValue)) {
resolve('validation skipped')
return
}

if (Raw.string(fieldValue)) {
resolve('validation passed')
return
}
reject(message)
})
}

/**
* aliases
*/
Expand Down
76 changes: 76 additions & 0 deletions test/validations.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3106,5 +3106,81 @@ describe('Validations', function() {
expect(passes).to.equal('validation skipped')
})
})

context('String', function () {
///////////////////
// test suite 205 //
///////////////////
it('should work fine when the confirmed field is string', function * () {
const data = { username: 'david' }
const field = 'username'
const message = 'Username should be a string'
const get = _.get
const args = []
const passes = yield Validations.string(data, field, message, args, get)
expect(passes).to.equal('validation passed')
})

///////////////////
// test suite 206 //
///////////////////
it('should throw an error when the confirmed field is a number', function * () {
const data = { username: 1234 }
const field = 'username'
const message = 'Username should be a string'
const get = _.get
const args = []
try {
const passes = yield Validations.string(data, field, message, args, get)
expect(passes).not.to.exist()
} catch(e) {
expect(e).to.equal(message)
}
})

///////////////////
// test suite 207 //
///////////////////
it('should throw an error when the confirmed field is a boolean', function * () {
const data = { username: true }
const field = 'username'
const message = 'Username should be a string'
const get = _.get
const args = []
try {
const passes = yield Validations.string(data, field, message, args, get)
expect(passes).not.to.exist()
} catch(e) {
expect(e).to.equal(message)
}
})

///////////////////
// test suite 208 //
///////////////////
it('should skip validation when field value is not defined', function * () {
const data = { }
const field = 'username'
const message = 'Username should be a string'
const get = _.get
const args = []
const passes = yield Validations.string(data, field, message, args, get)
expect(passes).to.equal('validation skipped')
})

///////////////////
// test suite 209 //
///////////////////
it('should skip validation when field value is undefined', function * () {
const data = { username: undefined }
const field = 'username'
const message = 'Username should be a string'
const get = _.get
const args = []
const passes = yield Validations.string(data, field, message, args, get)
expect(passes).to.equal('validation skipped')
})
})

})

0 comments on commit b0ee84a

Please # to comment.