Skip to content

Commit

Permalink
Revert "feat: validate method support Promise."
Browse files Browse the repository at this point in the history
  • Loading branch information
yiminghe committed Oct 17, 2018
1 parent e7deb94 commit f53993a
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 101 deletions.
21 changes: 0 additions & 21 deletions README.md
Expand Up @@ -47,23 +47,6 @@ validator.validate({name: "muji"}, (errors, fields) => {
}
// validation passed
});

// PROMISE USAGE
validator.validate({name: "muji"}, (errors, fields) => {
if(errors) {
// validation failed, errors is an array of all errors
// fields is an object keyed by field name with an array of
// errors per field
return handleErrors(errors, fields);
}
// validation passed
})
.then(() => {
// validation passed
})
.catch(({ errors, fields }) => {
return handleErrors(errors, fields);
})
```

### Validate
Expand All @@ -76,10 +59,6 @@ function(source, [options], callback)
* `options`: An object describing processing options for the validation (optional).
* `callback`: A callback function to invoke when validation completes (required).

The method will return a Promise object like:
* `then()`,validation passed
* `catch({ errors, fields })`,validation failed, errors is an array of all errors, fields is an object keyed by field name with an array of
### Options

* `first`: Boolean, Invoke `callback` when the first validation rule generates an error,
Expand Down
31 changes: 0 additions & 31 deletions __tests__/promise.spec.js
Expand Up @@ -141,34 +141,3 @@ describe('validator', () => {
});
});
});

describe('validate', () => {
it('validate fail', (done) => {
return new Schema({
v: {
type: 'string'
},
})
.validate({
v: 1
})
.catch(({ errors, fields }) => {
expect(errors[0].message).toBe('v is not a string');
expect(fields.v[0].message).toBe(('v is not a string'));
done()
})
});

it('validate success', (done) => {
return new Schema({
v: {
type: 'string'
},
}).validate({
v: '1'
})
.then(() => {
done();
})
});
})
10 changes: 0 additions & 10 deletions __tests__/utils.spec.js

This file was deleted.

5 changes: 1 addition & 4 deletions examples/simple.js
Expand Up @@ -54,9 +54,6 @@ schema.validate({
console.log(errors);
console.log('fields');
console.log(fields);
})
.catch(({ errors, fields }) => {
console.log(errors, fields);
});
});

console.log('end');
15 changes: 10 additions & 5 deletions src/index.js
@@ -1,4 +1,4 @@
import { format, complementError, asyncMap, warning, deepMerge, convertFieldsError } from './util';
import { format, complementError, asyncMap, warning, deepMerge } from './util';
import validators from './validator/';
import { messages as defaultMessages, newMessages } from './messages';

Expand Down Expand Up @@ -39,7 +39,7 @@ Schema.prototype = {
}
}
},
validate(source_, o = {}, oc = () => {}) {
validate(source_, o = {}, oc) {
let source = source_;
let options = o;
let callback = oc;
Expand All @@ -51,10 +51,11 @@ Schema.prototype = {
if (callback) {
callback();
}
return Promise.resolve();
return;
}
function complete(results) {
let i;
let field;
let errors = [];
let fields = {};

Expand All @@ -73,7 +74,11 @@ Schema.prototype = {
errors = null;
fields = null;
} else {
fields = convertFieldsError(errors);
for (i = 0; i < errors.length; i++) {
field = errors[i].field;
fields[field] = fields[field] || [];
fields[field].push(errors[i]);
}
}
callback(errors, fields);
}
Expand Down Expand Up @@ -127,7 +132,7 @@ Schema.prototype = {
});
});
const errorFields = {};
return asyncMap(series, options, (data, doIt) => {
asyncMap(series, options, (data, doIt) => {
const rule = data.rule;
let deep = (rule.type === 'object' || rule.type === 'array') &&
(typeof (rule.fields) === 'object' || typeof (rule.defaultField) === 'object');
Expand Down
44 changes: 14 additions & 30 deletions src/util.js
Expand Up @@ -16,17 +16,6 @@ if (process.env.NODE_ENV !== 'production' &&
};
}

export function convertFieldsError(errors) {
if (!errors || !errors.length) return null;
const fields = {};
errors.forEach(error => {
const field = error.field;
fields[field] = fields[field] || [];
fields[field].push(error);
});
return fields;
}

export function format(...args) {
let i = 1;
const f = args[0];
Expand Down Expand Up @@ -151,25 +140,20 @@ export function asyncMap(objArr, option, func, callback) {
const objArrLength = objArrKeys.length;
let total = 0;
const results = [];
return new Promise((resolve, reject) => {
const next = (errors) => {
results.push.apply(results, errors);
total++;
if (total === objArrLength) {
callback(results);
return results.length ?
reject({ errors: results, fields: convertFieldsError(results) }) :
resolve();
}
};
objArrKeys.forEach((key) => {
const arr = objArr[key];
if (firstFields.indexOf(key) !== -1) {
asyncSerialArray(arr, func, next);
} else {
asyncParallelArray(arr, func, next);
}
});
const next = (errors) => {
results.push.apply(results, errors);
total++;
if (total === objArrLength) {
callback(results);
}
};
objArrKeys.forEach((key) => {
const arr = objArr[key];
if (firstFields.indexOf(key) !== -1) {
asyncSerialArray(arr, func, next);
} else {
asyncParallelArray(arr, func, next);
}
});
}

Expand Down

0 comments on commit f53993a

Please sign in to comment.