Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/validation/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ import { validate } from './validate.js';
* @param options.ajv - Optional Ajv instance to use for validation, if not provided a new instance will be created.
*/
const validator = (options: ValidatorOptions) => {
const before: MiddlewareFn = async (request) => {
const before: MiddlewareFn = (request) => {
if (options.inboundSchema) {
const originalEvent = structuredClone(request.event);
try {
Expand All @@ -125,7 +125,7 @@ const validator = (options: ValidatorOptions) => {
}
};

const after = async (handler: MiddyLikeRequest) => {
const after = (handler: MiddyLikeRequest) => {
if (options.outboundSchema) {
try {
handler.response = validate({
Expand Down
19 changes: 13 additions & 6 deletions packages/validation/tests/unit/decorator.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { setTimeout } from 'node:timers/promises';
import { describe, expect, it } from 'vitest';
import { validator } from '../../src/decorator.js';
import { SchemaValidationError } from '../../src/errors.js';
Expand All @@ -21,11 +22,12 @@ const outboundSchema = {
};

describe('Decorator: validator', () => {
it('should validate inbound and outbound successfully', async () => {
it('validates both inbound and outbound successfully', async () => {
// Prepare
class TestClass {
@validator({ inboundSchema, outboundSchema })
async multiply(input: { value: number }): Promise<{ result: number }> {
await setTimeout(1); // simulate some processing time
return { result: input.value * 2 };
}
}
Expand All @@ -39,11 +41,12 @@ describe('Decorator: validator', () => {
expect(output).toEqual({ result: 10 });
});

it('should throw error on inbound validation failure', async () => {
it('throws an error on inbound validation failure', async () => {
// Prepare
class TestClass {
@validator({ inboundSchema, outboundSchema })
async multiply(input: { value: number }): Promise<{ result: number }> {
await setTimeout(1); // simulate some processing time
return { result: input.value * 2 };
}
}
Expand All @@ -58,11 +61,12 @@ describe('Decorator: validator', () => {
);
});

it('should throw error on outbound validation failure', async () => {
it('throws an error on outbound validation failure', async () => {
// Prepare
class TestClassInvalid {
@validator({ inboundSchema, outboundSchema })
async multiply(_input: { value: number }) {
await setTimeout(1); // simulate some processing time
return { result: 'invalid' };
}
}
Expand All @@ -74,11 +78,12 @@ describe('Decorator: validator', () => {
);
});

it('should no-op when no schemas are provided', async () => {
it('results in a no-op when no schemas are provided', async () => {
// Prepare
class TestClassNoOp {
@validator({})
async echo(input: unknown): Promise<unknown> {
await setTimeout(1); // simulate some processing time
return input;
}
}
Expand All @@ -92,11 +97,12 @@ describe('Decorator: validator', () => {
expect(result).toEqual(data);
});

it('should validate inbound only', async () => {
it('validates the inbound schema only', async () => {
// Prepare
class TestClassInbound {
@validator({ inboundSchema })
async process(input: { value: number }): Promise<{ data: string }> {
await setTimeout(1); // simulate some processing time
return { data: JSON.stringify(input) };
}
}
Expand All @@ -110,11 +116,12 @@ describe('Decorator: validator', () => {
expect(output).toEqual({ data: JSON.stringify(input) });
});

it('should validate outbound only', async () => {
it('validates the outbound schema only', async () => {
// Prepare
class TestClassOutbound {
@validator({ outboundSchema })
async process(_input: { text: string }): Promise<{ result: number }> {
await setTimeout(1); // simulate some processing time
return { result: 42 };
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/validation/tests/unit/middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const outboundSchema = {
additionalProperties: false,
};

const baseHandler = async (event: { inputValue: unknown }) => {
const baseHandler = (event: { inputValue: unknown }) => {
return {
outputValue: event.inputValue,
};
Expand Down
Loading