Skip to content

Commit 32820bb

Browse files
committed
Size and missed
1 parent 7f95fc4 commit 32820bb

3 files changed

Lines changed: 55 additions & 1 deletion

File tree

‎packages/contracts/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"size-limit": [
3636
{
3737
"path": "./dist/contracts.js",
38-
"limit": "774 B"
38+
"limit": "829 B"
3939
}
4040
]
4141
}

‎packages/contracts/src/contracts.test.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
tuple,
1313
type Contract,
1414
nothing,
15+
anything,
1516
} from './index';
1617

1718
describe('bool', () => {
@@ -625,3 +626,37 @@ describe('nothing', () => {
625626
`);
626627
});
627628
});
629+
630+
describe('anything', () => {
631+
it('accepts any field', () => {
632+
const cntrct = obj({ key: anything });
633+
634+
expect(cntrct.isData({})).toBeTruthy();
635+
expect(cntrct.getErrorMessages({})).toEqual([]);
636+
637+
expect(cntrct.isData({ key: 1 })).toBeTruthy();
638+
expect(cntrct.getErrorMessages({ key: 1 })).toEqual([]);
639+
640+
expect(cntrct.isData({ key: null })).toBeTruthy();
641+
expect(cntrct.getErrorMessages({ key: null })).toEqual([]);
642+
643+
expect(cntrct.isData({ key: undefined })).toBeTruthy();
644+
expect(cntrct.getErrorMessages({ key: undefined })).toEqual([]);
645+
});
646+
647+
it('does not break original', () => {
648+
const cntrct = obj({ key: or(num, anything) });
649+
650+
expect(cntrct.isData({ key: 1 })).toBeTruthy();
651+
expect(cntrct.getErrorMessages({ key: 1 })).toEqual([]);
652+
653+
expect(cntrct.isData({ key: null })).toBeTruthy();
654+
expect(cntrct.getErrorMessages({ key: null })).toEqual([]);
655+
656+
expect(cntrct.isData({ key: undefined })).toBeTruthy();
657+
expect(cntrct.getErrorMessages({ key: undefined })).toEqual([]);
658+
659+
expect(cntrct.isData({ key: 'a' })).toBeTruthy();
660+
expect(cntrct.getErrorMessages({ key: 'a' })).toEqual([]);
661+
});
662+
});

‎packages/contracts/src/index.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,25 @@ export function tuple(...contracts: Array<Contract<unknown, any>>): any {
396396
*/
397397
export const nothing = or(val(null), val(undefined));
398398

399+
/**
400+
* _Contract_ that allows any value, basically a no-op.
401+
*
402+
* @since v1.1.0
403+
*
404+
* @example
405+
*
406+
* anything.isData('hello') === true;
407+
* anything.isData(42) === true;
408+
* anything.isData({}) === true;
409+
* anything.isData([]) === true;
410+
* anything.isData(null) === true;
411+
* anything.isData(undefined) === true;
412+
*/
413+
export const anything: Contract<unknown, unknown> = {
414+
isData: (x): x is unknown => true,
415+
getErrorMessages: () => [],
416+
};
417+
399418
// -- utils
400419

401420
function createSimpleContract<T>(exepctedType: string): Contract<unknown, T> {

0 commit comments

Comments
 (0)