Skip to content

Commit

Permalink
Merge pull request #8 from alibaba-fusion/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
jdkahn authored Aug 1, 2019
2 parents ec8d637 + d71bbc1 commit 1dadad1
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
"表单"
]
},
"homepage": "https://github.com/alibaba-fusion/validate",
"homepage": "https://unpkg.com/@alifd/validate@1.1.0/build/index.html",
"bugs": "https://github.com/alibaba-fusion/validate/issues",
"publishConfig": {
"access": "public"
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class Schema {

const res = rule.validator(rule, data.value, cb, this._options);
if (res && res.then) {
res.then(() => cb(), e => cb(e));
res.then(e => cb(e), e => cb(e));
}
},
results => {
Expand Down
112 changes: 108 additions & 4 deletions test/validator.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,44 @@ describe('validator', () => {
}
);
});

it('passes value on resolve', done => {
new Schema({
v: [
{
validator(rule, value, callback) {
callback(new Error('e1'));
},
},
{
validator(rule, value, callback) {
callback(new Error('e2'));
},
},
],
v2: [
{
validator(rule, value, callback) {
callback(new Error('e3'));
},
},
],
}).validate(
{
v: 2,
},
errors => {
assert(errors.length === 2);
assert(errors[0].message === 'e1');
assert(errors[1].message === 'e3');
done();
}
);
});
});

describe('promise validator', () => {
it('works', done => {
it('works with reject', done => {
new Schema({
v: [
{
Expand Down Expand Up @@ -142,6 +176,40 @@ describe('promise validator', () => {
);
});

it('works with resolve', done => {
new Schema({
v: [
{
validator() {
return Promise.resolve(new Error('e1'));
},
},
{
validator() {
return Promise.resolve(new Error('e2'));
},
},
],
v2: [
{
validator() {
return Promise.resolve(new Error('e3'));
},
},
],
}).validate(
{
v: 2,
},
errors => {
assert(errors.length === 2);
assert(errors[0].message === 'e1');
assert(errors[1].message === 'e3');
done();
}
);
});

it('should return null when no errors', done => {
new Schema({
v: [
Expand Down Expand Up @@ -256,7 +324,7 @@ describe('promise validator with promise callaback', () => {
v: [
{
validator() {
return Promise.resolve();;
return Promise.resolve();
},
},
],
Expand All @@ -270,7 +338,7 @@ describe('promise validator with promise callaback', () => {
v3: [
{
validator() {
return Promise.resolve();;
return Promise.resolve();
},
},
],
Expand All @@ -284,7 +352,7 @@ describe('promise validator with promise callaback', () => {
assert.deepEqual(errors, null);
});

it('should resolve with errors and fields when rules fail', async () => {
it('should resolve with errors and fields when rules fail with reject', async () => {
const validator = new Schema({
v: [
{
Expand Down Expand Up @@ -320,6 +388,42 @@ describe('promise validator with promise callaback', () => {
assert.equal(errors[1].message, 'e3');
});

it('should resolve with errors and fields when rules resolve with value', async () => {
const validator = new Schema({
v: [
{
validator() {
return Promise.resolve('e1');
},
},
{
validator() {
return Promise.resolve('e2');
},
},
],
v2: [
{
validator() {
return Promise.resolve('e3');
},
},
],
})

const { errors, fields } = await validator.validate(
{
v: 2,
}
);


assert(errors.length === 2);
assert(Object.keys(fields).length === 2);
assert.equal(errors[0].message, 'e1');
assert.equal(errors[1].message, 'e3');
});

it('should resolve with one error and field when rules fail `options.first`', async () => {
const validator = new Schema(
{
Expand Down

0 comments on commit 1dadad1

Please sign in to comment.