Skip to content

Commit e121fa7

Browse files
committed
Merge branch 'feature/validator-decorator' of https://github.com/VatsalGoel3/powertools-lambda-typescript into feature/validator-decorator
2 parents e93f513 + 8676b00 commit e121fa7

File tree

1 file changed

+27
-22
lines changed

1 file changed

+27
-22
lines changed

packages/validation/src/decorator.ts

+27-22
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,38 @@
11
import { SchemaValidationError } from './errors.js';
22
import type { ValidatorOptions } from './types.js';
33
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) {
85
return (
9-
_target,
10-
_propertyKey,
11-
descriptor: TypedPropertyDescriptor<AsyncMethod>
6+
_target: unknown,
7+
_propertyKey: string | symbol,
8+
descriptor: PropertyDescriptor
129
) => {
1310
if (!descriptor.value) {
1411
return descriptor;
1512
}
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) {
1722
return descriptor;
1823
}
1924
const originalMethod = descriptor.value;
20-
descriptor.value = async function (...args: unknown[]): Promise<unknown> {
25+
descriptor.value = async function (...args: unknown[]) {
2126
let validatedInput = args[0];
22-
if (options.inboundSchema) {
27+
if (inboundSchema) {
2328
try {
2429
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,
3136
});
3237
} catch (error) {
3338
throw new SchemaValidationError('Inbound validation failed', error);
@@ -37,17 +42,17 @@ export function validator(options: ValidatorOptions): MethodDecorator {
3742
validatedInput,
3843
...args.slice(1),
3944
]);
40-
if (options.outboundSchema) {
45+
if (outboundSchema) {
4146
try {
4247
return validate({
4348
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,
4853
});
4954
} catch (error) {
50-
throw new SchemaValidationError('Outbound validation failed', error);
55+
throw new SchemaValidationError('Outbound Validation failed', error);
5156
}
5257
}
5358
return result;

0 commit comments

Comments
 (0)