@bedrockio/yada 1.0.26 → 1.0.28
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/dist/cjs/Schema.js +6 -1
- package/dist/cjs/array.js +7 -2
- package/dist/cjs/object.js +3 -18
- package/dist/cjs/utils.js +19 -1
- package/package.json +1 -1
- package/src/Schema.js +6 -1
- package/src/array.js +6 -2
- package/src/object.js +1 -18
- package/src/utils.js +18 -0
- package/types/Schema.d.ts.map +1 -1
- package/types/array.d.ts.map +1 -1
- package/types/object.d.ts.map +1 -1
- package/types/utils.d.ts +2 -0
- package/types/utils.d.ts.map +1 -1
package/dist/cjs/Schema.js
CHANGED
|
@@ -6,6 +6,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
6
6
|
exports.default = void 0;
|
|
7
7
|
exports.isSchema = isSchema;
|
|
8
8
|
var _errors = require("./errors");
|
|
9
|
+
var _utils = require("./utils");
|
|
9
10
|
const INITIAL_TYPES = ['default', 'required', 'type', 'transform'];
|
|
10
11
|
const REQUIRED_TYPES = ['default', 'required'];
|
|
11
12
|
|
|
@@ -249,6 +250,10 @@ class Schema {
|
|
|
249
250
|
for (let el of set) {
|
|
250
251
|
if (isSchema(el)) {
|
|
251
252
|
try {
|
|
253
|
+
// Must not pass cast option down when allowing
|
|
254
|
+
// other schema types as they may be allowed, for
|
|
255
|
+
// example allowing a string or array of strings.
|
|
256
|
+
options = (0, _utils.omit)(options, 'cast');
|
|
252
257
|
return await el.validate(val, options);
|
|
253
258
|
} catch (error) {
|
|
254
259
|
continue;
|
|
@@ -257,7 +262,7 @@ class Schema {
|
|
|
257
262
|
return;
|
|
258
263
|
}
|
|
259
264
|
}
|
|
260
|
-
throw new _errors.LocalizedError(msg, {
|
|
265
|
+
throw new _errors.LocalizedError(options.message || msg, {
|
|
261
266
|
types: types.join(', ')
|
|
262
267
|
});
|
|
263
268
|
}
|
package/dist/cjs/array.js
CHANGED
|
@@ -6,6 +6,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
6
6
|
exports.default = _default;
|
|
7
7
|
var _Schema = _interopRequireDefault(require("./Schema"));
|
|
8
8
|
var _errors = require("./errors");
|
|
9
|
+
var _utils = require("./utils");
|
|
9
10
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
10
11
|
class ArraySchema extends _Schema.default {
|
|
11
12
|
constructor(schemas) {
|
|
@@ -21,7 +22,8 @@ class ArraySchema extends _Schema.default {
|
|
|
21
22
|
*/
|
|
22
23
|
setup() {
|
|
23
24
|
const {
|
|
24
|
-
schemas
|
|
25
|
+
schemas,
|
|
26
|
+
message
|
|
25
27
|
} = this.meta;
|
|
26
28
|
const schema = schemas.length > 1 ? new _Schema.default().allow(schemas) : schemas[0];
|
|
27
29
|
this.assert('type', (val, options) => {
|
|
@@ -40,6 +42,9 @@ class ArraySchema extends _Schema.default {
|
|
|
40
42
|
for (let i = 0; i < arr.length; i++) {
|
|
41
43
|
const el = arr[i];
|
|
42
44
|
try {
|
|
45
|
+
// Allow enum message to take
|
|
46
|
+
// precedence over generic array message.
|
|
47
|
+
options = (0, _utils.omit)(options, 'message');
|
|
43
48
|
result.push(await schema.validate(el, options));
|
|
44
49
|
} catch (error) {
|
|
45
50
|
if (error.details?.length === 1) {
|
|
@@ -50,7 +55,7 @@ class ArraySchema extends _Schema.default {
|
|
|
50
55
|
}
|
|
51
56
|
}
|
|
52
57
|
if (errors.length) {
|
|
53
|
-
throw new _errors.ArrayError(
|
|
58
|
+
throw new _errors.ArrayError(message, errors);
|
|
54
59
|
} else {
|
|
55
60
|
return result;
|
|
56
61
|
}
|
package/dist/cjs/object.js
CHANGED
|
@@ -6,6 +6,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
6
6
|
exports.default = _default;
|
|
7
7
|
var _TypeSchema = _interopRequireDefault(require("./TypeSchema"));
|
|
8
8
|
var _errors = require("./errors");
|
|
9
|
+
var _utils = require("./utils");
|
|
9
10
|
var _Schema = _interopRequireDefault(require("./Schema"));
|
|
10
11
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
11
12
|
const BASE_ASSERTIONS = ['type', 'transform', 'field'];
|
|
@@ -148,7 +149,7 @@ class ObjectSchema extends _TypeSchema.default {
|
|
|
148
149
|
if (Array.isArray(names[0])) {
|
|
149
150
|
names = names[0];
|
|
150
151
|
}
|
|
151
|
-
const fields = pick(this.meta.fields, names);
|
|
152
|
+
const fields = (0, _utils.pick)(this.meta.fields, names);
|
|
152
153
|
return new ObjectSchema(fields, {
|
|
153
154
|
...this.meta
|
|
154
155
|
});
|
|
@@ -161,7 +162,7 @@ class ObjectSchema extends _TypeSchema.default {
|
|
|
161
162
|
if (Array.isArray(names[0])) {
|
|
162
163
|
names = names[0];
|
|
163
164
|
}
|
|
164
|
-
const fields = omit(this.meta.fields, names);
|
|
165
|
+
const fields = (0, _utils.omit)(this.meta.fields, names);
|
|
165
166
|
return new ObjectSchema(fields, {
|
|
166
167
|
...this.meta
|
|
167
168
|
});
|
|
@@ -200,22 +201,6 @@ function expandDotProperties(obj) {
|
|
|
200
201
|
}
|
|
201
202
|
return result;
|
|
202
203
|
}
|
|
203
|
-
function pick(obj, keys) {
|
|
204
|
-
const result = {};
|
|
205
|
-
for (let key of keys) {
|
|
206
|
-
result[key] = obj[key];
|
|
207
|
-
}
|
|
208
|
-
return result;
|
|
209
|
-
}
|
|
210
|
-
function omit(obj, keys) {
|
|
211
|
-
const result = {};
|
|
212
|
-
for (let key of Object.keys(obj || {})) {
|
|
213
|
-
if (!keys.includes(key)) {
|
|
214
|
-
result[key] = obj[key];
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
return result;
|
|
218
|
-
}
|
|
219
204
|
|
|
220
205
|
/**
|
|
221
206
|
* Creates an [object schema](https://github.com/bedrockio/yada#object).
|
package/dist/cjs/utils.js
CHANGED
|
@@ -15,5 +15,23 @@ Object.defineProperty(exports, "isSchemaError", {
|
|
|
15
15
|
return _errors.isSchemaError;
|
|
16
16
|
}
|
|
17
17
|
});
|
|
18
|
+
exports.omit = omit;
|
|
19
|
+
exports.pick = pick;
|
|
18
20
|
var _Schema = require("./Schema");
|
|
19
|
-
var _errors = require("./errors");
|
|
21
|
+
var _errors = require("./errors");
|
|
22
|
+
function pick(obj, keys) {
|
|
23
|
+
const result = {};
|
|
24
|
+
for (let key of keys) {
|
|
25
|
+
result[key] = obj[key];
|
|
26
|
+
}
|
|
27
|
+
return result;
|
|
28
|
+
}
|
|
29
|
+
function omit(obj, keys) {
|
|
30
|
+
const result = {};
|
|
31
|
+
for (let key of Object.keys(obj || {})) {
|
|
32
|
+
if (!keys.includes(key)) {
|
|
33
|
+
result[key] = obj[key];
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return result;
|
|
37
|
+
}
|
package/package.json
CHANGED
package/src/Schema.js
CHANGED
|
@@ -5,6 +5,7 @@ import {
|
|
|
5
5
|
LocalizedError,
|
|
6
6
|
ArrayError,
|
|
7
7
|
} from './errors';
|
|
8
|
+
import { omit } from './utils';
|
|
8
9
|
|
|
9
10
|
const INITIAL_TYPES = ['default', 'required', 'type', 'transform'];
|
|
10
11
|
const REQUIRED_TYPES = ['default', 'required'];
|
|
@@ -232,6 +233,10 @@ export default class Schema {
|
|
|
232
233
|
for (let el of set) {
|
|
233
234
|
if (isSchema(el)) {
|
|
234
235
|
try {
|
|
236
|
+
// Must not pass cast option down when allowing
|
|
237
|
+
// other schema types as they may be allowed, for
|
|
238
|
+
// example allowing a string or array of strings.
|
|
239
|
+
options = omit(options, 'cast');
|
|
235
240
|
return await el.validate(val, options);
|
|
236
241
|
} catch (error) {
|
|
237
242
|
continue;
|
|
@@ -240,7 +245,7 @@ export default class Schema {
|
|
|
240
245
|
return;
|
|
241
246
|
}
|
|
242
247
|
}
|
|
243
|
-
throw new LocalizedError(msg, {
|
|
248
|
+
throw new LocalizedError(options.message || msg, {
|
|
244
249
|
types: types.join(', '),
|
|
245
250
|
});
|
|
246
251
|
}
|
package/src/array.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import Schema from './Schema';
|
|
2
2
|
import { ArrayError, ElementError, LocalizedError } from './errors';
|
|
3
|
+
import { omit } from './utils';
|
|
3
4
|
|
|
4
5
|
class ArraySchema extends Schema {
|
|
5
6
|
constructor(schemas) {
|
|
@@ -11,7 +12,7 @@ class ArraySchema extends Schema {
|
|
|
11
12
|
* @private
|
|
12
13
|
*/
|
|
13
14
|
setup() {
|
|
14
|
-
const { schemas } = this.meta;
|
|
15
|
+
const { schemas, message } = this.meta;
|
|
15
16
|
const schema =
|
|
16
17
|
schemas.length > 1 ? new Schema().allow(schemas) : schemas[0];
|
|
17
18
|
|
|
@@ -32,6 +33,9 @@ class ArraySchema extends Schema {
|
|
|
32
33
|
for (let i = 0; i < arr.length; i++) {
|
|
33
34
|
const el = arr[i];
|
|
34
35
|
try {
|
|
36
|
+
// Allow enum message to take
|
|
37
|
+
// precedence over generic array message.
|
|
38
|
+
options = omit(options, 'message');
|
|
35
39
|
result.push(await schema.validate(el, options));
|
|
36
40
|
} catch (error) {
|
|
37
41
|
if (error.details?.length === 1) {
|
|
@@ -44,7 +48,7 @@ class ArraySchema extends Schema {
|
|
|
44
48
|
}
|
|
45
49
|
}
|
|
46
50
|
if (errors.length) {
|
|
47
|
-
throw new ArrayError(
|
|
51
|
+
throw new ArrayError(message, errors);
|
|
48
52
|
} else {
|
|
49
53
|
return result;
|
|
50
54
|
}
|
package/src/object.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import TypeSchema from './TypeSchema';
|
|
2
2
|
import { FieldError, LocalizedError } from './errors';
|
|
3
|
+
import { pick, omit } from './utils';
|
|
3
4
|
import Schema from './Schema';
|
|
4
5
|
|
|
5
6
|
const BASE_ASSERTIONS = ['type', 'transform', 'field'];
|
|
@@ -197,24 +198,6 @@ function expandDotProperties(obj) {
|
|
|
197
198
|
return result;
|
|
198
199
|
}
|
|
199
200
|
|
|
200
|
-
function pick(obj, keys) {
|
|
201
|
-
const result = {};
|
|
202
|
-
for (let key of keys) {
|
|
203
|
-
result[key] = obj[key];
|
|
204
|
-
}
|
|
205
|
-
return result;
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
function omit(obj, keys) {
|
|
209
|
-
const result = {};
|
|
210
|
-
for (let key of Object.keys(obj || {})) {
|
|
211
|
-
if (!keys.includes(key)) {
|
|
212
|
-
result[key] = obj[key];
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
return result;
|
|
216
|
-
}
|
|
217
|
-
|
|
218
201
|
/**
|
|
219
202
|
* Creates an [object schema](https://github.com/bedrockio/yada#object).
|
|
220
203
|
*
|
package/src/utils.js
CHANGED
|
@@ -1,2 +1,20 @@
|
|
|
1
1
|
export { isSchema } from './Schema';
|
|
2
2
|
export { isSchemaError } from './errors';
|
|
3
|
+
|
|
4
|
+
export function pick(obj, keys) {
|
|
5
|
+
const result = {};
|
|
6
|
+
for (let key of keys) {
|
|
7
|
+
result[key] = obj[key];
|
|
8
|
+
}
|
|
9
|
+
return result;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function omit(obj, keys) {
|
|
13
|
+
const result = {};
|
|
14
|
+
for (let key of Object.keys(obj || {})) {
|
|
15
|
+
if (!keys.includes(key)) {
|
|
16
|
+
result[key] = obj[key];
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return result;
|
|
20
|
+
}
|
package/types/Schema.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Schema.d.ts","sourceRoot":"","sources":["../src/Schema.js"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"Schema.d.ts","sourceRoot":"","sources":["../src/Schema.js"],"names":[],"mappings":"AAsVA,4CAEC;AA5UD;;GAEG;AAEH;IACE,uBAGC;IAFC,kBAAoB;IACpB,SAAgB;IAKlB;;OAEG;IACH,YAFa,IAAI,CAQhB;IAED;;;OAGG;IACH,qBAFa,IAAI,CAQhB;IAED;;;;OAIG;IACH,gBAHW,eAAe,GACb,IAAI,CAmBhB;IAED;;;;OAIG;IACH,mBAFa,IAAI,CAIhB;IAED;;;OAGG;IACH,sBAFa,IAAI,CAIhB;IAED;;;OAGG;IACH,uBAFa,IAAI,CAIhB;IAED;;OAEG;IACH,uBAFa,IAAI,CAIhB;IAED;;OAEG;IACH,gBAFa,IAAI,CAShB;IAED;;OAEG;IACH,+BAFa,IAAI,CAMhB;IAED;;OAEG;IACH,uBAFa,IAAI,CAIhB;IAED,iDAqCC;IAED;;OAEG;IACH,kBAFa,IAAI,CAOhB;IAED;;;OAGG;IACH,qBAFa,IAAI,CAMhB;IAED,2BAUC;IAED,kBAEC;IAED,4BAMC;IAID;;OAEG;IACH,kCAFa,IAAI,CAmChB;IAED;;OAEG;IACH,4BAFa,IAAI,CAUhB;IAED,oCAKC;IAED;;OAEG;IACH,oBAFa,IAAI,CAShB;IAED,gCAGC;IAED,qEAUC;IAED;;;;;;;;MAoCC;CACF;8BAvUY,CAAC,EAAE,UAAU,CAAC,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,UAAU,CAAC"}
|
package/types/array.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"array.d.ts","sourceRoot":"","sources":["../src/array.js"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"array.d.ts","sourceRoot":"","sources":["../src/array.js"],"names":[],"mappings":"AA2IA;;;;;;;GAOG;AACH,8CALc,MAAM,iBAUnB;;AApJD;IACE,0BAGC;IAED;;OAEG;IACH,cA2CC;IAED,iCAUC;IAED,8BAUC;IAED,8BAaC;IAED,sBAaC;CA2BF"}
|
package/types/object.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"object.d.ts","sourceRoot":"","sources":["../src/object.js"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"object.d.ts","sourceRoot":"","sources":["../src/object.js"],"names":[],"mappings":"AAwMA;;;;;;GAMG;AACH,uCAJW,SAAS,gBAMnB;;;;AA1MD;;GAEG;AAEH;IAUE;;OAEG;IACH,cAkEC;IAED;;OAEG;IACH,kBAEC;IAED;;OAEG;IAEH,YAHW,SAAS,GAAC,MAAM,gBAkC1B;IAED;;OAEG;IACH,gBAFc,MAAM,kBAWnB;IAED;;OAEG;IACH,gBAFc,MAAM,kBAWnB;CAcF"}
|
package/types/utils.d.ts
CHANGED
package/types/utils.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.js"],"names":[],"mappings":""}
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.js"],"names":[],"mappings":"AAGA,8CAMC;AAED,8CAQC"}
|