|
7 | 7 | StructError, |
8 | 8 | array, |
9 | 9 | type, |
| 10 | + union, |
10 | 11 | } from '../../src' |
11 | 12 |
|
12 | 13 | describe('mask', () => { |
@@ -44,19 +45,36 @@ describe('mask', () => { |
44 | 45 | it('masking of a nested type', () => { |
45 | 46 | const S = object({ |
46 | 47 | id: string(), |
47 | | - sub: array(type({ prop: string() })), |
| 48 | + sub: array( |
| 49 | + type({ prop: string(), defaultedProp: defaulted(string(), '42') }) |
| 50 | + ), |
| 51 | + union: array(union([object({ prop: string() }), type({ k: string() })])), |
48 | 52 | }) |
49 | 53 | const value = { |
50 | 54 | id: '1', |
51 | 55 | unknown: true, |
52 | 56 | sub: [{ prop: '2', unknown: true }], |
| 57 | + union: [ |
| 58 | + { prop: '3', unknown: true }, |
| 59 | + { k: '4', unknown: true }, |
| 60 | + ], |
53 | 61 | } |
54 | 62 | expect(mask(value, S)).toStrictEqual({ |
55 | 63 | id: '1', |
56 | | - sub: [{ prop: '2', unknown: true }], |
| 64 | + sub: [{ prop: '2', unknown: true, defaultedProp: '42' }], |
| 65 | + union: [{ prop: '3' }, { k: '4', unknown: true }], |
57 | 66 | }) |
58 | 67 | }) |
59 | 68 |
|
| 69 | + it('masking succeeds for objects with extra props in union', () => { |
| 70 | + const S = union([ |
| 71 | + object({ a: string(), defaultedProp: defaulted(string(), '42') }), |
| 72 | + object({ b: string() }), |
| 73 | + ]) |
| 74 | + const value = { a: '1', extraProp: 42 } |
| 75 | + expect(mask(value, S)).toStrictEqual({ a: '1', defaultedProp: '42' }) |
| 76 | + }) |
| 77 | + |
60 | 78 | it('masking of a top level type and nested object', () => { |
61 | 79 | const S = type({ |
62 | 80 | id: string(), |
|
0 commit comments