From 464e42ba1cc77a7b3172277061867e8aafcb8815 Mon Sep 17 00:00:00 2001 From: Jay Date: Wed, 10 Jun 2020 14:13:34 +0000 Subject: [PATCH 1/2] [fix] add types init values --- index.js | 10 +++++----- validator.test.js | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 803f21c..42d0543 100644 --- a/index.js +++ b/index.js @@ -4,11 +4,11 @@ const validator = {} module.exports = validator validator.Base = types.Base -validator.string = () => new types.StringType() -validator.number = () => new types.NumberType() -validator.boolean = () => new types.BooleanType() -validator.array = () => new types.ArrayType() -validator.object = () => new types.ObjectType() +validator.string = (...args) => new types.StringType(...args) +validator.number = (...args) => new types.NumberType(...args) +validator.boolean = (...args) => new types.BooleanType(...args) +validator.array = (...args) => new types.ArrayType(...args) +validator.object = (...args) => new types.ObjectType(...args) /** * validate diff --git a/validator.test.js b/validator.test.js index 51ac8cf..aa973e0 100644 --- a/validator.test.js +++ b/validator.test.js @@ -23,4 +23,21 @@ describe('test validator validate', () => { 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) + }) }) -- 2.45.2 From cdbecea36d61c1af7f46524647e8cbe3c949c9cf Mon Sep 17 00:00:00 2001 From: Jay Date: Wed, 10 Jun 2020 14:15:02 +0000 Subject: [PATCH 2/2] [chore] add test set --- types/object.test.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/types/object.test.js b/types/object.test.js index 4e8cce8..2c9a573 100644 --- a/types/object.test.js +++ b/types/object.test.js @@ -24,5 +24,7 @@ describe('test validate type schema object', () => { }).required() throwFunc(obj.validate({ name: 'asd' })) + + expect(throwFunc(obj.validate({ name: 123 }))).toThrow() }) }) -- 2.45.2