1 Commits

Author SHA1 Message Date
root 6fe8069868 Merge pull request 'Release v0.0.1' (#2) from release/v0.0.1 into master
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
2020-06-03 15:58:51 +00:00
16 changed files with 74 additions and 1711 deletions
+1 -20
View File
@@ -1,24 +1,5 @@
# ChangeLog # ChangeLog
## 2020-06-11 (0.0.5) ## 2020-06-03
- modify types require method
## 2020-06-11 (0.0.4)
- modify types export
- add package repository
## 2020-06-10 (0.0.3)
- add types init value
## 2020-06-10 (0.0.2)
- add string type pattern check function
- add object type child validate
- modify test
## 2020-06-03 (0.0.1)
- first version - first version
+39 -15
View File
@@ -1,23 +1,47 @@
const TypeBase = require('./types/base.js') const types = require('./types/index.js')
const TypeString = require('./types/string.js') const BaseType = require('./types/base.js') // eslint-disable-line
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 validator = {} const validator = {}
module.exports = validator module.exports = validator
validator.Base = TypeBase validator.Base = types.Base
validator.string = (...args) => new TypeString(...args) validator.string = () => new types.StringType()
validator.number = (...args) => new TypeNumber(...args) validator.number = () => new types.NumberType()
validator.boolean = (...args) => new TypeBoolean(...args) validator.boolean = () => new types.BooleanType()
validator.array = (...args) => new TypeArray(...args) validator.array = () => new types.ArrayType()
validator.object = (...args) => new TypeObject(...args) 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
View File
File diff suppressed because it is too large Load Diff
+2 -7
View File
@@ -1,6 +1,6 @@
{ {
"name": "mtfos-validator", "name": "mtfos-validator",
"version": "0.0.5", "version": "0.0.1",
"description": "", "description": "",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
@@ -10,11 +10,6 @@
"author": "", "author": "",
"license": "ISC", "license": "ISC",
"devDependencies": { "devDependencies": {
"jest": "^26.0.1", "jest": "^26.0.1"
"standard": "^12.0.1"
},
"repository": {
"type": "git",
"url": "https://git.trj.tw/nodejs/validator.git"
} }
} }
-13
View File
@@ -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 = {}
+4 -4
View File
@@ -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 -2
View File
@@ -1,8 +1,7 @@
/** /**
* type builder interface * type builder interface
* *
* @class * @interface
* @exports
*/ */
class BaseType { class BaseType {
constructor () { constructor () {
+1 -1
View File
@@ -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()
+15 -8
View File
@@ -1,8 +1,15 @@
const types = {} const Base = require('./base.js')
module.exports = types const StringType = require('./string.js')
types.Base = require('./base.js') const NumberType = require('./number.js')
types.StringType = require('./string.js') const BooleanType = require('./boolean.js')
types.NumberType = require('./number.js') const ObjectType = require('./object.js')
types.BooleanType = require('./boolean.js') const ArrayType = require('./array.js')
types.ObjectType = require('./object.js')
types.ArrayType = require('./array.js') module.exports = {
Base,
StringType,
NumberType,
BooleanType,
ObjectType,
ArrayType
}
+3 -5
View File
@@ -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 -18
View File
@@ -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 -12
View File
@@ -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,19 +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' }))
expect(throwFunc(obj.validate({ name: 123 }))).toThrow()
})
}) })
-13
View File
@@ -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
} }
} }
+6 -14
View File
@@ -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
View File
@@ -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
-17
View File
@@ -23,21 +23,4 @@ describe('test validator validate', () => {
expect(() => validator.validate(data, schema)).toThrow() expect(() => validator.validate(data, schema)).toThrow()
}) })
test('validate test 3', () => {
const data = {
str: '123',
strWithPattern: 'asd',
objWithChild: {
name: 'asd'
}
}
const schema = {
str: validator.string().required(),
strWithPattern: validator.string().pattern(/^asd$/).required(),
objWithChild: validator.object({ name: validator.string().required() }).required()
}
validator.validate(data, schema)
})
}) })