Skip to content

Commit

Permalink
fix #22
Browse files Browse the repository at this point in the history
  • Loading branch information
yiminghe committed Apr 23, 2019
1 parent 49ceefe commit 08d67b8
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 3 deletions.
10 changes: 10 additions & 0 deletions __tests__/__snapshots__/deep.spec.js.snap
@@ -0,0 +1,10 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`deep defaultField will merge top validation 1`] = `
Array [
Object {
"field": "test",
"message": "至少两项",
},
]
`;
30 changes: 30 additions & 0 deletions __tests__/deep.spec.js
Expand Up @@ -61,6 +61,36 @@ describe('deep', () => {
});
});

it('will merge top validation',()=>{
const obj = {
value: '',
test: [{
name: 'aa',
}],
};

const descriptor = {
test: {
type: 'array',
min: 2,
required: true,
message: '至少两项',
defaultField: [{
type: 'object',
required: true,
message: 'test 必须有',
fields: {
name: { type: 'string', required: true, message: 'name 必须有' },
},
}],
},
};

new Schema(descriptor).validate(obj,(errors)=>{
expect(errors).toMatchSnapshot();
});
});

it('array & required works', (done) => {
const descriptor = {
testArray: {
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "async-validator",
"version": "1.11.1",
"version": "1.11.2",
"description": "validate form asynchronous",
"keywords": [
"validator",
Expand Down
14 changes: 12 additions & 2 deletions src/index.js
Expand Up @@ -39,7 +39,8 @@ Schema.prototype = {
}
}
},
validate(source_, o = {}, oc = () => {}) {
validate(source_, o = {}, oc = () => {
}) {
let source = source_;
let options = o;
let callback = oc;
Expand All @@ -53,6 +54,7 @@ Schema.prototype = {
}
return Promise.resolve();
}

function complete(results) {
let i;
let errors = [];
Expand Down Expand Up @@ -133,6 +135,7 @@ Schema.prototype = {
(typeof (rule.fields) === 'object' || typeof (rule.defaultField) === 'object');
deep = deep && (rule.required || (!rule.required && data.value));
rule.field = data.field;

function addFullfield(key, schema) {
return {
...schema,
Expand Down Expand Up @@ -201,7 +204,14 @@ Schema.prototype = {
data.rule.options.error = options.error;
}
schema.validate(data.value, data.rule.options || options, (errs) => {
doIt(errs && errs.length ? errors.concat(errs) : errs);
const finalErrors = [];
if (errors && errors.length) {
finalErrors.push(...errors);
}
if (errs && errs.length) {
finalErrors.push(...errs);
}
doIt(finalErrors.length ? finalErrors : null);
});
}
}
Expand Down

0 comments on commit 08d67b8

Please sign in to comment.