Skip to content

Commit c9df371

Browse files
committed
rebuild
1 parent 47c1ff7 commit c9df371

File tree

5 files changed

+51
-14
lines changed

5 files changed

+51
-14
lines changed

lib/array.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ inherits(ArraySchema, MixedSchema, {
9393
return value;
9494
}
9595

96-
var result = value.map(function (item, key) {
96+
var validations = value.map(function (item, key) {
9797
var path = (options.path || '') + '[' + key + ']';
9898

9999
// object._validate note for isStrict explanation
@@ -104,9 +104,9 @@ inherits(ArraySchema, MixedSchema, {
104104
return true;
105105
});
106106

107-
result = endEarly ? Promise.all(result).catch(scopeError(value)) : collectErrors(result, value, options.path, errors);
107+
validations = endEarly ? Promise.all(validations).catch(scopeError(value)) : collectErrors({ validations: validations, value: value, errors: errors, path: options.path });
108108

109-
return result.then(function () {
109+
return validations.then(function () {
110110
return value;
111111
});
112112
});

lib/mixed.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@ var notEmpty = function notEmpty(value) {
1717
};
1818

1919
function runValidations(validations, endEarly, value, path) {
20-
return endEarly ? Promise.all(validations) : _.collectErrors(validations, value, path);
20+
return endEarly ? Promise.all(validations) : _.collectErrors({ validations: validations, value: value, path: path });
2121
}
2222

2323
function extractTestParams(name, message, test, useCallback) {
2424
var opts = name;
2525

26-
if (typeof message === 'function') test = message, message = locale.default;
26+
if (typeof message === 'function') test = message, message = locale.default, name = null;
27+
28+
if (typeof name === 'function') test = name, message = locale.default, name = null;
2729

2830
if (typeof name === 'string' || name === null) opts = { name: name, test: test, message: message, useCallback: useCallback, exclusive: false };
2931

lib/object.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ var locale = require('./locale.js').object;
99
var split = require('property-expr').split;
1010
var Ref = require('./util/reference');
1111
var c = require('case');
12+
var sortByFields = require('./util/sortByFields');
1213

1314
var _require = require('./util/_');
1415

@@ -141,7 +142,7 @@ inherits(ObjectSchema, MixedSchema, {
141142
return value;
142143
}
143144

144-
var result = _this4._nodes.map(function (key) {
145+
var validations = _this4._nodes.map(function (key) {
145146
var path = (opts.path ? opts.path + '.' : '') + key,
146147
field = _this4.fields[key],
147148
innerOptions = _extends({}, opts, { key: key, path: path, parent: value });
@@ -158,9 +159,12 @@ inherits(ObjectSchema, MixedSchema, {
158159
return true;
159160
});
160161

161-
result = endEarly ? Promise.all(result).catch(scopeError(value)) : collectErrors(result, value, opts.path, errors);
162+
validations = endEarly ? Promise.all(validations).catch(scopeError(value)) : collectErrors({ validations: validations, value: value, errors: errors,
163+
path: opts.path,
164+
sort: sortByFields(_this4)
165+
});
162166

163-
return result.then(function () {
167+
return validations.then(function () {
164168
return value;
165169
});
166170
});

lib/util/_.js

+17-6
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,27 @@ function settled(promises) {
3333
return Promise.all(promises.map(settle));
3434
}
3535

36-
function collectErrors(promises, value, path) {
37-
var errors = arguments.length <= 3 || arguments[3] === undefined ? [] : arguments[3];
36+
function collectErrors(_ref) {
37+
var validations = _ref.validations;
38+
var value = _ref.value;
39+
var path = _ref.path;
40+
var _ref$errors = _ref.errors;
41+
var errors = _ref$errors === undefined ? [] : _ref$errors;
42+
var sort = _ref.sort;
3843

3944
// unwrap aggregate errors
4045
errors = errors.inner && errors.inner.length ? errors.inner : [].concat(errors);
4146

42-
return settled(promises).then(function (results) {
43-
errors = results.reduce(function (arr, r) {
44-
return !r.fulfilled ? arr.concat(r.value) : arr;
45-
}, errors);
47+
return settled(validations).then(function (results) {
48+
var nestedErrors = results.filter(function (r) {
49+
return !r.fulfilled;
50+
}).reduce(function (arr, r) {
51+
return arr.concat(r.value);
52+
}, []);
53+
54+
if (sort) nestedErrors.sort(sort);
55+
//show parent errors after the nested ones: name.first, name
56+
errors = nestedErrors.concat(errors);
4657

4758
if (errors.length) throw new ValidationError(errors, value, path);
4859
});

lib/util/sortByFields.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"use strict";
2+
3+
function findIndex(arr, err) {
4+
var idx = Infinity;
5+
arr.some(function (key, ii) {
6+
if (err.path.indexOf(key) !== -1) {
7+
idx = ii;
8+
return true;
9+
}
10+
});
11+
12+
return idx;
13+
}
14+
15+
module.exports = function sortByFields(schema) {
16+
var keys = Object.keys(schema.fields);
17+
return function (a, b) {
18+
return findIndex(keys, a) - findIndex(keys, b);
19+
};
20+
};

0 commit comments

Comments
 (0)