Compare commits

...

18 Commits

Author SHA1 Message Date
Jay
c767e73f0a Merge pull request 'hotfix/array-check-contains-type' (#20) from hotfix/array-check-contains-type into develop
All checks were successful
continuous-integration/drone/push Build is passing
Reviewed-on: #20
2020-07-05 05:31:07 +00:00
Jay
7d8256a6d6 bump version and changelog
All checks were successful
continuous-integration/drone/push Build is passing
2020-07-05 05:29:53 +00:00
Jay
b282f241a6 [fix] check contains type 2020-07-05 05:29:39 +00:00
Jay
0bfa3b32bd Merge pull request '[chore] bump version and changelog' (#18) from release/0.0.6 into develop
All checks were successful
continuous-integration/drone/push Build is passing
Reviewed-on: #18
2020-07-04 14:36:05 +00:00
Jay
b684a0e35e Merge pull request 'release/0.0.6' (#17) from release/0.0.6 into master
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
Reviewed-on: #17
2020-07-04 14:35:49 +00:00
Jay
d18c7cbad4 [chore] bump version and changelog
All checks were successful
continuous-integration/drone/push Build is passing
2020-07-04 14:34:49 +00:00
Jay
4ae4427e8e Merge pull request '[feat] add array validate contains type' (#16) from feature/add-array-contains-type-validate into develop
All checks were successful
continuous-integration/drone/push Build is passing
Reviewed-on: #16
2020-07-04 14:32:15 +00:00
Jay
a0e117c540 [feat] add array validate contains type
All checks were successful
continuous-integration/drone/push Build is passing
2020-07-04 14:30:08 +00:00
Jay
109f8a9655 Merge pull request 'bump version and changelog' (#15) from release/0.0.5 into develop
All checks were successful
continuous-integration/drone/push Build is passing
2020-06-11 15:08:04 +00:00
Jay
8645d9914b Merge pull request 'release/0.0.5' (#14) from release/0.0.5 into master
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
2020-06-11 15:07:49 +00:00
Jay
c1b8985461 bump version and changelog
All checks were successful
continuous-integration/drone/push Build is passing
2020-06-11 15:06:37 +00:00
Jay
8c3f9da11a Merge pull request '[feat] change require types method' (#13) from feature/change-types into develop
All checks were successful
continuous-integration/drone/push Build is passing
2020-06-11 15:05:46 +00:00
Jay
ca57604da8 [feat] change require types method
All checks were successful
continuous-integration/drone/push Build is passing
2020-06-11 15:04:57 +00:00
Jay
1f605ce18e Merge pull request 'bump version and changelog' (#12) from release/0.0.4 into develop
All checks were successful
continuous-integration/drone/push Build is passing
2020-06-11 14:53:08 +00:00
Jay
057466c952 Merge pull request 'release/0.0.4' (#11) from release/0.0.4 into master
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
2020-06-11 14:52:52 +00:00
Jay
564578c160 Merge pull request 'release/0.0.3' (#8) from release/0.0.3 into master
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
2020-06-10 14:21:12 +00:00
Jay
e399cb0e30 Merge pull request 'release/0.0.2' (#5) from release/0.0.2 into master
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
2020-06-10 13:39:05 +00:00
Jay
6fe8069868 Merge pull request 'Release v0.0.1' (#2) from release/v0.0.1 into master
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
2020-06-03 15:58:51 +00:00
5 changed files with 67 additions and 8 deletions

View File

@ -1,5 +1,17 @@
# ChangeLog # ChangeLog
## 2020-07-05 (0.0.7)
- fix array check contains type
## 2020-07-04 (0.0.6)
- add array validate contains type
## 2020-06-11 (0.0.5)
- modify types require method
## 2020-06-11 (0.0.4) ## 2020-06-11 (0.0.4)
- modify types export - modify types export

View File

@ -1,14 +1,19 @@
const types = require('./types/index.js') const TypeBase = require('./types/base.js')
const TypeString = require('./types/string.js')
const TypeNumber = require('./types/number.js')
const TypeBoolean = require('./types/boolean.js')
const TypeArray = require('./types/array.js')
const TypeObject = require('./types/object.js')
const validate = require('./validate.js') const validate = require('./validate.js')
const validator = {} const validator = {}
module.exports = validator module.exports = validator
validator.Base = types.Base validator.Base = TypeBase
validator.string = (...args) => new types.StringType(...args) validator.string = (...args) => new TypeString(...args)
validator.number = (...args) => new types.NumberType(...args) validator.number = (...args) => new TypeNumber(...args)
validator.boolean = (...args) => new types.BooleanType(...args) validator.boolean = (...args) => new TypeBoolean(...args)
validator.array = (...args) => new types.ArrayType(...args) validator.array = (...args) => new TypeArray(...args)
validator.object = (...args) => new types.ObjectType(...args) validator.object = (...args) => new TypeObject(...args)
/** /**
* validate * validate

View File

@ -1,6 +1,6 @@
{ {
"name": "mtfos-validator", "name": "mtfos-validator",
"version": "0.0.4", "version": "0.0.7",
"description": "", "description": "",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {

View File

@ -1,5 +1,6 @@
const Base = require('./base.js') const Base = require('./base.js')
const util = require('util') const util = require('util')
const validate = require('../validate.js')
class TypeArray extends Base { class TypeArray extends Base {
constructor () { constructor () {
@ -8,6 +9,10 @@ class TypeArray extends Base {
this._empty = false this._empty = false
this._min = null this._min = null
this._max = null this._max = null
/**
* @type {Base[]}
*/
this._itemTypes = []
} }
/** /**
@ -30,6 +35,11 @@ class TypeArray extends Base {
return this return this
} }
items (...args) {
this._itemTypes.push(...args)
return this
}
/** /**
* set max length * set max length
* @param {number} num * @param {number} num
@ -50,6 +60,25 @@ class TypeArray extends Base {
if (!this._empty && value.length === 0) return `not allow empty` if (!this._empty && value.length === 0) return `not allow empty`
if (this._min !== null && value.length < this._min) return `value length < ${this._min}` if (this._min !== null && value.length < this._min) return `value length < ${this._min}`
if (this._max !== null && value.length > this._max) return `value length > ${this._max}` if (this._max !== null && value.length > this._max) return `value length > ${this._max}`
if (this._itemTypes.length > 0) {
for (const item of value) {
let verified = false
let fail = ''
for (const type of this._itemTypes) {
const result = type.validate(item)
if (result) {
fail = result
} else {
verified = true
fail = ''
break
}
}
if (!verified || fail) return fail || `item type not match`
}
}
return null return null
} }
} }

View File

@ -1,5 +1,7 @@
/* eslint-disable no-undef */ /* eslint-disable no-undef */
const TypeArray = require('./array.js') const TypeArray = require('./array.js')
const TypeString = require('./string.js')
const TypeObject = require('./object.js')
describe('test validate schema type array', () => { describe('test validate schema type array', () => {
function throwFunc (val) { function throwFunc (val) {
@ -59,4 +61,15 @@ describe('test validate schema type array', () => {
expect(throwFunc(arr.validate([1, 2, 3]))).toThrow() expect(throwFunc(arr.validate([1, 2, 3]))).toThrow()
}) })
it('test array contains value', async () => {
const arr = new TypeArray().items(new TypeString())
expect(throwFunc(arr.validate([1, 2, 3]))).toThrow()
throwFunc(arr.validate(['asd']))
const arr2 = new TypeArray().items(new TypeString(), new TypeObject())
expect(throwFunc(arr2.validate([123, true]))).toThrow()
throwFunc(arr2.validate(['asd', '33', {}]))
})
}) })