From 2a51ad2d9dddfbbdc2feb90f2f66c0e38377b26a Mon Sep 17 00:00:00 2001 From: seekcx Date: Sun, 26 Aug 2018 20:16:39 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E4=BA=86=20validate=20?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E7=9A=84=E8=BF=94=E5=9B=9E=E5=80=BC=E4=B8=BA?= =?UTF-8?q?=E6=9C=80=E7=BB=88=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/chain.js | 2 +- test/acr.test.js | 8 +++---- test/rules/number.test.js | 25 ++++++++++----------- test/rules/string.test.js | 47 +++++++++++++++++++-------------------- 4 files changed, 40 insertions(+), 42 deletions(-) diff --git a/src/chain.js b/src/chain.js index 50023ff..2fdfe81 100644 --- a/src/chain.js +++ b/src/chain.js @@ -135,7 +135,7 @@ class Chain { throw new ValidationError(result.detail); } - return this; + return this.value(); } async value() { diff --git a/test/acr.test.js b/test/acr.test.js index 3fa3a66..275af6b 100644 --- a/test/acr.test.js +++ b/test/acr.test.js @@ -75,16 +75,16 @@ test('async validate error', async t => { }); test('customize rule', async t => { - acr.type('test').define('foo', (value, { params }) => { + acr.type('test').define('equal', (value, { params }) => { return Promise.resolve(value === params[0] ? null : false); }); - const chain = await acr + const value = await acr .test() - .foo('abel') + .equal('abel') .validate('abel'); - t.true(chain instanceof Chain); + t.is(value, 'abel'); }); test('should be failed when return a string', async t => { diff --git a/test/rules/number.test.js b/test/rules/number.test.js index befd47d..9bf2f3a 100644 --- a/test/rules/number.test.js +++ b/test/rules/number.test.js @@ -1,6 +1,5 @@ const test = require('ava'); const Acr = require('../../src'); -const Chain = require('../../src/chain'); let acr; test.beforeEach(() => { @@ -8,11 +7,11 @@ test.beforeEach(() => { }); test('equal', async t => { - const chain = await acr + const value = await acr .number() .equal(12) .validate(12); - t.true(chain instanceof Chain); + t.is(value, 12); let promise = acr .number({ name: 'test' }) @@ -32,11 +31,11 @@ test('equal', async t => { }); test('max', async t => { - const chain = await acr + const value = await acr .number() .max(5) .validate(2); - t.true(chain instanceof Chain); + t.is(value, 2); let promise = acr .number('test') @@ -56,11 +55,11 @@ test('max', async t => { }); test('min', async t => { - const chain = await acr + const value = await acr .number() .min(5) .validate(8); - t.true(chain instanceof Chain); + t.is(value, 8); let promise = acr .number('test') @@ -80,11 +79,11 @@ test('min', async t => { }); test('positive', async t => { - const chain = await acr + const value = await acr .number() .positive() .validate(8); - t.true(chain instanceof Chain); + t.is(value, 8); let promise = acr .number('test') @@ -104,11 +103,11 @@ test('positive', async t => { }); test('negative', async t => { - const chain = await acr + const value = await acr .number() .negative() .validate(-2); - t.true(chain instanceof Chain); + t.is(value, -2); let promise = acr .number('test') @@ -128,11 +127,11 @@ test('negative', async t => { }); test('integer', async t => { - const chain = await acr + const value = await acr .number() .integer() .validate(8); - t.true(chain instanceof Chain); + t.is(value, 8); let promise = acr .number('test') diff --git a/test/rules/string.test.js b/test/rules/string.test.js index a9382a6..4dbdf69 100644 --- a/test/rules/string.test.js +++ b/test/rules/string.test.js @@ -1,6 +1,5 @@ const test = require('ava'); const Acr = require('../../src'); -const Chain = require('../../src/chain'); let acr; test.beforeEach(() => { @@ -8,11 +7,11 @@ test.beforeEach(() => { }); test('equal', async t => { - const chain = await acr + const value = await acr .string() .equal('foo') .validate('foo'); - t.true(chain instanceof Chain); + t.is(value, 'foo'); let promise = acr .string({ name: 'test' }) @@ -32,11 +31,11 @@ test('equal', async t => { }); test('max', async t => { - const chain = await acr + const value = await acr .string() .max(5) .validate('foo'); - t.true(chain instanceof Chain); + t.is(value, 'foo'); let promise = acr .string('test') @@ -56,11 +55,11 @@ test('max', async t => { }); test('min', async t => { - const chain = await acr + const value = await acr .string() .min(5) .validate('foobar'); - t.true(chain instanceof Chain); + t.is(value, 'foobar'); let promise = acr .string('test') @@ -80,11 +79,11 @@ test('min', async t => { }); test('length', async t => { - const chain = await acr + const value = await acr .string() .length(5) .validate('fooba'); - t.true(chain instanceof Chain); + t.is(value, 'fooba'); let promise = acr .string('test') @@ -97,18 +96,18 @@ test('length', async t => { promise = acr .string() .length(5, 'foobar') - .validate('foo'); + .validate('foobar'); error = await t.throws(promise); t.is(error.errors.message, 'foobar'); }); test('email', async t => { - const chain = await acr + const value = await acr .string() .email() .validate('abel@seek.cx'); - t.true(chain instanceof Chain); + t.is(value, 'abel@seek.cx'); let promise = acr .string('test') @@ -128,11 +127,11 @@ test('email', async t => { }); test('regex', async t => { - const chain = await acr + const value = await acr .string() .regex(/abel/) .validate('abel'); - t.true(chain instanceof Chain); + t.is(value, 'abel'); let promise = acr .string('test') @@ -152,11 +151,11 @@ test('regex', async t => { }); test('in', async t => { - const chain = await acr + const value = await acr .string() .in(['foo', 'bar']) .validate('foo'); - t.true(chain instanceof Chain); + t.is(value, 'foo'); let promise = acr .string('test') @@ -176,11 +175,11 @@ test('in', async t => { }); test('objectId', async t => { - const chain = await acr + const value = await acr .string() .objectId() .validate('5b2b5a1bcc6f21584075edc9'); - t.true(chain instanceof Chain); + t.is(value, '5b2b5a1bcc6f21584075edc9'); let promise = acr .string('test') @@ -200,11 +199,11 @@ test('objectId', async t => { }); test('base64', async t => { - const chain = await acr + const value = await acr .string() .base64() .validate('MTIzNA=='); - t.true(chain instanceof Chain); + t.is(value, 'MTIzNA=='); let promise = acr .string('test') @@ -232,11 +231,11 @@ test('base64', async t => { }); test('url', async t => { - const chain = await acr + const value = await acr .string() .url() .validate('https://seek.cx'); - t.true(chain instanceof Chain); + t.is(value, 'https://seek.cx'); let promise = acr .string('test') @@ -256,11 +255,11 @@ test('url', async t => { }); test('uuid', async t => { - const chain = await acr + const value = await acr .string() .uuid(4) .validate('b9a98908-436b-4481-9814-a0dd03ff1698'); - t.true(chain instanceof Chain); + t.is(value, 'b9a98908-436b-4481-9814-a0dd03ff1698'); let promise = acr .string('test') From 313161c3de38900a119b5e8e7ffa7a75f237e0c9 Mon Sep 17 00:00:00 2001 From: seekcx Date: Sun, 26 Aug 2018 20:24:37 +0800 Subject: [PATCH 2/2] =?UTF-8?q?chore:=20=E4=BF=AE=E5=A4=8D=20.d.ts?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index c249dc8..7f578d2 100644 --- a/index.d.ts +++ b/index.d.ts @@ -69,7 +69,7 @@ declare namespace Acr { } class Chain { - validate(value: any): Promise; + validate(value: any): Promise; transform(fn: (value: any) => Promise | any): Promise; required(message?: string): this; default(value: any): this;