Skip to content

Commit

Permalink
Merge e8d5c14 into 4d1ac3c
Browse files Browse the repository at this point in the history
  • Loading branch information
nuintun committed Aug 6, 2019
2 parents 4d1ac3c + e8d5c14 commit b30e389
Show file tree
Hide file tree
Showing 3 changed files with 228 additions and 7 deletions.
10 changes: 5 additions & 5 deletions README.md
Expand Up @@ -33,7 +33,7 @@ The following is modified from earlier version of [async-validate](https://githu
Basic usage involves defining a descriptor, assigning it to a schema and passing the object to be validated and a callback function to the `validate` method of the schema:

```javascript
var schema = require('async-validator');
var schema = require('async-validator').default;
var descriptor = {
name: {
type: "string",
Expand Down Expand Up @@ -116,7 +116,7 @@ function(rule, value, callback, source, options)
The options passed to `validate` or `asyncValidate` are passed on to the validation functions so that you may reference transient data (such as model references) in validation functions. However, some option names are reserved; if you use these properties of the options object they are overwritten. The reserved properties are `messages`, `exception` and `error`.

```javascript
var schema = require('async-validator');
var schema = require('async-validator').default;
var descriptor = {
name(rule, value, callback, source, options) {
var errors = [];
Expand Down Expand Up @@ -292,7 +292,7 @@ Note that `defaultField` is expanded to `fields`, see [deep rules](#deep-rules).
Sometimes it is necessary to transform a value before validation, possibly to coerce the value or to sanitize it in some way. To do this add a `transform` function to the validation rule. The property is transformed prior to validation and re-assigned to the source object to mutate the value of the property in place.

```javascript
var schema = require('async-validator');
var schema = require('async-validator').default;
var sanitize = require('validator').sanitize;
var descriptor = {
name: {
Expand Down Expand Up @@ -338,7 +338,7 @@ Potentially you may require the same schema validation rules for different langu
In this scenario you could just provide your own messages for the language and assign it to the schema:

```javascript
var schema = require('async-validator');
var schema = require('async-validator').default;
var cn = {
required: '%s 必填',
};
Expand Down Expand Up @@ -416,7 +416,7 @@ const fields = {
### How to avoid warning

```js
var Schema = require('async-validator');
var Schema = require('async-validator').default;
Schema.warning = function(){};
```

Expand Down
211 changes: 211 additions & 0 deletions __tests__/validator.spec.js
Expand Up @@ -207,4 +207,215 @@ describe('validator', () => {
);
});
});

describe('promise api', () => {
it('works', 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'));
},
},
],
v3: [
{
validator() {
return false;
},
},
{
validator() {
return new Error('e5');
},
},
{
validator() {
return false;
},
message: 'e6',
},
{
validator() {
return true;
},
},
],
})
.validate({
v: 2,
})
.catch(({ errors }) => {
expect(errors.length).toBe(6);
expect(errors[0].message).toBe('e1');
expect(errors[1].message).toBe('e2');
expect(errors[2].message).toBe('e3');
expect(errors[3].message).toBe('v3 fails');
expect(errors[4].message).toBe('e5');
expect(errors[5].message).toBe('e6');
done();
});
});

it('first works', 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,
v2: 1,
},
{
first: true,
},
)
.catch(({ errors }) => {
expect(errors.length).toBe(1);
expect(errors[0].message).toBe('e1');
done();
});
});

describe('firstFields', () => {
it('works for true', 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'));
},
},
],
v3: [
{
validator(rule, value, callback) {
callback(new Error('e4'));
},
},
{
validator(rule, value, callback) {
callback(new Error('e5'));
},
},
],
})
.validate(
{
v: 1,
v2: 1,
v3: 1,
},
{
firstFields: true,
},
)
.catch(({ errors }) => {
expect(errors.length).toBe(3);
expect(errors[0].message).toBe('e1');
expect(errors[1].message).toBe('e3');
expect(errors[2].message).toBe('e4');
done();
});
});

it('works for array', 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'));
},
},
],
v3: [
{
validator(rule, value, callback) {
callback(new Error('e4'));
},
},
{
validator(rule, value, callback) {
callback(new Error('e5'));
},
},
],
})
.validate(
{
v: 1,
v2: 1,
v3: 1,
},
{
firstFields: ['v'],
},
)
.catch(({ errors }) => {
expect(errors.length).toBe(4);
expect(errors[0].message).toBe('e1');
expect(errors[1].message).toBe('e3');
expect(errors[2].message).toBe('e4');
expect(errors[3].message).toBe('e5');
done();
});
});
});
});
});
14 changes: 12 additions & 2 deletions src/util.js
Expand Up @@ -145,8 +145,18 @@ function flattenObjArr(objArr) {

export function asyncMap(objArr, option, func, callback) {
if (option.first) {
const flattenArr = flattenObjArr(objArr);
return asyncSerialArray(flattenArr, func, callback);
const pending = new Promise((resolve, reject) => {
const next = errors => {
callback(errors);
return errors.length
? reject({ errors, fields: convertFieldsError(errors) })
: resolve();
};
const flattenArr = flattenObjArr(objArr);
asyncSerialArray(flattenArr, func, next);
});
pending.catch(e => e);
return pending;
}
let firstFields = option.firstFields || [];
if (firstFields === true) {
Expand Down

0 comments on commit b30e389

Please sign in to comment.