-
-
Notifications
You must be signed in to change notification settings - Fork 284
feat: add guard action for narrowing using type predicates #1204
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
EskiMojo14
wants to merge
4
commits into
fabian-hiller:main
Choose a base branch
from
EskiMojo14:guard
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,93 @@ | ||
| import { describe, expectTypeOf, test } from 'vitest'; | ||
| import { pipe } from '../../methods/index.ts'; | ||
| import { literal, number, string } from '../../schemas/index.ts'; | ||
| import type { InferInput, InferIssue, InferOutput } from '../../types/index.ts'; | ||
| import type { GuardAction, GuardIssue } from './guard.ts'; | ||
| import { guard } from './guard.ts'; | ||
|
|
||
| describe('guard', () => { | ||
| type PixelString = `${number}px`; | ||
| const isPixelString = (input: string): input is PixelString => | ||
| /^\d+px$/u.test(input); | ||
|
|
||
| describe('should return action object', () => { | ||
| test('with no message', () => { | ||
| expectTypeOf(guard(isPixelString)).toEqualTypeOf< | ||
| GuardAction<string, typeof isPixelString, undefined> | ||
| >(); | ||
| }); | ||
| test('with string message', () => { | ||
| expectTypeOf(guard(isPixelString, 'message')).toEqualTypeOf< | ||
| GuardAction<string, typeof isPixelString, 'message'> | ||
| >(); | ||
| }); | ||
|
|
||
| test('with function message', () => { | ||
| expectTypeOf(guard(isPixelString, () => 'message')).toEqualTypeOf< | ||
| GuardAction<string, typeof isPixelString, () => string> | ||
| >(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('should infer correct types', () => { | ||
| test('of input', () => { | ||
| expectTypeOf< | ||
| InferInput<GuardAction<string, typeof isPixelString, undefined>> | ||
| >().toEqualTypeOf<string>(); | ||
| }); | ||
|
|
||
| test('of output', () => { | ||
| expectTypeOf< | ||
| InferOutput<GuardAction<string, typeof isPixelString, undefined>> | ||
| >().toEqualTypeOf<PixelString>(); | ||
| }); | ||
|
|
||
| test('of issue', () => { | ||
| expectTypeOf< | ||
| InferIssue<GuardAction<string, typeof isPixelString, undefined>> | ||
| >().toEqualTypeOf<GuardIssue<string, typeof isPixelString>>(); | ||
| }); | ||
| }); | ||
|
|
||
| test('should infer correct type in pipe', () => { | ||
| // eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
| const schema = pipe( | ||
| string(), | ||
| guard((input) => { | ||
| expectTypeOf(input).toEqualTypeOf<string>(); | ||
| return isPixelString(input); | ||
| }) | ||
| ); | ||
| expectTypeOf<InferOutput<typeof schema>>().toEqualTypeOf<PixelString>(); | ||
| }); | ||
|
|
||
| test("should error if pipe input doesn't match", () => { | ||
| pipe( | ||
| number(), | ||
| // @ts-expect-error | ||
| guard(isPixelString) | ||
| ); | ||
| }); | ||
|
|
||
| test('should allow narrower input or wider output', () => { | ||
| // eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
| const narrowInput = pipe( | ||
| string(), | ||
| // guard allows wider input than current pipe | ||
| guard( | ||
| (input: unknown) => typeof input === 'string' && isPixelString(input) | ||
| ) | ||
| ); | ||
|
|
||
| expectTypeOf< | ||
| InferOutput<typeof narrowInput> | ||
| >().toEqualTypeOf<PixelString>(); | ||
|
|
||
| // guarded type is wider than current pipe | ||
| // so we keep the narrower type | ||
| // eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
| const wideOutput = pipe(literal('123px'), guard(isPixelString)); | ||
|
|
||
| expectTypeOf<InferOutput<typeof wideOutput>>().toEqualTypeOf<'123px'>(); | ||
| }); | ||
| }); |
This file contains hidden or 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,90 @@ | ||
| import { describe, expect, test } from 'vitest'; | ||
| import type { FailureDataset } from '../../types/dataset.ts'; | ||
| import type { GuardAction, GuardIssue } from './guard.ts'; | ||
| import { guard } from './guard.ts'; | ||
|
|
||
| describe('guard', () => { | ||
| type PixelString = `${number}px`; | ||
| const isPixelString = (input: string): input is PixelString => | ||
| /^\d+px$/u.test(input); | ||
|
|
||
| const baseAction: Omit< | ||
| GuardAction<string, typeof isPixelString, undefined>, | ||
| 'message' | ||
| > = { | ||
| kind: 'transformation', | ||
| type: 'guard', | ||
| reference: guard, | ||
| requirement: isPixelString, | ||
| async: false, | ||
| '~run': expect.any(Function), | ||
| }; | ||
|
|
||
| describe('should return action object', () => { | ||
| test('with undefined message', () => { | ||
| const action: GuardAction<string, typeof isPixelString, undefined> = { | ||
| ...baseAction, | ||
| message: undefined, | ||
| }; | ||
| expect(guard(isPixelString)).toStrictEqual(action); | ||
| expect(guard(isPixelString, undefined)).toStrictEqual(action); | ||
| }); | ||
|
|
||
| test('with string message', () => { | ||
| const action: GuardAction<string, typeof isPixelString, 'message'> = { | ||
| ...baseAction, | ||
| message: 'message', | ||
| }; | ||
| expect(guard(isPixelString, 'message')).toStrictEqual(action); | ||
| }); | ||
|
|
||
| test('with function message', () => { | ||
| const message = () => 'message'; | ||
| const action: GuardAction<string, typeof isPixelString, typeof message> = | ||
| { | ||
| ...baseAction, | ||
| message, | ||
| }; | ||
| expect(guard(isPixelString, message)).toStrictEqual(action); | ||
| }); | ||
| }); | ||
|
|
||
| test('should return dataset without issues', () => { | ||
| const action = guard(isPixelString); | ||
| const outputDataset = { typed: true, value: '123px' }; | ||
| expect(action['~run']({ typed: true, value: '123px' }, {})).toStrictEqual( | ||
| outputDataset | ||
| ); | ||
| }); | ||
|
|
||
| test('should return dataset with issues', () => { | ||
| const action = guard(isPixelString, 'message'); | ||
| const baseIssue: Omit< | ||
| GuardIssue<string, typeof isPixelString>, | ||
| 'input' | 'received' | ||
| > = { | ||
| kind: 'transformation', | ||
| type: 'guard', | ||
| expected: null, | ||
| message: 'message', | ||
| requirement: isPixelString, | ||
| path: undefined, | ||
| issues: undefined, | ||
| lang: undefined, | ||
| abortEarly: undefined, | ||
| abortPipeEarly: undefined, | ||
| }; | ||
|
|
||
| expect(action['~run']({ typed: true, value: '123' }, {})).toStrictEqual({ | ||
| typed: false, | ||
| value: '123', | ||
| issues: [ | ||
| { | ||
| ...baseIssue, | ||
| input: '123', | ||
| received: '"123"', | ||
| }, | ||
| ], | ||
| } satisfies FailureDataset<GuardIssue<string, typeof isPixelString>>); | ||
| }); | ||
| }); |
This file contains hidden or 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,158 @@ | ||
| import type { | ||
| BaseIssue, | ||
| BaseTransformation, | ||
| ErrorMessage, | ||
| } from '../../types/index.ts'; | ||
| import { _addIssue } from '../../utils/index.ts'; | ||
|
|
||
| type BaseGuard<TInput> = ( | ||
| input: TInput | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| ) => input is any; | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| type InferGuarded<TGuard extends BaseGuard<any>> = TGuard extends ( | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| input: any | ||
| ) => input is infer TOutput | ||
| ? TOutput | ||
| : unknown; | ||
|
|
||
| /** | ||
| * Guard issue interface. | ||
| */ | ||
| export interface GuardIssue<TInput, TGuard extends BaseGuard<TInput>> | ||
| extends BaseIssue<TInput> { | ||
| /** | ||
| * The issue kind. | ||
| */ | ||
| readonly kind: 'transformation'; | ||
| /** | ||
| * The validation type. | ||
| */ | ||
| readonly type: 'guard'; | ||
| /** | ||
| * The validation requirement. | ||
| */ | ||
| readonly requirement: TGuard; | ||
| } | ||
|
|
||
| /** | ||
| * Guard action interface. | ||
| */ | ||
| export interface GuardAction< | ||
| TInput, | ||
| TGuard extends BaseGuard<TInput>, | ||
| TMessage extends ErrorMessage<GuardIssue<TInput, TGuard>> | undefined, | ||
| > extends BaseTransformation< | ||
| TInput, | ||
| // intersect in case guard is actually wider | ||
| TInput & InferGuarded<TGuard>, | ||
| GuardIssue<TInput, TGuard> | ||
| > { | ||
| /** | ||
| * The action type. | ||
| */ | ||
| readonly type: 'guard'; | ||
| /** | ||
| * The action reference. | ||
| */ | ||
| readonly reference: typeof guard; | ||
| /** | ||
| * The guard function. | ||
| */ | ||
| readonly requirement: TGuard; | ||
| /** | ||
| * The error message. | ||
| */ | ||
| readonly message: TMessage; | ||
| } | ||
| /** | ||
| * Creates a guard validation action. | ||
| * | ||
| * @param requirement The guard function. | ||
| * | ||
| * @returns A guard action. | ||
| */ | ||
| // known input from pipe | ||
| export function guard<TInput, const TGuard extends BaseGuard<TInput>>( | ||
| requirement: TGuard | ||
| ): GuardAction<TInput, TGuard, undefined>; | ||
|
|
||
| /** | ||
| * Creates a guard validation action. | ||
| * | ||
| * @param requirement The guard function. | ||
| * | ||
| * @returns A guard action. | ||
| */ | ||
| // unknown input, e.g. standalone | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| export function guard<const TGuard extends BaseGuard<any>>( | ||
| requirement: TGuard | ||
| ): GuardAction<Parameters<TGuard>[0], TGuard, undefined>; | ||
|
|
||
| /** | ||
| * Creates a guard validation action. | ||
| * | ||
| * @param requirement The guard function. | ||
| * @param message The error message. | ||
| * | ||
| * @returns A guard action. | ||
| */ | ||
| // known input from pipe | ||
| export function guard< | ||
| TInput, | ||
| const TGuard extends BaseGuard<TInput>, | ||
| const TMessage extends ErrorMessage<GuardIssue<TInput, TGuard>> | undefined, | ||
| >( | ||
| requirement: TGuard, | ||
| message: TMessage | ||
| ): GuardAction<TInput, TGuard, TMessage>; | ||
|
|
||
| /** | ||
| * Creates a guard validation action. | ||
| * | ||
| * @param requirement The guard function. | ||
| * @param message The error message. | ||
| * | ||
| * @returns A guard action. | ||
| */ | ||
| // unknown input, e.g. standalone | ||
| export function guard< | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| const TGuard extends BaseGuard<any>, | ||
| const TMessage extends | ||
| | ErrorMessage<GuardIssue<Parameters<TGuard>[0], TGuard>> | ||
| | undefined, | ||
| >( | ||
| requirement: TGuard, | ||
| message: TMessage | ||
| ): GuardAction<Parameters<TGuard>[0], TGuard, TMessage>; | ||
|
|
||
| // @__NO_SIDE_EFFECTS__ | ||
| export function guard( | ||
| requirement: BaseGuard<unknown>, | ||
| message?: ErrorMessage<GuardIssue<unknown, BaseGuard<unknown>>> | ||
| ): GuardAction< | ||
| unknown, | ||
| BaseGuard<unknown>, | ||
| ErrorMessage<GuardIssue<unknown, BaseGuard<unknown>>> | undefined | ||
| > { | ||
| return { | ||
| kind: 'transformation', | ||
| type: 'guard', | ||
| reference: guard, | ||
| async: false, | ||
| requirement, | ||
| message, | ||
| '~run'(dataset, config) { | ||
| if (dataset.typed && !this.requirement(dataset.value)) { | ||
| _addIssue(this, 'input', dataset, config); | ||
| // @ts-expect-error | ||
| dataset.typed = false; | ||
| } | ||
| return dataset; | ||
| }, | ||
| }; | ||
| } | ||
This file contains hidden or 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 @@ | ||
| export * from './guard.ts'; |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.