@bedrockio/yada 1.0.6 → 1.0.8
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 +19 -0
- package/dist/cjs/Schema.js +8 -1
- package/dist/cjs/TypeSchema.js +3 -1
- package/dist/cjs/array.js +14 -1
- package/dist/cjs/boolean.js +3 -1
- package/dist/cjs/date.js +3 -1
- package/dist/cjs/errors.js +3 -1
- package/dist/cjs/index.js +3 -1
- package/dist/cjs/localization.js +3 -1
- package/dist/cjs/messages.js +3 -1
- package/dist/cjs/number.js +3 -1
- package/dist/cjs/object.js +18 -9
- package/dist/cjs/password.js +3 -1
- package/dist/cjs/string.js +3 -1
- package/dist/cjs/utils.js +15 -4
- package/package.json +2 -2
- package/src/Schema.js +4 -0
- package/src/array.js +12 -0
- package/src/object.js +15 -8
- package/test/all.test.js +30 -0
package/README.md
CHANGED
|
@@ -22,6 +22,7 @@ Concepts
|
|
|
22
22
|
- [Reject](#reject)
|
|
23
23
|
- [Custom](#custom)
|
|
24
24
|
- [Default](#default)
|
|
25
|
+
- [Strip](#strip)
|
|
25
26
|
- [Validation Options](#validation-options)
|
|
26
27
|
- [Error Messages](#error-messages)
|
|
27
28
|
- [Localization](#localization)
|
|
@@ -467,6 +468,24 @@ console.log(await schema.validate()); // "hi!"
|
|
|
467
468
|
console.log(await schema.validate('hello!')); // "hello!"
|
|
468
469
|
```
|
|
469
470
|
|
|
471
|
+
### Strip
|
|
472
|
+
|
|
473
|
+
The `strip` method serves as a way to conditionally exclude fields when the
|
|
474
|
+
schema is used inside an object schema:
|
|
475
|
+
|
|
476
|
+
```js
|
|
477
|
+
const schema = yd.object({
|
|
478
|
+
name: yd.string(),
|
|
479
|
+
age: yd.number().strip((val, { self }) => {
|
|
480
|
+
// That's private!
|
|
481
|
+
return !self;
|
|
482
|
+
}),
|
|
483
|
+
});
|
|
484
|
+
```
|
|
485
|
+
|
|
486
|
+
Arguments are identical to those passed to [custom](#custom). The field will be
|
|
487
|
+
stripped out if the function returns a truthy value.
|
|
488
|
+
|
|
470
489
|
## Validation Options
|
|
471
490
|
|
|
472
491
|
Validation options in Yada can be passed at runtime on validation or baked into
|
package/dist/cjs/Schema.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
exports
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
4
6
|
exports.default = void 0;
|
|
5
7
|
exports.isSchema = isSchema;
|
|
6
8
|
var _errors = require("./errors");
|
|
@@ -44,6 +46,11 @@ class Schema {
|
|
|
44
46
|
return await fn(val, options);
|
|
45
47
|
});
|
|
46
48
|
}
|
|
49
|
+
strip(strip) {
|
|
50
|
+
return this.clone({
|
|
51
|
+
strip
|
|
52
|
+
});
|
|
53
|
+
}
|
|
47
54
|
allow(...set) {
|
|
48
55
|
return this.assertEnum(set, true);
|
|
49
56
|
}
|
package/dist/cjs/TypeSchema.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
exports
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
4
6
|
exports.default = void 0;
|
|
5
7
|
var _Schema = _interopRequireDefault(require("./Schema"));
|
|
6
8
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
package/dist/cjs/array.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
exports
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
4
6
|
exports.default = void 0;
|
|
5
7
|
var _Schema = _interopRequireDefault(require("./Schema"));
|
|
6
8
|
var _errors = require("./errors");
|
|
@@ -60,6 +62,17 @@ class ArraySchema extends _Schema.default {
|
|
|
60
62
|
});
|
|
61
63
|
}
|
|
62
64
|
}
|
|
65
|
+
length(length) {
|
|
66
|
+
return this.clone().assert('length', arr => {
|
|
67
|
+
if (arr.length !== length) {
|
|
68
|
+
const s = length === 1 ? '' : 's';
|
|
69
|
+
throw new _errors.LocalizedError('Must contain exactly {length} element{s}.', {
|
|
70
|
+
length,
|
|
71
|
+
s
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
}
|
|
63
76
|
min(length) {
|
|
64
77
|
return this.clone().assert('length', arr => {
|
|
65
78
|
if (arr.length < length) {
|
package/dist/cjs/boolean.js
CHANGED
package/dist/cjs/date.js
CHANGED
package/dist/cjs/errors.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
exports
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
4
6
|
exports.ValidationError = exports.LocalizedError = exports.FieldError = exports.ElementError = exports.AssertionError = exports.ArrayError = void 0;
|
|
5
7
|
exports.isSchemaError = isSchemaError;
|
|
6
8
|
var _messages = require("./messages");
|
package/dist/cjs/index.js
CHANGED
package/dist/cjs/localization.js
CHANGED
package/dist/cjs/messages.js
CHANGED
package/dist/cjs/number.js
CHANGED
package/dist/cjs/object.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
exports
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
4
6
|
exports.default = void 0;
|
|
5
7
|
var _TypeSchema = _interopRequireDefault(require("./TypeSchema"));
|
|
6
8
|
var _errors = require("./errors");
|
|
@@ -46,9 +48,22 @@ class ObjectSchema extends _TypeSchema.default {
|
|
|
46
48
|
for (let [key, schema] of Object.entries(this.meta.fields)) {
|
|
47
49
|
this.assert('field', async (obj, options) => {
|
|
48
50
|
if (obj) {
|
|
49
|
-
|
|
51
|
+
const val = obj[key];
|
|
52
|
+
const {
|
|
53
|
+
strip
|
|
54
|
+
} = schema.meta;
|
|
55
|
+
if (strip && strip(val, options)) {
|
|
56
|
+
delete obj[key];
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
50
59
|
try {
|
|
51
|
-
|
|
60
|
+
const result = await schema.validate(val, options);
|
|
61
|
+
if (result !== undefined) {
|
|
62
|
+
return {
|
|
63
|
+
...obj,
|
|
64
|
+
[key]: result
|
|
65
|
+
};
|
|
66
|
+
}
|
|
52
67
|
} catch (error) {
|
|
53
68
|
if (error.details?.length === 1) {
|
|
54
69
|
const {
|
|
@@ -60,12 +75,6 @@ class ObjectSchema extends _TypeSchema.default {
|
|
|
60
75
|
throw new _errors.FieldError('Field failed validation.', key, error.original, error.details);
|
|
61
76
|
}
|
|
62
77
|
}
|
|
63
|
-
return {
|
|
64
|
-
...obj,
|
|
65
|
-
...(val !== undefined && {
|
|
66
|
-
[key]: val
|
|
67
|
-
})
|
|
68
|
-
};
|
|
69
78
|
}
|
|
70
79
|
});
|
|
71
80
|
}
|
package/dist/cjs/password.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
exports
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
4
6
|
exports.PASSWORD_DEFAULTS = void 0;
|
|
5
7
|
exports.validateLength = validateLength;
|
|
6
8
|
exports.validateUppercase = exports.validateSymbols = exports.validateNumbers = exports.validateLowercase = void 0;
|
package/dist/cjs/string.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
exports
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
4
6
|
exports.default = void 0;
|
|
5
7
|
var _validator = _interopRequireDefault(require("validator"));
|
|
6
8
|
var _TypeSchema = _interopRequireDefault(require("./TypeSchema"));
|
package/dist/cjs/utils.js
CHANGED
|
@@ -1,14 +1,25 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
exports
|
|
4
|
-
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
Object.defineProperty(exports, "isSchema", {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: function () {
|
|
9
|
+
return _Schema.isSchema;
|
|
10
|
+
}
|
|
11
|
+
});
|
|
12
|
+
Object.defineProperty(exports, "isSchemaError", {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
get: function () {
|
|
15
|
+
return _errors.isSchemaError;
|
|
16
|
+
}
|
|
17
|
+
});
|
|
5
18
|
exports.wrapAny = wrapAny;
|
|
6
19
|
exports.wrapArgs = wrapArgs;
|
|
7
20
|
exports.wrapSchema = wrapSchema;
|
|
8
21
|
var _Schema = _interopRequireWildcard(require("./Schema"));
|
|
9
|
-
exports.isSchema = _Schema.isSchema;
|
|
10
22
|
var _errors = require("./errors");
|
|
11
|
-
exports.isSchemaError = _errors.isSchemaError;
|
|
12
23
|
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
13
24
|
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
14
25
|
function wrapSchema(Class) {
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bedrockio/yada",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.8",
|
|
4
4
|
"description": "Validation library inspired by Joi.",
|
|
5
5
|
"scripts": {
|
|
6
|
-
"test": "
|
|
6
|
+
"test": "jest",
|
|
7
7
|
"lint": "eslint",
|
|
8
8
|
"build": "scripts/build",
|
|
9
9
|
"prepublishOnly": "npm run build"
|
package/src/Schema.js
CHANGED
package/src/array.js
CHANGED
|
@@ -59,6 +59,18 @@ class ArraySchema extends Schema {
|
|
|
59
59
|
}
|
|
60
60
|
}
|
|
61
61
|
|
|
62
|
+
length(length) {
|
|
63
|
+
return this.clone().assert('length', (arr) => {
|
|
64
|
+
if (arr.length !== length) {
|
|
65
|
+
const s = length === 1 ? '' : 's';
|
|
66
|
+
throw new LocalizedError('Must contain exactly {length} element{s}.', {
|
|
67
|
+
length,
|
|
68
|
+
s,
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
|
|
62
74
|
min(length) {
|
|
63
75
|
return this.clone().assert('length', (arr) => {
|
|
64
76
|
if (arr.length < length) {
|
package/src/object.js
CHANGED
|
@@ -38,9 +38,22 @@ class ObjectSchema extends TypeSchema {
|
|
|
38
38
|
for (let [key, schema] of Object.entries(this.meta.fields)) {
|
|
39
39
|
this.assert('field', async (obj, options) => {
|
|
40
40
|
if (obj) {
|
|
41
|
-
|
|
41
|
+
const val = obj[key];
|
|
42
|
+
const { strip } = schema.meta;
|
|
43
|
+
|
|
44
|
+
if (strip && strip(val, options)) {
|
|
45
|
+
delete obj[key];
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
|
|
42
49
|
try {
|
|
43
|
-
|
|
50
|
+
const result = await schema.validate(val, options);
|
|
51
|
+
if (result !== undefined) {
|
|
52
|
+
return {
|
|
53
|
+
...obj,
|
|
54
|
+
[key]: result,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
44
57
|
} catch (error) {
|
|
45
58
|
if (error.details?.length === 1) {
|
|
46
59
|
const { message, original } = error.details[0];
|
|
@@ -54,12 +67,6 @@ class ObjectSchema extends TypeSchema {
|
|
|
54
67
|
);
|
|
55
68
|
}
|
|
56
69
|
}
|
|
57
|
-
return {
|
|
58
|
-
...obj,
|
|
59
|
-
...(val !== undefined && {
|
|
60
|
-
[key]: val,
|
|
61
|
-
}),
|
|
62
|
-
};
|
|
63
70
|
}
|
|
64
71
|
});
|
|
65
72
|
}
|
package/test/all.test.js
CHANGED
|
@@ -693,6 +693,29 @@ describe('custom', () => {
|
|
|
693
693
|
});
|
|
694
694
|
});
|
|
695
695
|
|
|
696
|
+
describe('strip', () => {
|
|
697
|
+
it('should conditionally strip out fields', async () => {
|
|
698
|
+
const schema = yd.object({
|
|
699
|
+
name: yd.string(),
|
|
700
|
+
age: yd.number().strip((val, { self }) => {
|
|
701
|
+
return !self;
|
|
702
|
+
}),
|
|
703
|
+
});
|
|
704
|
+
const result = await schema.validate(
|
|
705
|
+
{
|
|
706
|
+
name: 'Brett',
|
|
707
|
+
age: 25,
|
|
708
|
+
},
|
|
709
|
+
{
|
|
710
|
+
isPrivate: true,
|
|
711
|
+
}
|
|
712
|
+
);
|
|
713
|
+
expect(result).toEqual({
|
|
714
|
+
name: 'Brett',
|
|
715
|
+
});
|
|
716
|
+
});
|
|
717
|
+
});
|
|
718
|
+
|
|
696
719
|
describe('array', () => {
|
|
697
720
|
it('should validate an optional array', async () => {
|
|
698
721
|
const schema = yd.array();
|
|
@@ -770,6 +793,13 @@ describe('array', () => {
|
|
|
770
793
|
await assertFail(schema, [{ bar: 'hi' }], ['Unknown field "bar".']);
|
|
771
794
|
});
|
|
772
795
|
|
|
796
|
+
it('should validate a fixed length', async () => {
|
|
797
|
+
const schema = yd.array().length(2);
|
|
798
|
+
await assertFail(schema, [1], ['Must contain exactly 2 elements.']);
|
|
799
|
+
await assertPass(schema, [1, 2]);
|
|
800
|
+
await assertFail(schema, [1, 2, 3], ['Must contain exactly 2 elements.']);
|
|
801
|
+
});
|
|
802
|
+
|
|
773
803
|
it('should validate a minimum length', async () => {
|
|
774
804
|
const schema = yd.array().min(1);
|
|
775
805
|
await assertPass(schema, ['one']);
|