11import type {
22 BaseIssue ,
3- BaseValidation ,
3+ BaseTransformation ,
44 ErrorMessage ,
55} from '../../types/index.ts' ;
66import { _addIssue } from '../../utils/index.ts' ;
77
8- export type Guard < TInput > = ( input : TInput ) => input is TInput ;
8+ export type Guard < TInput , TOutput extends TInput > = (
9+ input : TInput
10+ ) => input is TOutput ;
911
10- // eslint-disable-next-line @typescript-eslint/no-explicit-any
11- export type InferGuarded < TGuard extends Guard < any > > = TGuard extends (
12- // eslint-disable-next-line @typescript-eslint/no-explicit-any
13- input : any
14- ) => input is infer TGuarded
15- ? TGuarded
16- : never ;
12+ /**
13+ * Guard issue interface.
14+ */
15+ export interface GuardIssue < TInput , TOutput extends TInput >
16+ extends BaseIssue < TInput > {
17+ /**
18+ * The issue kind.
19+ */
20+ readonly kind : 'transformation' ;
21+ /**
22+ * The validation type.
23+ */
24+ readonly type : 'guard' ;
25+ /**
26+ * The validation requirement.
27+ */
28+ readonly requirement : Guard < TInput , TOutput > ;
29+ }
1730
1831/**
1932 * Guard action interface.
2033 */
2134export interface GuardAction <
2235 TInput ,
23- TGuard extends Guard < TInput > ,
24- TMessage extends ErrorMessage < GuardIssue < TInput , TGuard > > | undefined ,
25- > extends BaseValidation <
26- TInput ,
27- InferGuarded < TGuard > ,
28- GuardIssue < TInput , TGuard >
29- > {
36+ TOutput extends TInput ,
37+ TMessage extends ErrorMessage < GuardIssue < TInput , TOutput > > | undefined ,
38+ > extends BaseTransformation < TInput , TOutput , GuardIssue < TInput , TOutput > > {
3039 /**
3140 * The action type.
3241 */
@@ -35,46 +44,25 @@ export interface GuardAction<
3544 * The action reference.
3645 */
3746 readonly reference : typeof guard ;
38- /**
39- * The expected property.
40- */
41- readonly expects : null ;
4247 /**
4348 * The guard function.
4449 */
45- readonly requirement : TGuard ;
50+ readonly requirement : Guard < TInput , TOutput > ;
4651 /**
4752 * The error message.
4853 */
4954 readonly message : TMessage ;
5055}
51-
52- /**
53- * Guard issue interface.
54- */
55- export interface GuardIssue < TInput , TGuard extends Guard < TInput > >
56- extends BaseIssue < TInput > {
57- /**
58- * The validation type.
59- */
60- type : 'guard' ;
61- /**
62- * The validation requirement.
63- */
64- requirement : TGuard ;
65- }
66-
6756/**
6857 * Creates a guard validation action.
6958 *
7059 * @param requirement The guard function.
7160 *
7261 * @returns A guard action.
7362 */
74- // overload for a known input, i.e. within a pipe
75- export function guard < TInput , const TGuard extends Guard < TInput > > (
76- requirement : TGuard
77- ) : GuardAction < TInput , TGuard , undefined > ;
63+ export function guard < TInput , TOutput extends TInput > (
64+ requirement : Guard < TInput , TOutput >
65+ ) : GuardAction < TInput , TOutput , undefined > ;
7866
7967/**
8068 * Creates a guard validation action.
@@ -84,64 +72,29 @@ export function guard<TInput, const TGuard extends Guard<TInput>>(
8472 *
8573 * @returns A guard action.
8674 */
87- // overload for a known input, i.e. within a pipe
8875export function guard <
8976 TInput ,
90- const TGuard extends Guard < TInput > ,
91- const TMessage extends ErrorMessage < GuardIssue < TInput , TGuard > > | undefined ,
77+ TOutput extends TInput ,
78+ const TMessage extends ErrorMessage < GuardIssue < TInput , TOutput > > | undefined ,
9279> (
93- requirement : TGuard ,
80+ requirement : Guard < TInput , TOutput > ,
9481 message : TMessage
95- ) : GuardAction < TInput , TGuard , TMessage > ;
96-
97- /**
98- * Creates a guard validation action.
99- *
100- * @param requirement The guard function.
101- *
102- * @returns A guard action.
103- */
104- // overload for an unknown input, i.e. standalone
105- // eslint-disable-next-line @typescript-eslint/no-explicit-any
106- export function guard < const TGuard extends Guard < any > > (
107- requirement : TGuard
108- ) : GuardAction < Parameters < TGuard > [ 0 ] , TGuard , undefined > ;
109-
110- /**
111- * Creates a guard validation action.
112- *
113- * @param requirement The guard function.
114- * @param message The error message.
115- *
116- * @returns A guard action.
117- */
118- // overload for an unknown input, i.e. standalone
119- export function guard <
120- // eslint-disable-next-line @typescript-eslint/no-explicit-any
121- const TGuard extends Guard < any > ,
122- const TMessage extends
123- | ErrorMessage < GuardIssue < Parameters < TGuard > [ 0 ] , TGuard > >
124- | undefined ,
125- > (
126- requirement : TGuard ,
127- message : TMessage
128- ) : GuardAction < Parameters < TGuard > [ 0 ] , TGuard , TMessage > ;
82+ ) : GuardAction < TInput , TOutput , TMessage > ;
12983
13084// @__NO_SIDE_EFFECTS__
13185export function guard (
132- requirement : Guard < unknown > ,
133- message ?: ErrorMessage < GuardIssue < unknown , Guard < unknown > > >
86+ requirement : Guard < unknown , unknown > ,
87+ message ?: ErrorMessage < GuardIssue < unknown , unknown > >
13488) : GuardAction <
13589 unknown ,
136- Guard < unknown > ,
137- ErrorMessage < GuardIssue < unknown , Guard < unknown > > > | undefined
90+ unknown ,
91+ ErrorMessage < GuardIssue < unknown , unknown > > | undefined
13892> {
13993 return {
140- kind : 'validation ' ,
94+ kind : 'transformation ' ,
14195 type : 'guard' ,
14296 reference : guard ,
14397 async : false ,
144- expects : null ,
14598 requirement,
14699 message,
147100 '~run' ( dataset , config ) {
0 commit comments