Compare commits
2 Commits
0dabcea445
...
6a092ce0b7
Author | SHA1 | Date | |
---|---|---|---|
6a092ce0b7 | |||
46b19757bd |
35
index.js
35
index.js
@ -1,5 +1,5 @@
|
||||
const types = require('./types/index.js')
|
||||
const BaseType = require('./types/base.js') // eslint-disable-line
|
||||
const validate = require('./validate.js')
|
||||
const validator = {}
|
||||
module.exports = validator
|
||||
|
||||
@ -10,38 +10,9 @@ validator.boolean = () => new types.BooleanType()
|
||||
validator.array = () => new types.ArrayType()
|
||||
validator.object = () => new types.ObjectType()
|
||||
|
||||
/**
|
||||
* @exports
|
||||
* @typedef SchemaField
|
||||
* @property {BaseType} type
|
||||
* @property {SchemaField} children
|
||||
*/
|
||||
/**
|
||||
* @exports
|
||||
* @typedef {Object<string, SchemaField|BaseType>} Schema
|
||||
*/
|
||||
|
||||
/**
|
||||
* validate
|
||||
* @param {Object} src
|
||||
* @param {Schema} schema
|
||||
* @param {import('./type.js').Schema} schema
|
||||
*/
|
||||
validator.validate = function (src, schema) {
|
||||
if (typeof src !== 'object' || typeof schema !== 'object') throw new Error('source or schema not object')
|
||||
|
||||
for (const it in schema) {
|
||||
if (schema[it] instanceof BaseType) {
|
||||
const result = schema[it].validate(src[it])
|
||||
if (result) throw new Error(`field [${it}]: ${result}`)
|
||||
} else if (typeof schema[it] === 'object' && 'type' in schema[it] && schema[it].type instanceof BaseType) {
|
||||
const result = schema[it].type.validate(src[it])
|
||||
if (result) throw new Error(`field [${it}]: ${result}`)
|
||||
} else {
|
||||
throw new Error('not get any check schema type')
|
||||
}
|
||||
|
||||
if (typeof schema[it] === 'object' && 'children' in schema[it]) {
|
||||
validator.validate(src[it], schema[it].children)
|
||||
}
|
||||
}
|
||||
}
|
||||
validator.validate = validate
|
||||
|
1535
package-lock.json
generated
1535
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -10,6 +10,7 @@
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"devDependencies": {
|
||||
"jest": "^26.0.1"
|
||||
"jest": "^26.0.1",
|
||||
"standard": "^12.0.1"
|
||||
}
|
||||
}
|
||||
|
13
type.js
Normal file
13
type.js
Normal file
@ -0,0 +1,13 @@
|
||||
const BaseType = require('./types/base.js') // eslint-disable-line
|
||||
|
||||
/**
|
||||
* @exports
|
||||
* @typedef SchemaField
|
||||
* @property {BaseType} type
|
||||
* @property {SchemaField} children
|
||||
*/
|
||||
/**
|
||||
* @exports
|
||||
* @typedef {Object<string, SchemaField|BaseType>} Schema
|
||||
*/
|
||||
module.exports = {}
|
@ -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()
|
||||
})
|
||||
|
@ -1,7 +1,8 @@
|
||||
/**
|
||||
* type builder interface
|
||||
*
|
||||
* @interface
|
||||
* @class
|
||||
* @exports
|
||||
*/
|
||||
class BaseType {
|
||||
constructor () {
|
||||
|
@ -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()
|
||||
|
@ -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()
|
||||
})
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
@ -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' }))
|
||||
})
|
||||
})
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
@ -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'))
|
||||
})
|
||||
})
|
||||
|
27
validate.js
Normal file
27
validate.js
Normal file
@ -0,0 +1,27 @@
|
||||
const BaseType = require('./types/base.js')
|
||||
|
||||
/**
|
||||
* validate
|
||||
* @param {Object} src
|
||||
* @param {import('./type.js').Schema} schema
|
||||
*/
|
||||
function validate (src, schema) {
|
||||
if (typeof src !== 'object' || typeof schema !== 'object') throw new Error('source or schema not object')
|
||||
for (const it in schema) {
|
||||
if (schema[it] instanceof BaseType) {
|
||||
const result = schema[it].validate(src[it])
|
||||
if (result) throw new Error(`field [${it}]: ${result}`)
|
||||
} else if (typeof schema[it] === 'object' && 'type' in schema[it] && schema[it].type instanceof BaseType) {
|
||||
const result = schema[it].type.validate(src[it])
|
||||
if (result) throw new Error(`field [${it}]: ${result}`)
|
||||
} else {
|
||||
throw new Error('not get any check schema type')
|
||||
}
|
||||
|
||||
if (typeof schema[it] === 'object' && 'children' in schema[it]) {
|
||||
validate(src[it], schema[it].children)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = validate
|
Loading…
Reference in New Issue
Block a user