|
1 |
| -import { validate, assert as superstructAssert, is, string } from 'superstruct'; |
| 1 | +import { |
| 2 | + validate, |
| 3 | + assert as superstructAssert, |
| 4 | + is, |
| 5 | + string, |
| 6 | + union, |
| 7 | + literal, |
| 8 | + max, |
| 9 | + number, |
| 10 | +} from 'superstruct'; |
2 | 11 |
|
3 | 12 | import {
|
4 | 13 | assert,
|
@@ -111,6 +120,73 @@ describe('object', () => {
|
111 | 120 | });
|
112 | 121 | });
|
113 | 122 |
|
| 123 | +describe('exactOptional', () => { |
| 124 | + const simpleStruct = object({ |
| 125 | + foo: exactOptional(string()), |
| 126 | + }); |
| 127 | + |
| 128 | + it.each([ |
| 129 | + { struct: simpleStruct, obj: {}, expected: true }, |
| 130 | + { struct: simpleStruct, obj: { foo: undefined }, expected: false }, |
| 131 | + { struct: simpleStruct, obj: { foo: 'hi' }, expected: true }, |
| 132 | + { struct: simpleStruct, obj: { bar: 'hi' }, expected: false }, |
| 133 | + { struct: simpleStruct, obj: { foo: 1 }, expected: false }, |
| 134 | + ])( |
| 135 | + 'returns $expected for is($obj, <struct>)', |
| 136 | + ({ struct, obj, expected }) => { |
| 137 | + expect(is(obj, struct)).toBe(expected); |
| 138 | + }, |
| 139 | + ); |
| 140 | + |
| 141 | + const nestedStruct = object({ |
| 142 | + foo: object({ |
| 143 | + bar: exactOptional(string()), |
| 144 | + }), |
| 145 | + }); |
| 146 | + |
| 147 | + it.each([ |
| 148 | + { struct: nestedStruct, obj: { foo: {} }, expected: true }, |
| 149 | + { struct: nestedStruct, obj: { foo: { bar: 'hi' } }, expected: true }, |
| 150 | + { |
| 151 | + struct: nestedStruct, |
| 152 | + obj: { foo: { bar: undefined } }, |
| 153 | + expected: false, |
| 154 | + }, |
| 155 | + ])( |
| 156 | + 'returns $expected for is($obj, <struct>)', |
| 157 | + ({ struct, obj, expected }) => { |
| 158 | + expect(is(obj, struct)).toBe(expected); |
| 159 | + }, |
| 160 | + ); |
| 161 | + |
| 162 | + const structWithUndefined = object({ |
| 163 | + foo: exactOptional(union([string(), literal(undefined)])), |
| 164 | + }); |
| 165 | + |
| 166 | + it.each([ |
| 167 | + { struct: structWithUndefined, obj: {}, expected: true }, |
| 168 | + { struct: structWithUndefined, obj: { foo: undefined }, expected: true }, |
| 169 | + { struct: structWithUndefined, obj: { foo: 'hi' }, expected: true }, |
| 170 | + { struct: structWithUndefined, obj: { bar: 'hi' }, expected: false }, |
| 171 | + { struct: structWithUndefined, obj: { foo: 1 }, expected: false }, |
| 172 | + ])( |
| 173 | + 'returns $expected for is($obj, <struct>)', |
| 174 | + ({ struct, obj, expected }) => { |
| 175 | + expect(is(obj, struct)).toBe(expected); |
| 176 | + }, |
| 177 | + ); |
| 178 | + |
| 179 | + it('supports refinements', () => { |
| 180 | + const struct = object({ |
| 181 | + foo: exactOptional(max(number(), 0)), |
| 182 | + }); |
| 183 | + |
| 184 | + expect(is({ foo: 0 }, struct)).toBe(true); |
| 185 | + expect(is({ foo: -1 }, struct)).toBe(true); |
| 186 | + expect(is({ foo: 1 }, struct)).toBe(false); |
| 187 | + }); |
| 188 | +}); |
| 189 | + |
114 | 190 | describe('json', () => {
|
115 | 191 | beforeEach(() => {
|
116 | 192 | const actual = jest.requireActual('superstruct');
|
|
0 commit comments