Skip to content

Commit

Permalink
Merge pull request #6 from seekcx/fixed/validate-return
Browse files Browse the repository at this point in the history
修正了验证链上 validate 方法的返回值为最终值
  • Loading branch information
seekcx authored Aug 26, 2018
2 parents c3c81c7 + 313161c commit 8e26236
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 43 deletions.
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ declare namespace Acr {
}

class Chain {
validate(value: any): Promise<this>;
validate(value: any): Promise<any>;
transform(fn: (value: any) => Promise<any> | any): Promise<any>;
required(message?: string): this;
default(value: any): this;
Expand Down
2 changes: 1 addition & 1 deletion src/chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class Chain {
throw new ValidationError(result.detail);
}

return this;
return this.value();
}

async value() {
Expand Down
8 changes: 4 additions & 4 deletions test/acr.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
25 changes: 12 additions & 13 deletions test/rules/number.test.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
const test = require('ava');
const Acr = require('../../src');
const Chain = require('../../src/chain');

let acr;
test.beforeEach(() => {
acr = new Acr({ locale: 'en' });
});

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' })
Expand All @@ -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')
Expand All @@ -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')
Expand All @@ -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')
Expand All @@ -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')
Expand All @@ -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')
Expand Down
47 changes: 23 additions & 24 deletions test/rules/string.test.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
const test = require('ava');
const Acr = require('../../src');
const Chain = require('../../src/chain');

let acr;
test.beforeEach(() => {
acr = new Acr({ locale: 'en' });
});

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' })
Expand All @@ -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')
Expand All @@ -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')
Expand All @@ -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')
Expand All @@ -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('[email protected]');
t.true(chain instanceof Chain);
t.is(value, '[email protected]');

let promise = acr
.string('test')
Expand All @@ -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')
Expand All @@ -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')
Expand All @@ -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')
Expand All @@ -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')
Expand Down Expand Up @@ -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')
Expand All @@ -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')
Expand Down

0 comments on commit 8e26236

Please sign in to comment.