-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improved zod types and async refinements
- Loading branch information
1 parent
5d2de4a
commit 2008e2a
Showing
10 changed files
with
818 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { createRule, type RegleRuleDefinition } from '@regle/core'; | ||
|
||
describe('createRule', () => { | ||
it('should error when creating a rule without a function', () => { | ||
assert.throw(() => createRule({} as any), 'Validator must be a function'); | ||
}); | ||
|
||
it('should create a rule definition without parameters', () => { | ||
const rule = createRule({ | ||
validator() { | ||
return true; | ||
}, | ||
message: '', | ||
}); | ||
|
||
expect(rule.exec('fooo')).toBe(true); | ||
expectTypeOf(rule).toMatchTypeOf<RegleRuleDefinition<unknown, [], false, true, unknown>>(); | ||
}); | ||
|
||
it('should handle metadata', () => { | ||
const rule = createRule({ | ||
validator() { | ||
return { $valid: true }; | ||
}, | ||
message: '', | ||
}); | ||
|
||
expect(rule.exec('fooo')).toBe(true); | ||
expectTypeOf(rule).toMatchTypeOf< | ||
RegleRuleDefinition< | ||
unknown, | ||
[], | ||
false, | ||
{ | ||
$valid: true; | ||
}, | ||
unknown | ||
> | ||
>(); | ||
}); | ||
|
||
it('should handle async metadata', async () => { | ||
const rule = createRule({ | ||
async validator() { | ||
return Promise.resolve({ $valid: true }); | ||
}, | ||
message: '', | ||
}); | ||
|
||
expect(await rule.exec('fooo')).toBe(true); | ||
expectTypeOf(rule).toMatchTypeOf< | ||
RegleRuleDefinition< | ||
unknown, | ||
[], | ||
true, | ||
{ | ||
$valid: true; | ||
}, | ||
unknown | ||
> | ||
>(); | ||
}); | ||
|
||
it('should be false if not given boolean or $valid response', async () => { | ||
const rule = createRule({ | ||
validator() { | ||
return 'foo' as unknown as boolean; | ||
}, | ||
message: '', | ||
}); | ||
|
||
expect(await rule.exec('fooo')).toBe(false); | ||
}); | ||
}); |
Oops, something went wrong.