Skip to content

Commit

Permalink
Size and missed
Browse files Browse the repository at this point in the history
  • Loading branch information
igorkamyshev committed Aug 1, 2024
1 parent 7f95fc4 commit 32820bb
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"size-limit": [
{
"path": "./dist/contracts.js",
"limit": "774 B"
"limit": "829 B"
}
]
}
35 changes: 35 additions & 0 deletions packages/contracts/src/contracts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
tuple,
type Contract,
nothing,
anything,
} from './index';

describe('bool', () => {
Expand Down Expand Up @@ -625,3 +626,37 @@ describe('nothing', () => {
`);
});
});

describe('anything', () => {
it('accepts any field', () => {
const cntrct = obj({ key: anything });

expect(cntrct.isData({})).toBeTruthy();
expect(cntrct.getErrorMessages({})).toEqual([]);

expect(cntrct.isData({ key: 1 })).toBeTruthy();
expect(cntrct.getErrorMessages({ key: 1 })).toEqual([]);

expect(cntrct.isData({ key: null })).toBeTruthy();
expect(cntrct.getErrorMessages({ key: null })).toEqual([]);

expect(cntrct.isData({ key: undefined })).toBeTruthy();
expect(cntrct.getErrorMessages({ key: undefined })).toEqual([]);
});

it('does not break original', () => {
const cntrct = obj({ key: or(num, anything) });

expect(cntrct.isData({ key: 1 })).toBeTruthy();
expect(cntrct.getErrorMessages({ key: 1 })).toEqual([]);

expect(cntrct.isData({ key: null })).toBeTruthy();
expect(cntrct.getErrorMessages({ key: null })).toEqual([]);

expect(cntrct.isData({ key: undefined })).toBeTruthy();
expect(cntrct.getErrorMessages({ key: undefined })).toEqual([]);

expect(cntrct.isData({ key: 'a' })).toBeTruthy();
expect(cntrct.getErrorMessages({ key: 'a' })).toEqual([]);
});
});
19 changes: 19 additions & 0 deletions packages/contracts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,25 @@ export function tuple(...contracts: Array<Contract<unknown, any>>): any {
*/
export const nothing = or(val(null), val(undefined));

/**
* _Contract_ that allows any value, basically a no-op.
*
* @since v1.1.0
*
* @example
*
* anything.isData('hello') === true;
* anything.isData(42) === true;
* anything.isData({}) === true;
* anything.isData([]) === true;
* anything.isData(null) === true;
* anything.isData(undefined) === true;
*/
export const anything: Contract<unknown, unknown> = {
isData: (x): x is unknown => true,
getErrorMessages: () => [],
};

// -- utils

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

0 comments on commit 32820bb

Please sign in to comment.