@bedrockio/yada 1.0.40 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +66 -9
- package/dist/cjs/Schema.js +27 -52
- package/dist/cjs/TypeSchema.js +2 -2
- package/dist/cjs/array.js +8 -6
- package/dist/cjs/errors.js +80 -61
- package/dist/cjs/index.js +3 -3
- package/dist/cjs/localization.js +3 -0
- package/dist/cjs/messages.js +25 -20
- package/dist/cjs/object.js +5 -3
- package/dist/cjs/tuple.js +6 -7
- package/package.json +1 -1
- package/src/Schema.js +30 -55
- package/src/TypeSchema.js +1 -1
- package/src/array.js +6 -11
- package/src/errors.js +80 -66
- package/src/index.js +3 -3
- package/src/localization.js +3 -0
- package/src/messages.js +28 -18
- package/src/object.js +3 -11
- package/src/tuple.js +4 -9
- package/types/Schema.d.ts +2 -6
- package/types/Schema.d.ts.map +1 -1
- package/types/array.d.ts +3 -1
- package/types/array.d.ts.map +1 -1
- package/types/errors.d.ts +43 -21
- package/types/errors.d.ts.map +1 -1
- package/types/index.d.ts +2 -2
- package/types/index.d.ts.map +1 -1
- package/types/localization.d.ts.map +1 -1
- package/types/messages.d.ts.map +1 -1
- package/types/object.d.ts.map +1 -1
- package/types/tuple.d.ts.map +1 -1
package/README.md
CHANGED
|
@@ -19,12 +19,13 @@ Concepts
|
|
|
19
19
|
- [Object](#object)
|
|
20
20
|
- [Date](#date)
|
|
21
21
|
- [Common Methods](#common-methods)
|
|
22
|
-
- [
|
|
23
|
-
- [
|
|
24
|
-
- [
|
|
25
|
-
- [
|
|
26
|
-
- [
|
|
27
|
-
- [
|
|
22
|
+
- [allow](#allow)
|
|
23
|
+
- [reject](#reject)
|
|
24
|
+
- [append](#append)
|
|
25
|
+
- [custom](#custom)
|
|
26
|
+
- [default](#default)
|
|
27
|
+
- [strip](#strip)
|
|
28
|
+
- [message](#message)
|
|
28
29
|
- [Validation Options](#validation-options)
|
|
29
30
|
- [Error Messages](#error-messages)
|
|
30
31
|
- [Localization](#localization)
|
|
@@ -552,6 +553,65 @@ const schema = yd.object({
|
|
|
552
553
|
Arguments are identical to those passed to [custom](#custom). The field will be
|
|
553
554
|
stripped out if the function returns a truthy value.
|
|
554
555
|
|
|
556
|
+
### Message
|
|
557
|
+
|
|
558
|
+
The `message` method allows adding a custom message to schema or field. Note
|
|
559
|
+
that when using with [`getFullMessage`](#error-messages) the custom message will
|
|
560
|
+
not include the nested field name by default:
|
|
561
|
+
|
|
562
|
+
```js
|
|
563
|
+
const schema = yd.object({
|
|
564
|
+
name: yd.string().required().message('Please provide your full name.'),
|
|
565
|
+
});
|
|
566
|
+
try {
|
|
567
|
+
await schema.validate({});
|
|
568
|
+
} catch (error) {
|
|
569
|
+
console.log(error.getFullMessage());
|
|
570
|
+
// -> Please provide your full name.
|
|
571
|
+
}
|
|
572
|
+
```
|
|
573
|
+
|
|
574
|
+
To include the field name, the `{field}` token may be used in the message:
|
|
575
|
+
|
|
576
|
+
```js
|
|
577
|
+
const schema = yd.object({
|
|
578
|
+
name: yd
|
|
579
|
+
.string()
|
|
580
|
+
.match(/^[A-Z]/)
|
|
581
|
+
.message('{field} must start with an uppercase letter.'),
|
|
582
|
+
});
|
|
583
|
+
try {
|
|
584
|
+
await schema.validate({
|
|
585
|
+
name: 'frank',
|
|
586
|
+
});
|
|
587
|
+
} catch (error) {
|
|
588
|
+
console.log(error.getFullMessage());
|
|
589
|
+
// -> "name" must start with an uppercase letter.
|
|
590
|
+
}
|
|
591
|
+
```
|
|
592
|
+
|
|
593
|
+
The `{field}` token may also be used when throwing custom errors:
|
|
594
|
+
|
|
595
|
+
```js
|
|
596
|
+
const schema = yd.object({
|
|
597
|
+
profile: yd.object({
|
|
598
|
+
name: yd.custom((value) => {
|
|
599
|
+
if (value !== 'Frank') {
|
|
600
|
+
throw new Error('{field} must be "Frank".');
|
|
601
|
+
}
|
|
602
|
+
}),
|
|
603
|
+
}),
|
|
604
|
+
});
|
|
605
|
+
try {
|
|
606
|
+
await schema.validate({
|
|
607
|
+
name: 'Bob',
|
|
608
|
+
});
|
|
609
|
+
} catch (error) {
|
|
610
|
+
console.log(error.getFullMessage());
|
|
611
|
+
// -> "name" must be "Frank".
|
|
612
|
+
}
|
|
613
|
+
```
|
|
614
|
+
|
|
555
615
|
## Validation Options
|
|
556
616
|
|
|
557
617
|
Validation options in Yada can be passed at runtime on validation or baked into
|
|
@@ -634,7 +694,6 @@ which returns that map:
|
|
|
634
694
|
```js
|
|
635
695
|
yd.useLocalizer({
|
|
636
696
|
'Must be at least {length} character{s}.': '{length}文字以上入力して下さい。',
|
|
637
|
-
'Object failed validation.': '不正な入力がありました。',
|
|
638
697
|
});
|
|
639
698
|
```
|
|
640
699
|
|
|
@@ -657,13 +716,11 @@ allow quick discovery of strings that have not yet been localized:
|
|
|
657
716
|
```js
|
|
658
717
|
yd.useLocalizer({
|
|
659
718
|
'Must be at least {length} character{s}.': '{length}文字以上入力して下さい。',
|
|
660
|
-
'Object failed validation.': '不正な入力がありました。',
|
|
661
719
|
});
|
|
662
720
|
// Error validation occuring here
|
|
663
721
|
yd.getLocalizedMessages();
|
|
664
722
|
// {
|
|
665
723
|
// 'Must be at least {length} character{s}.': '{length}文字以上入力して下さい。',
|
|
666
|
-
// 'Object failed validation.': '不正な入力がありました。',
|
|
667
724
|
// 'Value must be a string.': 'Value must be a string.',
|
|
668
725
|
// ...etc
|
|
669
726
|
// }
|
package/dist/cjs/Schema.js
CHANGED
|
@@ -9,11 +9,6 @@ var _errors = require("./errors");
|
|
|
9
9
|
var _utils = require("./utils");
|
|
10
10
|
const INITIAL_TYPES = ['default', 'required', 'type', 'transform'];
|
|
11
11
|
const REQUIRED_TYPES = ['default', 'required'];
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* @typedef {[fn: Function] | [type: string, fn: Function]} CustomSignature
|
|
15
|
-
*/
|
|
16
|
-
|
|
17
12
|
class Schema {
|
|
18
13
|
constructor(meta = {}) {
|
|
19
14
|
this.assertions = [];
|
|
@@ -52,24 +47,14 @@ class Schema {
|
|
|
52
47
|
|
|
53
48
|
/**
|
|
54
49
|
* Validate by a custom function. [Link](https://github.com/bedrockio/yada#custom)
|
|
55
|
-
* @param {
|
|
50
|
+
* @param {Function} fn
|
|
56
51
|
* @returns {this}
|
|
57
52
|
*/
|
|
58
|
-
custom(
|
|
59
|
-
|
|
60
|
-
if (typeof args[0] === 'function') {
|
|
61
|
-
type = 'custom';
|
|
62
|
-
fn = args[0];
|
|
63
|
-
} else {
|
|
64
|
-
type = args[0];
|
|
65
|
-
fn = args[1];
|
|
66
|
-
}
|
|
67
|
-
if (!type) {
|
|
68
|
-
throw new Error('Assertion type required.');
|
|
69
|
-
} else if (!fn) {
|
|
53
|
+
custom(fn) {
|
|
54
|
+
if (!fn) {
|
|
70
55
|
throw new Error('Assertion function required.');
|
|
71
56
|
}
|
|
72
|
-
return this.clone().assert(
|
|
57
|
+
return this.clone().assert('custom', async (val, options) => {
|
|
73
58
|
return await fn(val, options);
|
|
74
59
|
});
|
|
75
60
|
}
|
|
@@ -157,8 +142,15 @@ class Schema {
|
|
|
157
142
|
value = result;
|
|
158
143
|
}
|
|
159
144
|
} catch (error) {
|
|
160
|
-
|
|
161
|
-
|
|
145
|
+
const {
|
|
146
|
+
type
|
|
147
|
+
} = assertion;
|
|
148
|
+
if (type === 'type') {
|
|
149
|
+
details.push(new _errors.TypeError(error, this.meta.type));
|
|
150
|
+
} else if (type === 'format') {
|
|
151
|
+
details.push(new _errors.FormatError(error, this.meta.format));
|
|
152
|
+
} else if (error instanceof _errors.LocalizedError) {
|
|
153
|
+
details.push(new _errors.AssertionError(error, type));
|
|
162
154
|
} else {
|
|
163
155
|
details.push(error);
|
|
164
156
|
}
|
|
@@ -168,10 +160,7 @@ class Schema {
|
|
|
168
160
|
}
|
|
169
161
|
}
|
|
170
162
|
if (details.length) {
|
|
171
|
-
|
|
172
|
-
message = 'Input failed validation.'
|
|
173
|
-
} = this.meta;
|
|
174
|
-
throw new _errors.ValidationError(message, details);
|
|
163
|
+
throw new _errors.ValidationError(this.meta.message, details);
|
|
175
164
|
}
|
|
176
165
|
return value;
|
|
177
166
|
}
|
|
@@ -266,12 +255,11 @@ class Schema {
|
|
|
266
255
|
}
|
|
267
256
|
return el;
|
|
268
257
|
});
|
|
269
|
-
const
|
|
258
|
+
const message = `${allow ? 'Must' : 'Must not'} be one of [{types}].`;
|
|
270
259
|
return this.clone({
|
|
271
260
|
enum: set
|
|
272
261
|
}).assert('enum', async (val, options) => {
|
|
273
262
|
if (val !== undefined) {
|
|
274
|
-
let error;
|
|
275
263
|
for (let el of set) {
|
|
276
264
|
if (isSchema(el)) {
|
|
277
265
|
try {
|
|
@@ -282,28 +270,23 @@ class Schema {
|
|
|
282
270
|
return await el.validate(val, options);
|
|
283
271
|
} catch (err) {
|
|
284
272
|
const [first] = err.details;
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
//
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
//
|
|
291
|
-
//
|
|
292
|
-
|
|
273
|
+
if (first instanceof _errors.TypeError) {
|
|
274
|
+
// If the error is a simple type error then continue
|
|
275
|
+
// to show more meaningful messages for simple enums.
|
|
276
|
+
continue;
|
|
277
|
+
} else {
|
|
278
|
+
// Otherwise throw the error to surface messages on
|
|
279
|
+
// more complex schemas.
|
|
280
|
+
throw err;
|
|
293
281
|
}
|
|
294
|
-
continue;
|
|
295
282
|
}
|
|
296
283
|
} else if (el === val === allow) {
|
|
297
284
|
return;
|
|
298
285
|
}
|
|
299
286
|
}
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
}
|
|
303
|
-
throw new _errors.LocalizedError(options.message || msg, {
|
|
304
|
-
types: types.join(', ')
|
|
305
|
-
});
|
|
306
|
-
}
|
|
287
|
+
throw new _errors.LocalizedError(message, {
|
|
288
|
+
types: types.join(', ')
|
|
289
|
+
});
|
|
307
290
|
}
|
|
308
291
|
});
|
|
309
292
|
}
|
|
@@ -344,17 +327,9 @@ class Schema {
|
|
|
344
327
|
}
|
|
345
328
|
async runAssertion(assertion, value, options = {}) {
|
|
346
329
|
const {
|
|
347
|
-
type,
|
|
348
330
|
fn
|
|
349
331
|
} = assertion;
|
|
350
|
-
|
|
351
|
-
return await fn(value, options);
|
|
352
|
-
} catch (error) {
|
|
353
|
-
if ((0, _errors.isSchemaError)(error)) {
|
|
354
|
-
throw error;
|
|
355
|
-
}
|
|
356
|
-
throw new _errors.AssertionError(error.message, error.type || type, error);
|
|
357
|
-
}
|
|
332
|
+
return await fn(value, options);
|
|
358
333
|
}
|
|
359
334
|
enumToOpenApi() {
|
|
360
335
|
const {
|
package/dist/cjs/TypeSchema.js
CHANGED
package/dist/cjs/array.js
CHANGED
|
@@ -5,13 +5,13 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.default = _default;
|
|
7
7
|
var _Schema = _interopRequireDefault(require("./Schema"));
|
|
8
|
+
var _TypeSchema = _interopRequireDefault(require("./TypeSchema"));
|
|
8
9
|
var _errors = require("./errors");
|
|
9
10
|
var _utils = require("./utils");
|
|
10
11
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
11
|
-
class ArraySchema extends
|
|
12
|
+
class ArraySchema extends _TypeSchema.default {
|
|
12
13
|
constructor(schemas) {
|
|
13
|
-
super({
|
|
14
|
-
message: 'Array failed validation.',
|
|
14
|
+
super(Array, {
|
|
15
15
|
schemas
|
|
16
16
|
});
|
|
17
17
|
this.setup();
|
|
@@ -22,8 +22,7 @@ class ArraySchema extends _Schema.default {
|
|
|
22
22
|
*/
|
|
23
23
|
setup() {
|
|
24
24
|
const {
|
|
25
|
-
schemas
|
|
26
|
-
message
|
|
25
|
+
schemas
|
|
27
26
|
} = this.meta;
|
|
28
27
|
const schema = schemas.length > 1 ? new _Schema.default().allow(schemas) : schemas[0];
|
|
29
28
|
this.assert('type', (val, options) => {
|
|
@@ -36,6 +35,9 @@ class ArraySchema extends _Schema.default {
|
|
|
36
35
|
return val;
|
|
37
36
|
});
|
|
38
37
|
if (schema) {
|
|
38
|
+
const {
|
|
39
|
+
message
|
|
40
|
+
} = schema.meta;
|
|
39
41
|
this.assert('elements', async (arr, options) => {
|
|
40
42
|
const errors = [];
|
|
41
43
|
const result = [];
|
|
@@ -47,7 +49,7 @@ class ArraySchema extends _Schema.default {
|
|
|
47
49
|
options = (0, _utils.omit)(options, 'message');
|
|
48
50
|
result.push(await schema.validate(el, options));
|
|
49
51
|
} catch (error) {
|
|
50
|
-
errors.push(new _errors.ElementError(
|
|
52
|
+
errors.push(new _errors.ElementError(message, i, error.details));
|
|
51
53
|
}
|
|
52
54
|
}
|
|
53
55
|
if (errors.length) {
|
package/dist/cjs/errors.js
CHANGED
|
@@ -3,58 +3,38 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.ValidationError = exports.LocalizedError = exports.FieldError = exports.ElementError = exports.AssertionError = exports.ArrayError = void 0;
|
|
6
|
+
exports.ValidationError = exports.TypeError = exports.LocalizedError = exports.FormatError = exports.FieldError = exports.ElementError = exports.AssertionError = exports.ArrayError = void 0;
|
|
7
7
|
exports.isSchemaError = isSchemaError;
|
|
8
8
|
var _messages = require("./messages");
|
|
9
9
|
var _localization = require("./localization");
|
|
10
10
|
class LocalizedError extends Error {
|
|
11
|
-
constructor(message, values) {
|
|
11
|
+
constructor(message, values = {}) {
|
|
12
12
|
super((0, _localization.localize)(message, values));
|
|
13
|
-
this.values = values;
|
|
14
|
-
}
|
|
15
|
-
get type() {
|
|
16
|
-
return this.values?.type;
|
|
17
13
|
}
|
|
18
14
|
}
|
|
19
15
|
exports.LocalizedError = LocalizedError;
|
|
20
16
|
class ValidationError extends Error {
|
|
21
|
-
constructor(
|
|
22
|
-
super((
|
|
17
|
+
constructor(arg, details = []) {
|
|
18
|
+
super(getLocalizedMessage(arg));
|
|
19
|
+
this.type = 'validation';
|
|
23
20
|
this.details = details;
|
|
24
|
-
this.type = type;
|
|
25
21
|
}
|
|
26
22
|
toJSON() {
|
|
27
|
-
if (this.canRollup()) {
|
|
28
|
-
const [first] = this.details;
|
|
29
|
-
return {
|
|
30
|
-
...first.toJSON(),
|
|
31
|
-
type: this.type
|
|
32
|
-
};
|
|
33
|
-
} else {
|
|
34
|
-
return {
|
|
35
|
-
type: this.type,
|
|
36
|
-
message: this.message,
|
|
37
|
-
details: this.details.map(error => {
|
|
38
|
-
return error.toJSON();
|
|
39
|
-
})
|
|
40
|
-
};
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
canRollup() {
|
|
44
23
|
const {
|
|
24
|
+
message,
|
|
45
25
|
details
|
|
46
26
|
} = this;
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
27
|
+
return {
|
|
28
|
+
type: this.type,
|
|
29
|
+
...(message && {
|
|
30
|
+
message
|
|
31
|
+
}),
|
|
32
|
+
...(details.length && {
|
|
33
|
+
details: details.map(error => {
|
|
34
|
+
return serializeError(error);
|
|
35
|
+
})
|
|
36
|
+
})
|
|
37
|
+
};
|
|
58
38
|
}
|
|
59
39
|
getFullMessage(options) {
|
|
60
40
|
return (0, _messages.getFullMessage)(this, {
|
|
@@ -64,12 +44,46 @@ class ValidationError extends Error {
|
|
|
64
44
|
}
|
|
65
45
|
}
|
|
66
46
|
exports.ValidationError = ValidationError;
|
|
47
|
+
class AssertionError extends ValidationError {
|
|
48
|
+
constructor(message, type = 'assertion') {
|
|
49
|
+
super(message);
|
|
50
|
+
this.type = type;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
exports.AssertionError = AssertionError;
|
|
54
|
+
class TypeError extends ValidationError {
|
|
55
|
+
constructor(message, kind) {
|
|
56
|
+
super(message);
|
|
57
|
+
this.type = 'type';
|
|
58
|
+
this.kind = kind;
|
|
59
|
+
}
|
|
60
|
+
toJSON() {
|
|
61
|
+
return {
|
|
62
|
+
...super.toJSON(),
|
|
63
|
+
kind: this.kind
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
exports.TypeError = TypeError;
|
|
68
|
+
class FormatError extends ValidationError {
|
|
69
|
+
constructor(message, format) {
|
|
70
|
+
super(message);
|
|
71
|
+
this.type = 'format';
|
|
72
|
+
this.format = format;
|
|
73
|
+
}
|
|
74
|
+
toJSON() {
|
|
75
|
+
return {
|
|
76
|
+
...super.toJSON(),
|
|
77
|
+
format: this.format
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
exports.FormatError = FormatError;
|
|
67
82
|
class FieldError extends ValidationError {
|
|
68
|
-
constructor(message, field,
|
|
69
|
-
super(message, details
|
|
83
|
+
constructor(message, field, details) {
|
|
84
|
+
super(message, details);
|
|
85
|
+
this.type = 'field';
|
|
70
86
|
this.field = field;
|
|
71
|
-
this.original = original;
|
|
72
|
-
this.details = details;
|
|
73
87
|
}
|
|
74
88
|
toJSON() {
|
|
75
89
|
return {
|
|
@@ -80,11 +94,10 @@ class FieldError extends ValidationError {
|
|
|
80
94
|
}
|
|
81
95
|
exports.FieldError = FieldError;
|
|
82
96
|
class ElementError extends ValidationError {
|
|
83
|
-
constructor(message, index,
|
|
84
|
-
super(message, details
|
|
97
|
+
constructor(message, index, details) {
|
|
98
|
+
super(message, details);
|
|
99
|
+
this.type = 'element';
|
|
85
100
|
this.index = index;
|
|
86
|
-
this.original = original;
|
|
87
|
-
this.details = details;
|
|
88
101
|
}
|
|
89
102
|
toJSON() {
|
|
90
103
|
return {
|
|
@@ -94,27 +107,33 @@ class ElementError extends ValidationError {
|
|
|
94
107
|
}
|
|
95
108
|
}
|
|
96
109
|
exports.ElementError = ElementError;
|
|
97
|
-
class
|
|
98
|
-
constructor(message, type, original) {
|
|
99
|
-
super(message);
|
|
100
|
-
this.type = type;
|
|
101
|
-
this.original = original;
|
|
102
|
-
}
|
|
103
|
-
toJSON() {
|
|
104
|
-
return {
|
|
105
|
-
type: this.type,
|
|
106
|
-
message: this.message
|
|
107
|
-
};
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
exports.AssertionError = AssertionError;
|
|
111
|
-
class ArrayError extends Error {
|
|
110
|
+
class ArrayError extends ValidationError {
|
|
112
111
|
constructor(message, details) {
|
|
113
112
|
super(message);
|
|
113
|
+
this.type = 'array';
|
|
114
114
|
this.details = details;
|
|
115
115
|
}
|
|
116
116
|
}
|
|
117
117
|
exports.ArrayError = ArrayError;
|
|
118
118
|
function isSchemaError(arg) {
|
|
119
|
-
return arg instanceof ValidationError
|
|
119
|
+
return arg instanceof ValidationError;
|
|
120
|
+
}
|
|
121
|
+
function getLocalizedMessage(arg) {
|
|
122
|
+
if (arg instanceof LocalizedError) {
|
|
123
|
+
return arg.message;
|
|
124
|
+
} else if (arg instanceof Error) {
|
|
125
|
+
return (0, _localization.localize)(arg.message);
|
|
126
|
+
} else {
|
|
127
|
+
return (0, _localization.localize)(arg);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
function serializeError(error) {
|
|
131
|
+
if (error.toJSON) {
|
|
132
|
+
return error.toJSON();
|
|
133
|
+
} else {
|
|
134
|
+
return {
|
|
135
|
+
...error,
|
|
136
|
+
message: error.message
|
|
137
|
+
};
|
|
138
|
+
}
|
|
120
139
|
}
|
package/dist/cjs/index.js
CHANGED
|
@@ -115,10 +115,10 @@ function reject(...args) {
|
|
|
115
115
|
|
|
116
116
|
/**
|
|
117
117
|
* Validate by a custom function. [Link](https://github.com/bedrockio/yada#custom)
|
|
118
|
-
* @param {
|
|
118
|
+
* @param {Function} fn
|
|
119
119
|
*/
|
|
120
|
-
function custom(
|
|
121
|
-
return new _Schema.default().custom(
|
|
120
|
+
function custom(fn) {
|
|
121
|
+
return new _Schema.default().custom(fn);
|
|
122
122
|
}
|
|
123
123
|
var _default = {
|
|
124
124
|
array: _array.default,
|
package/dist/cjs/localization.js
CHANGED
package/dist/cjs/messages.js
CHANGED
|
@@ -4,37 +4,29 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.getFullMessage = getFullMessage;
|
|
7
|
-
var _errors = require("./errors");
|
|
8
7
|
var _localization = require("./localization");
|
|
9
8
|
function getFullMessage(error, options) {
|
|
10
9
|
const {
|
|
11
10
|
delimiter = '\n'
|
|
12
11
|
} = options;
|
|
13
|
-
if (error.
|
|
12
|
+
if (error.message) {
|
|
13
|
+
return getLabeledMessage(error, options);
|
|
14
|
+
} else if (error.details?.length) {
|
|
14
15
|
return error.details.map(error => {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
});
|
|
20
|
-
} else {
|
|
21
|
-
return error.message;
|
|
22
|
-
}
|
|
16
|
+
return getFullMessage(error, {
|
|
17
|
+
...options,
|
|
18
|
+
path: getInnerPath(error, options)
|
|
19
|
+
});
|
|
23
20
|
}).join(delimiter);
|
|
24
|
-
} else {
|
|
25
|
-
return getLabeledMessage(error, options);
|
|
26
21
|
}
|
|
27
22
|
}
|
|
28
23
|
function getInnerPath(error, options) {
|
|
29
|
-
const {
|
|
30
|
-
type
|
|
31
|
-
} = error;
|
|
32
24
|
const {
|
|
33
25
|
path = []
|
|
34
26
|
} = options;
|
|
35
|
-
if (
|
|
27
|
+
if (error.field) {
|
|
36
28
|
return [...path, error.field];
|
|
37
|
-
} else if (
|
|
29
|
+
} else if (error.index != null) {
|
|
38
30
|
return [...path, error.index];
|
|
39
31
|
} else {
|
|
40
32
|
return path;
|
|
@@ -48,15 +40,28 @@ function getLabeledMessage(error, options) {
|
|
|
48
40
|
path = []
|
|
49
41
|
} = options;
|
|
50
42
|
const base = getBase(error.message);
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
43
|
+
let template;
|
|
44
|
+
if (base.includes('{field}')) {
|
|
45
|
+
template = base;
|
|
46
|
+
} else if (canAutoAddField(type, path)) {
|
|
47
|
+
template = `{field} ${downcase(base)}`;
|
|
48
|
+
}
|
|
49
|
+
if (template) {
|
|
50
|
+
return (0, _localization.localize)(template, {
|
|
54
51
|
field: getFieldLabel(options)
|
|
55
52
|
});
|
|
56
53
|
} else {
|
|
57
54
|
return (0, _localization.localize)(base);
|
|
58
55
|
}
|
|
59
56
|
}
|
|
57
|
+
const DISALLOWED_TYPES = ['field', 'element', 'array', 'custom'];
|
|
58
|
+
|
|
59
|
+
// Error types that have custom messages should not add the field
|
|
60
|
+
// names automatically. Instead the custom messages can include
|
|
61
|
+
// the {field} token to allow it to be interpolated if required.
|
|
62
|
+
function canAutoAddField(type, path) {
|
|
63
|
+
return type && path.length && !DISALLOWED_TYPES.includes(type);
|
|
64
|
+
}
|
|
60
65
|
function getFieldLabel(options) {
|
|
61
66
|
const {
|
|
62
67
|
path = [],
|
package/dist/cjs/object.js
CHANGED
|
@@ -21,8 +21,7 @@ class ObjectSchema extends _TypeSchema.default {
|
|
|
21
21
|
constructor(fields, meta) {
|
|
22
22
|
super(Object, {
|
|
23
23
|
...meta,
|
|
24
|
-
fields
|
|
25
|
-
message: 'Object failed validation.'
|
|
24
|
+
fields
|
|
26
25
|
});
|
|
27
26
|
this.setup();
|
|
28
27
|
}
|
|
@@ -93,7 +92,10 @@ class ObjectSchema extends _TypeSchema.default {
|
|
|
93
92
|
};
|
|
94
93
|
}
|
|
95
94
|
} catch (error) {
|
|
96
|
-
|
|
95
|
+
const {
|
|
96
|
+
message
|
|
97
|
+
} = schema.meta;
|
|
98
|
+
throw new _errors.FieldError(message, key, error.details);
|
|
97
99
|
}
|
|
98
100
|
}
|
|
99
101
|
});
|
package/dist/cjs/tuple.js
CHANGED
|
@@ -10,7 +10,6 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
|
|
|
10
10
|
class TupleSchema extends _Schema.default {
|
|
11
11
|
constructor(schemas) {
|
|
12
12
|
super({
|
|
13
|
-
message: 'Tuple failed validation.',
|
|
14
13
|
schemas
|
|
15
14
|
});
|
|
16
15
|
this.setup();
|
|
@@ -21,7 +20,8 @@ class TupleSchema extends _Schema.default {
|
|
|
21
20
|
*/
|
|
22
21
|
setup() {
|
|
23
22
|
const {
|
|
24
|
-
schemas
|
|
23
|
+
schemas,
|
|
24
|
+
message
|
|
25
25
|
} = this.meta;
|
|
26
26
|
this.assert('type', (val, options) => {
|
|
27
27
|
if (typeof val === 'string' && options.cast) {
|
|
@@ -63,11 +63,10 @@ class TupleSchema extends _Schema.default {
|
|
|
63
63
|
}
|
|
64
64
|
result.push(await schema.validate(el, options));
|
|
65
65
|
} catch (error) {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
}
|
|
66
|
+
const {
|
|
67
|
+
message
|
|
68
|
+
} = schema.meta;
|
|
69
|
+
errors.push(new _errors.ElementError(message, i, error.details));
|
|
71
70
|
}
|
|
72
71
|
}
|
|
73
72
|
if (errors.length) {
|