@bedrockio/yada 1.0.8 → 1.0.9
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 +27 -5
- package/dist/cjs/Schema.js +5 -0
- package/dist/cjs/object.js +10 -1
- package/package.json +1 -1
- package/src/Schema.js +6 -0
- package/src/object.js +10 -1
- package/test/all.test.js +50 -0
package/README.md
CHANGED
|
@@ -301,11 +301,6 @@ const schema = yd
|
|
|
301
301
|
.required();
|
|
302
302
|
```
|
|
303
303
|
|
|
304
|
-
#### Methods:
|
|
305
|
-
|
|
306
|
-
- `append` - Appends the passed argument to create a new object schema. Accepts
|
|
307
|
-
either an object or another `.object()` schema.
|
|
308
|
-
|
|
309
304
|
## Date
|
|
310
305
|
|
|
311
306
|
Dates are similar to the basic types with the exception that in addition to date
|
|
@@ -377,6 +372,33 @@ await schema.validate(true); // pass
|
|
|
377
372
|
await schema.validate('true'); // error!
|
|
378
373
|
```
|
|
379
374
|
|
|
375
|
+
### Append
|
|
376
|
+
|
|
377
|
+
Appends another schema:
|
|
378
|
+
|
|
379
|
+
```js
|
|
380
|
+
const schema1 = yd.string();
|
|
381
|
+
const schema2 = yd.custom((str) => {
|
|
382
|
+
if (str === 'foo') {
|
|
383
|
+
throw new Error('Cannot be foo!');
|
|
384
|
+
}
|
|
385
|
+
});
|
|
386
|
+
const schema = schema1.append(schema2);
|
|
387
|
+
```
|
|
388
|
+
|
|
389
|
+
Note that when applied to an object schema the fields will be merged. In this
|
|
390
|
+
case a plain object is also accepted:
|
|
391
|
+
|
|
392
|
+
```js
|
|
393
|
+
const schema1 = yd.object({
|
|
394
|
+
foo: yd.string(),
|
|
395
|
+
});
|
|
396
|
+
const schema2 = yd.object({
|
|
397
|
+
bar: yd.string(),
|
|
398
|
+
});
|
|
399
|
+
const schema = schema1.append(schema2);
|
|
400
|
+
```
|
|
401
|
+
|
|
380
402
|
### Custom
|
|
381
403
|
|
|
382
404
|
The `custom` schema allows for custom validations expressed in code. A custom
|
package/dist/cjs/Schema.js
CHANGED
package/dist/cjs/object.js
CHANGED
|
@@ -80,7 +80,16 @@ class ObjectSchema extends _TypeSchema.default {
|
|
|
80
80
|
}
|
|
81
81
|
}
|
|
82
82
|
append(arg) {
|
|
83
|
-
|
|
83
|
+
let schema;
|
|
84
|
+
if (arg instanceof ObjectSchema) {
|
|
85
|
+
schema = arg;
|
|
86
|
+
} else if ((0, _Schema.isSchema)(arg)) {
|
|
87
|
+
// If the schema is of a different type then
|
|
88
|
+
// simply append it and don't merge fields.
|
|
89
|
+
return super.append(arg);
|
|
90
|
+
} else {
|
|
91
|
+
schema = new ObjectSchema(arg);
|
|
92
|
+
}
|
|
84
93
|
const fields = {
|
|
85
94
|
...this.meta.fields,
|
|
86
95
|
...schema.meta.fields
|
package/package.json
CHANGED
package/src/Schema.js
CHANGED
|
@@ -127,6 +127,12 @@ export default class Schema {
|
|
|
127
127
|
return clone;
|
|
128
128
|
}
|
|
129
129
|
|
|
130
|
+
append(schema) {
|
|
131
|
+
const merged = this.clone(schema.meta);
|
|
132
|
+
merged.assertions = [...this.assertions, ...schema.assertions];
|
|
133
|
+
return merged;
|
|
134
|
+
}
|
|
135
|
+
|
|
130
136
|
// Private
|
|
131
137
|
|
|
132
138
|
assertEnum(set, allow) {
|
package/src/object.js
CHANGED
|
@@ -73,7 +73,16 @@ class ObjectSchema extends TypeSchema {
|
|
|
73
73
|
}
|
|
74
74
|
|
|
75
75
|
append(arg) {
|
|
76
|
-
|
|
76
|
+
let schema;
|
|
77
|
+
if (arg instanceof ObjectSchema) {
|
|
78
|
+
schema = arg;
|
|
79
|
+
} else if (isSchema(arg)) {
|
|
80
|
+
// If the schema is of a different type then
|
|
81
|
+
// simply append it and don't merge fields.
|
|
82
|
+
return super.append(arg);
|
|
83
|
+
} else {
|
|
84
|
+
schema = new ObjectSchema(arg);
|
|
85
|
+
}
|
|
77
86
|
|
|
78
87
|
const fields = {
|
|
79
88
|
...this.meta.fields,
|
package/test/all.test.js
CHANGED
|
@@ -978,6 +978,56 @@ describe('default', () => {
|
|
|
978
978
|
});
|
|
979
979
|
});
|
|
980
980
|
|
|
981
|
+
describe('append', () => {
|
|
982
|
+
it('should allow appending to a string schema', async () => {
|
|
983
|
+
const custom = yd.custom((str) => {
|
|
984
|
+
if (str === 'fop') {
|
|
985
|
+
throw new Error('You misspelled foo!');
|
|
986
|
+
}
|
|
987
|
+
});
|
|
988
|
+
|
|
989
|
+
const schema = yd.string().append(custom);
|
|
990
|
+
|
|
991
|
+
await assertPass(schema, 'foo');
|
|
992
|
+
await assertFail(schema, 'fop', ['You misspelled foo!']);
|
|
993
|
+
});
|
|
994
|
+
|
|
995
|
+
it('should append a custom schema to an object schema', async () => {
|
|
996
|
+
const custom = yd.custom((obj) => {
|
|
997
|
+
if (obj.foo === 'fop') {
|
|
998
|
+
throw new Error('You misspelled foo!');
|
|
999
|
+
}
|
|
1000
|
+
});
|
|
1001
|
+
|
|
1002
|
+
const schema = yd
|
|
1003
|
+
.object({
|
|
1004
|
+
foo: yd.string(),
|
|
1005
|
+
})
|
|
1006
|
+
.append(custom);
|
|
1007
|
+
|
|
1008
|
+
await assertPass(schema, { foo: 'foo' });
|
|
1009
|
+
await assertFail(schema, { foo: 'fop' }, ['You misspelled foo!']);
|
|
1010
|
+
});
|
|
1011
|
+
|
|
1012
|
+
it('should preserve defaults', async () => {
|
|
1013
|
+
const custom = yd.custom((str) => {
|
|
1014
|
+
if (str === 'fop') {
|
|
1015
|
+
throw new Error('You misspelled foo!');
|
|
1016
|
+
}
|
|
1017
|
+
});
|
|
1018
|
+
const schema = yd.string().required();
|
|
1019
|
+
|
|
1020
|
+
await assertPass(schema.default('foo').append(custom));
|
|
1021
|
+
await assertPass(schema.append(custom).default('foo'));
|
|
1022
|
+
await assertFail(schema.default('fop').append(custom), undefined, [
|
|
1023
|
+
'You misspelled foo!',
|
|
1024
|
+
]);
|
|
1025
|
+
await assertFail(schema.append(custom).default('fop'), undefined, [
|
|
1026
|
+
'You misspelled foo!',
|
|
1027
|
+
]);
|
|
1028
|
+
});
|
|
1029
|
+
});
|
|
1030
|
+
|
|
981
1031
|
describe('serialization', () => {
|
|
982
1032
|
it('should correctly serialize object error', async () => {
|
|
983
1033
|
const schema = yd.object({
|