[feat] add string type pattern check, add object child check
continuous-integration/drone/push Build is passing

This commit is contained in:
Jay
2020-06-10 13:32:50 +00:00
parent 0dabcea445
commit 46b19757bd
13 changed files with 1647 additions and 50 deletions
+4 -4
View File
@@ -18,7 +18,7 @@ describe('test validate schema type array', () => {
it('test type array with required', async () => {
const arr = new TypeArray().required()
arr.validate([1, 2])
throwFunc(arr.validate([1, 2]))
expect(throwFunc(arr.validate(undefined))).toThrow()
})
@@ -26,7 +26,7 @@ describe('test validate schema type array', () => {
it('test type array with empty', async () => {
const arr = new TypeArray().empty()
arr.validate([])
throwFunc(arr.validate([]))
})
it('test type array without empty', async () => {
@@ -43,7 +43,7 @@ describe('test validate schema type array', () => {
expect(() => new TypeArray().min(-1)).toThrow()
expect(() => new TypeArray().max(1).min(2)).toThrow()
arr.validate([1, 2])
throwFunc(arr.validate([1, 2]))
expect(throwFunc(arr.validate([1]))).toThrow()
})
@@ -55,7 +55,7 @@ describe('test validate schema type array', () => {
expect(() => new TypeArray().max(false)).toThrow()
expect(() => new TypeArray().min(5).max(3)).toThrow()
arr.validate([1])
throwFunc(arr.validate([1]))
expect(throwFunc(arr.validate([1, 2, 3]))).toThrow()
})
+2 -1
View File
@@ -1,7 +1,8 @@
/**
* type builder interface
*
* @interface
* @class
* @exports
*/
class BaseType {
constructor () {
+1 -1
View File
@@ -11,7 +11,7 @@ describe('test validate schema type boolean', () => {
it('test type boolean required', async () => {
const bool = new TypeBoolean().required()
bool.validate(true)
throwFunc(bool.validate(true))
expect(throwFunc(bool.validate(undefined))).toThrow()
expect(throwFunc(bool.validate({}))).toThrow()
+5 -3
View File
@@ -11,7 +11,9 @@ describe('test validate schema type number', () => {
it('test type number with required', async () => {
const num = new TypeNumber().required()
num.validate(1)
throwFunc(num.validate(1))
throwFunc(num.validate(1.23))
expect(throwFunc(num.validate(undefined))).toThrow()
expect(throwFunc(num.validate({}))).toThrow()
@@ -24,7 +26,7 @@ describe('test validate schema type number', () => {
expect(() => new TypeNumber().min(false)).toThrow()
expect(() => new TypeNumber().max(1).min(2)).toThrow()
num.validate(2)
throwFunc(num.validate(2))
expect(throwFunc(num.validate(-1))).toThrow()
})
@@ -36,7 +38,7 @@ describe('test validate schema type number', () => {
expect(() => new TypeNumber().max(false)).toThrow()
expect(() => new TypeNumber().min(3).max(2)).toThrow()
num.validate(2)
throwFunc(num.validate(2))
expect(throwFunc(num.validate(3))).toThrow()
})
+18 -1
View File
@@ -1,10 +1,20 @@
const Base = require('./base.js')
const util = require('util')
const validate = require('../validate.js')
/**
* @typedef {import('../type.js').Schema} Schema
*/
class TypeObject extends Base {
constructor () {
/**
* @param {Object<string, Schema>} child
*/
constructor (child) {
super()
if (child && typeof child !== 'object') throw new Error('child type wrong')
this._type = 'object'
this._child = child && Object.keys(child).length > 0 ? child : null
}
validate (value) {
@@ -12,6 +22,13 @@ class TypeObject extends Base {
if (value === undefined) return null
/* eslint-disable-next-line */
if (typeof value !== this._type) return `${util.inspect(value, false, null)} type not ${this._type}`
if (this._child !== null) {
try {
validate(value, this._child)
} catch (err) {
return err.message
}
}
return null
}
}
+10 -1
View File
@@ -1,5 +1,6 @@
/* eslint-disable no-undef */
const TypeObject = require('./object.js')
const TypeString = require('./string.js')
describe('test validate type schema object', () => {
function throwFunc (val) {
@@ -11,9 +12,17 @@ describe('test validate type schema object', () => {
it('test object type with required', async () => {
const obj = new TypeObject().required()
obj.validate({})
throwFunc(obj.validate({}))
expect(throwFunc(obj.validate(undefined))).toThrow()
expect(throwFunc(obj.validate(false))).toThrow()
})
it('test object type with child', async () => {
const obj = new TypeObject({
name: new TypeString()
}).required()
throwFunc(obj.validate({ name: 'asd' }))
})
})
+13
View File
@@ -11,6 +11,7 @@ class TypeString extends Base {
this._max = null
this._min = null
this._empty = false
this._regexp = null
}
/**
@@ -43,6 +44,17 @@ class TypeString extends Base {
return this
}
/**
* set test pattern with regexp
* @param {RegExp} regexp
*/
pattern (regexp) {
if (!(regexp instanceof RegExp)) throw new Error('input wrong')
if (this._regexp !== null) throw new Error('regexp pattern already exist')
this._regexp = regexp
return this
}
validate (value) {
if (value === undefined && this._required) return `required`
if (value === undefined) return null
@@ -52,6 +64,7 @@ class TypeString extends Base {
const len = value.length
if (this._min !== null && len < this._min) return `value length < ${this._min}`
if (this._max !== null && len > this._max) return `value length > ${this._max}`
if (this._regexp !== null && !this._regexp.test(value)) return `value pattern not match ${this._regexp.toString()}`
return null
}
}
+14 -6
View File
@@ -10,24 +10,24 @@ describe('test validate type schema string', () => {
it('test string type without required', async () => {
const str = new TypeString()
str.validate(undefined)
throwFunc(str.validate(undefined))
})
it('test string type with required', async () => {
const str = new TypeString().required()
str.validate('123')
throwFunc(str.validate('123'))
expect(throwFunc(str.validate(undefined))).toThrow()
})
it('test string type with empty', async () => {
const str = new TypeString().empty().required()
str.validate('')
throwFunc(str.validate(''))
})
it('test string type without empty', async () => {
const str = new TypeString().required()
str.validate('asd')
throwFunc(str.validate('asd'))
expect(throwFunc(str.validate(''))).toThrow()
})
@@ -39,7 +39,7 @@ describe('test validate type schema string', () => {
expect(() => new TypeString().min(false)).toThrow()
expect(() => new TypeString().max(2).min(3)).toThrow()
str.validate('asd')
throwFunc(str.validate('asd'))
expect(throwFunc(str.validate('a'))).toThrow()
})
@@ -51,8 +51,16 @@ describe('test validate type schema string', () => {
expect(() => new TypeString().max(false)).toThrow()
expect(() => new TypeString().min(2).max(1)).toThrow()
str.validate('a')
throwFunc(str.validate('a'))
expect(throwFunc(str.validate('asd'))).toThrow()
})
it('test string type with pattern', async () => {
const str = new TypeString().required().pattern(/^asd$/)
expect(throwFunc(str.validate('dsa'))).toThrow()
throwFunc(str.validate('asd'))
})
})