@bedrockio/yada 1.0.7 → 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 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
@@ -46,6 +46,11 @@ class Schema {
46
46
  return await fn(val, options);
47
47
  });
48
48
  }
49
+ strip(strip) {
50
+ return this.clone({
51
+ strip
52
+ });
53
+ }
49
54
  allow(...set) {
50
55
  return this.assertEnum(set, true);
51
56
  }
@@ -48,9 +48,22 @@ class ObjectSchema extends _TypeSchema.default {
48
48
  for (let [key, schema] of Object.entries(this.meta.fields)) {
49
49
  this.assert('field', async (obj, options) => {
50
50
  if (obj) {
51
- let val;
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
+ }
52
59
  try {
53
- val = await schema.validate(obj[key], options);
60
+ const result = await schema.validate(val, options);
61
+ if (result !== undefined) {
62
+ return {
63
+ ...obj,
64
+ [key]: result
65
+ };
66
+ }
54
67
  } catch (error) {
55
68
  if (error.details?.length === 1) {
56
69
  const {
@@ -62,12 +75,6 @@ class ObjectSchema extends _TypeSchema.default {
62
75
  throw new _errors.FieldError('Field failed validation.', key, error.original, error.details);
63
76
  }
64
77
  }
65
- return {
66
- ...obj,
67
- ...(val !== undefined && {
68
- [key]: val
69
- })
70
- };
71
78
  }
72
79
  });
73
80
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bedrockio/yada",
3
- "version": "1.0.7",
3
+ "version": "1.0.8",
4
4
  "description": "Validation library inspired by Joi.",
5
5
  "scripts": {
6
6
  "test": "jest",
package/src/Schema.js CHANGED
@@ -46,6 +46,10 @@ export default class Schema {
46
46
  });
47
47
  }
48
48
 
49
+ strip(strip) {
50
+ return this.clone({ strip });
51
+ }
52
+
49
53
  allow(...set) {
50
54
  return this.assertEnum(set, true);
51
55
  }
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
- let val;
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
- val = await schema.validate(obj[key], options);
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();