Skip to content

Commit

Permalink
Merge pull request #6 from alibaba-fusion/develop
Browse files Browse the repository at this point in the history
Release `validatePromise` fix
  • Loading branch information
jdkahn authored Jul 30, 2019
2 parents 191f5d7 + 6392d53 commit ec8d637
Show file tree
Hide file tree
Showing 11 changed files with 2,003 additions and 996 deletions.
13 changes: 5 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class Schema {
* @returns {null | Promise}
* - { null } - if using callbacks
* - { Promise }
* - { null } - if no rules or no errors
* - { errors: null } - if no rules or no errors
* - { errors: Array, fields: Object } - errors from validation and fields that have errors
*/
validate(source, callback) {
Expand Down Expand Up @@ -138,18 +138,18 @@ class Schema {
*
* @param {Object} source - map of field names and values to use in validation
* @returns {Promise}
* - {null} if no rules or no errors
* - { errors: null } if no rules or no errors
* - { errors: Array, fields: Object } - errors from validation and fields that have errors
*/
async validatePromise(source) {
if (!this._rules || Object.keys(this._rules).length === 0) {
return Promise.resolve(null);
return Promise.resolve({ errors: null });
}

const series = serializeRules(source, this._rules);

if (Object.keys(series).length === 0) {
return Promise.resolve(null);
return Promise.resolve({ errors: null });
}

const results = await asyncMapPromise(
Expand All @@ -165,6 +165,7 @@ class Schema {
errors = await rule.validator(
rule,
data.value,
null,
this._options
);
} catch (error) {
Expand All @@ -188,10 +189,6 @@ class Schema {
}
);

if (!results) {
return { errors: results };
}

return processErrorResults(results);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ export function serializeRules(source, rules) {
* @param {Array} results errors from running validation
* @returns {Object} { errors: Array, fields: Object }
*/
export function processErrorResults(results) {
export function processErrorResults(results = []) {
let errors = [];
let fields = {};

Expand Down
21 changes: 17 additions & 4 deletions src/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import rules from './rules/';

/**
* {required, format} => format; {required} => required
* If a promise is wanted from the validator, either return a promise from the callback,
* or do not pass a callback
*
* @param {function} validator [description]
* @param {string} ruleType [description]
* @return {function} [description]
Expand All @@ -17,16 +20,26 @@ export function validateFunc(validator, ruleType) {
rules.required(rule, value, errors, options);
if (errors.length > 0) {
if ('required' in rule) {
cb(errors);
if (cb) {
return cb(errors);
} else {
return Promise.reject(errors);
}
} else if (cb) {
return cb([]); //忽略空数据的判断
} else {
cb([]); //忽略空数据的判断
return Promise.resolve(null);
}
return;
}
}

validator(rule, value, errors, options);
cb(errors);
if (cb) {
return cb(errors);
}
if (Promise) {
return Promise.resolve(errors);
}
};
}

Expand Down
Loading

0 comments on commit ec8d637

Please sign in to comment.