1
1
import { SchemaValidationError } from './errors.js' ;
2
2
import type { ValidatorOptions } from './types.js' ;
3
3
import { validate } from './validate.js' ;
4
-
5
- type AsyncMethod = ( ...args : unknown [ ] ) => Promise < unknown > ;
6
-
7
- export function validator ( options : ValidatorOptions ) : MethodDecorator {
4
+ export function validator ( options : ValidatorOptions ) {
8
5
return (
9
- _target ,
10
- _propertyKey ,
11
- descriptor : TypedPropertyDescriptor < AsyncMethod >
6
+ _target : unknown ,
7
+ _propertyKey : string | symbol ,
8
+ descriptor : PropertyDescriptor
12
9
) => {
13
10
if ( ! descriptor . value ) {
14
11
return descriptor ;
15
12
}
16
- if ( ! options . inboundSchema && ! options . outboundSchema ) {
13
+ const {
14
+ inboundSchema,
15
+ outboundSchema,
16
+ envelope,
17
+ formats,
18
+ externalRefs,
19
+ ajv,
20
+ } = options ;
21
+ if ( ! inboundSchema && ! outboundSchema ) {
17
22
return descriptor ;
18
23
}
19
24
const originalMethod = descriptor . value ;
20
- descriptor . value = async function ( ...args : unknown [ ] ) : Promise < unknown > {
25
+ descriptor . value = async function ( ...args : unknown [ ] ) {
21
26
let validatedInput = args [ 0 ] ;
22
- if ( options . inboundSchema ) {
27
+ if ( inboundSchema ) {
23
28
try {
24
29
validatedInput = validate ( {
25
- payload : args [ 0 ] ,
26
- schema : options . inboundSchema ,
27
- envelope : options . envelope ,
28
- formats : options . formats ,
29
- externalRefs : options . externalRefs ,
30
- ajv : options . ajv ,
30
+ payload : validatedInput ,
31
+ schema : inboundSchema ,
32
+ envelope : envelope ,
33
+ formats : formats ,
34
+ externalRefs : externalRefs ,
35
+ ajv : ajv ,
31
36
} ) ;
32
37
} catch ( error ) {
33
38
throw new SchemaValidationError ( 'Inbound validation failed' , error ) ;
@@ -37,17 +42,17 @@ export function validator(options: ValidatorOptions): MethodDecorator {
37
42
validatedInput ,
38
43
...args . slice ( 1 ) ,
39
44
] ) ;
40
- if ( options . outboundSchema ) {
45
+ if ( outboundSchema ) {
41
46
try {
42
47
return validate ( {
43
48
payload : result ,
44
- schema : options . outboundSchema ,
45
- formats : options . formats ,
46
- externalRefs : options . externalRefs ,
47
- ajv : options . ajv ,
49
+ schema : outboundSchema ,
50
+ formats : formats ,
51
+ externalRefs : externalRefs ,
52
+ ajv : ajv ,
48
53
} ) ;
49
54
} catch ( error ) {
50
- throw new SchemaValidationError ( 'Outbound validation failed' , error ) ;
55
+ throw new SchemaValidationError ( 'Outbound Validation failed' , error ) ;
51
56
}
52
57
}
53
58
return result ;
0 commit comments