Compare commits
No commits in common. "e399cb0e30bf448e0766759180a645fdd399e151" and "6fe8069868384ee3bbdac2f60d69d1ea583a0147" have entirely different histories.
e399cb0e30
...
6fe8069868
@ -1,11 +1,5 @@
|
|||||||
# ChangeLog
|
# ChangeLog
|
||||||
|
|
||||||
## 2020-06-10 (0.0.2)
|
## 2020-06-03
|
||||||
|
|
||||||
- add string type pattern check function
|
|
||||||
- add object type child validate
|
|
||||||
- modify test
|
|
||||||
|
|
||||||
## 2020-06-03 (0.0.1)
|
|
||||||
|
|
||||||
- first version
|
- first version
|
||||||
|
35
index.js
35
index.js
@ -1,5 +1,5 @@
|
|||||||
const types = require('./types/index.js')
|
const types = require('./types/index.js')
|
||||||
const validate = require('./validate.js')
|
const BaseType = require('./types/base.js') // eslint-disable-line
|
||||||
const validator = {}
|
const validator = {}
|
||||||
module.exports = validator
|
module.exports = validator
|
||||||
|
|
||||||
@ -10,9 +10,38 @@ validator.boolean = () => new types.BooleanType()
|
|||||||
validator.array = () => new types.ArrayType()
|
validator.array = () => new types.ArrayType()
|
||||||
validator.object = () => new types.ObjectType()
|
validator.object = () => new types.ObjectType()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @exports
|
||||||
|
* @typedef SchemaField
|
||||||
|
* @property {BaseType} type
|
||||||
|
* @property {SchemaField} children
|
||||||
|
*/
|
||||||
|
/**
|
||||||
|
* @exports
|
||||||
|
* @typedef {Object<string, SchemaField|BaseType>} Schema
|
||||||
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* validate
|
* validate
|
||||||
* @param {Object} src
|
* @param {Object} src
|
||||||
* @param {import('./type.js').Schema} schema
|
* @param {Schema} schema
|
||||||
*/
|
*/
|
||||||
validator.validate = validate
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
1535
package-lock.json
generated
1535
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "mtfos-validator",
|
"name": "mtfos-validator",
|
||||||
"version": "0.0.2",
|
"version": "0.0.1",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
@ -10,7 +10,6 @@
|
|||||||
"author": "",
|
"author": "",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"jest": "^26.0.1",
|
"jest": "^26.0.1"
|
||||||
"standard": "^12.0.1"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
13
type.js
13
type.js
@ -1,13 +0,0 @@
|
|||||||
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 () => {
|
it('test type array with required', async () => {
|
||||||
const arr = new TypeArray().required()
|
const arr = new TypeArray().required()
|
||||||
|
|
||||||
throwFunc(arr.validate([1, 2]))
|
arr.validate([1, 2])
|
||||||
|
|
||||||
expect(throwFunc(arr.validate(undefined))).toThrow()
|
expect(throwFunc(arr.validate(undefined))).toThrow()
|
||||||
})
|
})
|
||||||
@ -26,7 +26,7 @@ describe('test validate schema type array', () => {
|
|||||||
it('test type array with empty', async () => {
|
it('test type array with empty', async () => {
|
||||||
const arr = new TypeArray().empty()
|
const arr = new TypeArray().empty()
|
||||||
|
|
||||||
throwFunc(arr.validate([]))
|
arr.validate([])
|
||||||
})
|
})
|
||||||
|
|
||||||
it('test type array without empty', async () => {
|
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().min(-1)).toThrow()
|
||||||
expect(() => new TypeArray().max(1).min(2)).toThrow()
|
expect(() => new TypeArray().max(1).min(2)).toThrow()
|
||||||
|
|
||||||
throwFunc(arr.validate([1, 2]))
|
arr.validate([1, 2])
|
||||||
|
|
||||||
expect(throwFunc(arr.validate([1]))).toThrow()
|
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().max(false)).toThrow()
|
||||||
expect(() => new TypeArray().min(5).max(3)).toThrow()
|
expect(() => new TypeArray().min(5).max(3)).toThrow()
|
||||||
|
|
||||||
throwFunc(arr.validate([1]))
|
arr.validate([1])
|
||||||
|
|
||||||
expect(throwFunc(arr.validate([1, 2, 3]))).toThrow()
|
expect(throwFunc(arr.validate([1, 2, 3]))).toThrow()
|
||||||
})
|
})
|
||||||
|
@ -1,8 +1,7 @@
|
|||||||
/**
|
/**
|
||||||
* type builder interface
|
* type builder interface
|
||||||
*
|
*
|
||||||
* @class
|
* @interface
|
||||||
* @exports
|
|
||||||
*/
|
*/
|
||||||
class BaseType {
|
class BaseType {
|
||||||
constructor () {
|
constructor () {
|
||||||
|
@ -11,7 +11,7 @@ describe('test validate schema type boolean', () => {
|
|||||||
it('test type boolean required', async () => {
|
it('test type boolean required', async () => {
|
||||||
const bool = new TypeBoolean().required()
|
const bool = new TypeBoolean().required()
|
||||||
|
|
||||||
throwFunc(bool.validate(true))
|
bool.validate(true)
|
||||||
|
|
||||||
expect(throwFunc(bool.validate(undefined))).toThrow()
|
expect(throwFunc(bool.validate(undefined))).toThrow()
|
||||||
expect(throwFunc(bool.validate({}))).toThrow()
|
expect(throwFunc(bool.validate({}))).toThrow()
|
||||||
|
@ -11,9 +11,7 @@ describe('test validate schema type number', () => {
|
|||||||
it('test type number with required', async () => {
|
it('test type number with required', async () => {
|
||||||
const num = new TypeNumber().required()
|
const num = new TypeNumber().required()
|
||||||
|
|
||||||
throwFunc(num.validate(1))
|
num.validate(1)
|
||||||
|
|
||||||
throwFunc(num.validate(1.23))
|
|
||||||
|
|
||||||
expect(throwFunc(num.validate(undefined))).toThrow()
|
expect(throwFunc(num.validate(undefined))).toThrow()
|
||||||
expect(throwFunc(num.validate({}))).toThrow()
|
expect(throwFunc(num.validate({}))).toThrow()
|
||||||
@ -26,7 +24,7 @@ describe('test validate schema type number', () => {
|
|||||||
expect(() => new TypeNumber().min(false)).toThrow()
|
expect(() => new TypeNumber().min(false)).toThrow()
|
||||||
expect(() => new TypeNumber().max(1).min(2)).toThrow()
|
expect(() => new TypeNumber().max(1).min(2)).toThrow()
|
||||||
|
|
||||||
throwFunc(num.validate(2))
|
num.validate(2)
|
||||||
|
|
||||||
expect(throwFunc(num.validate(-1))).toThrow()
|
expect(throwFunc(num.validate(-1))).toThrow()
|
||||||
})
|
})
|
||||||
@ -38,7 +36,7 @@ describe('test validate schema type number', () => {
|
|||||||
expect(() => new TypeNumber().max(false)).toThrow()
|
expect(() => new TypeNumber().max(false)).toThrow()
|
||||||
expect(() => new TypeNumber().min(3).max(2)).toThrow()
|
expect(() => new TypeNumber().min(3).max(2)).toThrow()
|
||||||
|
|
||||||
throwFunc(num.validate(2))
|
num.validate(2)
|
||||||
|
|
||||||
expect(throwFunc(num.validate(3))).toThrow()
|
expect(throwFunc(num.validate(3))).toThrow()
|
||||||
})
|
})
|
||||||
|
@ -1,20 +1,10 @@
|
|||||||
const Base = require('./base.js')
|
const Base = require('./base.js')
|
||||||
const util = require('util')
|
const util = require('util')
|
||||||
const validate = require('../validate.js')
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @typedef {import('../type.js').Schema} Schema
|
|
||||||
*/
|
|
||||||
|
|
||||||
class TypeObject extends Base {
|
class TypeObject extends Base {
|
||||||
/**
|
constructor () {
|
||||||
* @param {Object<string, Schema>} child
|
|
||||||
*/
|
|
||||||
constructor (child) {
|
|
||||||
super()
|
super()
|
||||||
if (child && typeof child !== 'object') throw new Error('child type wrong')
|
|
||||||
this._type = 'object'
|
this._type = 'object'
|
||||||
this._child = child && Object.keys(child).length > 0 ? child : null
|
|
||||||
}
|
}
|
||||||
|
|
||||||
validate (value) {
|
validate (value) {
|
||||||
@ -22,13 +12,6 @@ class TypeObject extends Base {
|
|||||||
if (value === undefined) return null
|
if (value === undefined) return null
|
||||||
/* eslint-disable-next-line */
|
/* eslint-disable-next-line */
|
||||||
if (typeof value !== this._type) return `${util.inspect(value, false, null)} type not ${this._type}`
|
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
|
return null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
/* eslint-disable no-undef */
|
/* eslint-disable no-undef */
|
||||||
const TypeObject = require('./object.js')
|
const TypeObject = require('./object.js')
|
||||||
const TypeString = require('./string.js')
|
|
||||||
|
|
||||||
describe('test validate type schema object', () => {
|
describe('test validate type schema object', () => {
|
||||||
function throwFunc (val) {
|
function throwFunc (val) {
|
||||||
@ -12,17 +11,9 @@ describe('test validate type schema object', () => {
|
|||||||
it('test object type with required', async () => {
|
it('test object type with required', async () => {
|
||||||
const obj = new TypeObject().required()
|
const obj = new TypeObject().required()
|
||||||
|
|
||||||
throwFunc(obj.validate({}))
|
obj.validate({})
|
||||||
|
|
||||||
expect(throwFunc(obj.validate(undefined))).toThrow()
|
expect(throwFunc(obj.validate(undefined))).toThrow()
|
||||||
expect(throwFunc(obj.validate(false))).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,7 +11,6 @@ class TypeString extends Base {
|
|||||||
this._max = null
|
this._max = null
|
||||||
this._min = null
|
this._min = null
|
||||||
this._empty = false
|
this._empty = false
|
||||||
this._regexp = null
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -44,17 +43,6 @@ class TypeString extends Base {
|
|||||||
return this
|
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) {
|
validate (value) {
|
||||||
if (value === undefined && this._required) return `required`
|
if (value === undefined && this._required) return `required`
|
||||||
if (value === undefined) return null
|
if (value === undefined) return null
|
||||||
@ -64,7 +52,6 @@ class TypeString extends Base {
|
|||||||
const len = value.length
|
const len = value.length
|
||||||
if (this._min !== null && len < this._min) return `value length < ${this._min}`
|
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._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
|
return null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,24 +10,24 @@ describe('test validate type schema string', () => {
|
|||||||
|
|
||||||
it('test string type without required', async () => {
|
it('test string type without required', async () => {
|
||||||
const str = new TypeString()
|
const str = new TypeString()
|
||||||
throwFunc(str.validate(undefined))
|
str.validate(undefined)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('test string type with required', async () => {
|
it('test string type with required', async () => {
|
||||||
const str = new TypeString().required()
|
const str = new TypeString().required()
|
||||||
throwFunc(str.validate('123'))
|
str.validate('123')
|
||||||
|
|
||||||
expect(throwFunc(str.validate(undefined))).toThrow()
|
expect(throwFunc(str.validate(undefined))).toThrow()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('test string type with empty', async () => {
|
it('test string type with empty', async () => {
|
||||||
const str = new TypeString().empty().required()
|
const str = new TypeString().empty().required()
|
||||||
throwFunc(str.validate(''))
|
str.validate('')
|
||||||
})
|
})
|
||||||
|
|
||||||
it('test string type without empty', async () => {
|
it('test string type without empty', async () => {
|
||||||
const str = new TypeString().required()
|
const str = new TypeString().required()
|
||||||
throwFunc(str.validate('asd'))
|
str.validate('asd')
|
||||||
|
|
||||||
expect(throwFunc(str.validate(''))).toThrow()
|
expect(throwFunc(str.validate(''))).toThrow()
|
||||||
})
|
})
|
||||||
@ -39,7 +39,7 @@ describe('test validate type schema string', () => {
|
|||||||
expect(() => new TypeString().min(false)).toThrow()
|
expect(() => new TypeString().min(false)).toThrow()
|
||||||
expect(() => new TypeString().max(2).min(3)).toThrow()
|
expect(() => new TypeString().max(2).min(3)).toThrow()
|
||||||
|
|
||||||
throwFunc(str.validate('asd'))
|
str.validate('asd')
|
||||||
|
|
||||||
expect(throwFunc(str.validate('a'))).toThrow()
|
expect(throwFunc(str.validate('a'))).toThrow()
|
||||||
})
|
})
|
||||||
@ -51,16 +51,8 @@ describe('test validate type schema string', () => {
|
|||||||
expect(() => new TypeString().max(false)).toThrow()
|
expect(() => new TypeString().max(false)).toThrow()
|
||||||
expect(() => new TypeString().min(2).max(1)).toThrow()
|
expect(() => new TypeString().min(2).max(1)).toThrow()
|
||||||
|
|
||||||
throwFunc(str.validate('a'))
|
str.validate('a')
|
||||||
|
|
||||||
expect(throwFunc(str.validate('asd'))).toThrow()
|
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
27
validate.js
@ -1,27 +0,0 @@
|
|||||||
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