From 14dabe67f7c21f0c0ba7c941664117049e72478f Mon Sep 17 00:00:00 2001 From: Andrea Amorosi Date: Wed, 24 Sep 2025 16:38:37 +0200 Subject: [PATCH 1/2] style(validation): apply stricter linting --- packages/validation/src/middleware.ts | 4 +- .../validation/tests/unit/decorator.test.ts | 40 +++++++++---------- .../validation/tests/unit/middleware.test.ts | 2 +- 3 files changed, 21 insertions(+), 25 deletions(-) diff --git a/packages/validation/src/middleware.ts b/packages/validation/src/middleware.ts index af6d953033..59a5093c62 100644 --- a/packages/validation/src/middleware.ts +++ b/packages/validation/src/middleware.ts @@ -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 { @@ -125,7 +125,7 @@ const validator = (options: ValidatorOptions) => { } }; - const after = async (handler: MiddyLikeRequest) => { + const after = (handler: MiddyLikeRequest) => { if (options.outboundSchema) { try { handler.response = validate({ diff --git a/packages/validation/tests/unit/decorator.test.ts b/packages/validation/tests/unit/decorator.test.ts index 28e6cd27fd..2b61baa6b5 100644 --- a/packages/validation/tests/unit/decorator.test.ts +++ b/packages/validation/tests/unit/decorator.test.ts @@ -21,11 +21,11 @@ const outboundSchema = { }; describe('Decorator: validator', () => { - it('should validate inbound and outbound successfully', async () => { + it('should validate inbound and outbound successfully', () => { // Prepare class TestClass { @validator({ inboundSchema, outboundSchema }) - async multiply(input: { value: number }): Promise<{ result: number }> { + multiply(input: { value: number }): { result: number } { return { result: input.value * 2 }; } } @@ -33,17 +33,17 @@ describe('Decorator: validator', () => { const input = { value: 5 }; // Act - const output = await instance.multiply(input); + const output = instance.multiply(input); // Assess expect(output).toEqual({ result: 10 }); }); - it('should throw error on inbound validation failure', async () => { + it('should throw error on inbound validation failure', () => { // Prepare class TestClass { @validator({ inboundSchema, outboundSchema }) - async multiply(input: { value: number }): Promise<{ result: number }> { + multiply(input: { value: number }): { result: number } { return { result: input.value * 2 }; } } @@ -53,32 +53,28 @@ describe('Decorator: validator', () => { }; // Act & Assess - await expect(instance.multiply(invalidInput)).rejects.toThrow( - SchemaValidationError - ); + expect(instance.multiply(invalidInput)).toThrow(SchemaValidationError); }); - it('should throw error on outbound validation failure', async () => { + it('should throw error on outbound validation failure', () => { // Prepare class TestClassInvalid { @validator({ inboundSchema, outboundSchema }) - async multiply(_input: { value: number }) { + multiply(_input: { value: number }) { return { result: 'invalid' }; } } const instance = new TestClassInvalid(); // Act & Assess - await expect(instance.multiply({ value: 5 })).rejects.toThrow( - SchemaValidationError - ); + expect(instance.multiply({ value: 5 })).toThrow(SchemaValidationError); }); - it('should no-op when no schemas are provided', async () => { + it('should no-op when no schemas are provided', () => { // Prepare class TestClassNoOp { @validator({}) - async echo(input: unknown): Promise { + echo(input: unknown): unknown { return input; } } @@ -86,17 +82,17 @@ describe('Decorator: validator', () => { const data = { foo: 'bar' }; // Act - const result = await instance.echo(data); + const result = instance.echo(data); // Assess expect(result).toEqual(data); }); - it('should validate inbound only', async () => { + it('should validate inbound only', () => { // Prepare class TestClassInbound { @validator({ inboundSchema }) - async process(input: { value: number }): Promise<{ data: string }> { + process(input: { value: number }): { data: string } { return { data: JSON.stringify(input) }; } } @@ -104,17 +100,17 @@ describe('Decorator: validator', () => { const input = { value: 10 }; // Act - const output = await instance.process(input); + const output = instance.process(input); // Assess expect(output).toEqual({ data: JSON.stringify(input) }); }); - it('should validate outbound only', async () => { + it('should validate outbound only', () => { // Prepare class TestClassOutbound { @validator({ outboundSchema }) - async process(_input: { text: string }): Promise<{ result: number }> { + process(_input: { text: string }): { result: number } { return { result: 42 }; } } @@ -122,7 +118,7 @@ describe('Decorator: validator', () => { const input = { text: 'hello' }; // Act - const output = await instance.process(input); + const output = instance.process(input); // Assess expect(output).toEqual({ result: 42 }); diff --git a/packages/validation/tests/unit/middleware.test.ts b/packages/validation/tests/unit/middleware.test.ts index 1d2796638a..6b76d66d65 100644 --- a/packages/validation/tests/unit/middleware.test.ts +++ b/packages/validation/tests/unit/middleware.test.ts @@ -22,7 +22,7 @@ const outboundSchema = { additionalProperties: false, }; -const baseHandler = async (event: { inputValue: unknown }) => { +const baseHandler = (event: { inputValue: unknown }) => { return { outputValue: event.inputValue, }; From 27cacacbc0df638531ed73f5b10af5fa3df4557f Mon Sep 17 00:00:00 2001 From: Andrea Amorosi Date: Wed, 24 Sep 2025 16:42:15 +0200 Subject: [PATCH 2/2] style(validation): apply stricter linting --- .../validation/tests/unit/decorator.test.ts | 47 ++++++++++++------- 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/packages/validation/tests/unit/decorator.test.ts b/packages/validation/tests/unit/decorator.test.ts index 2b61baa6b5..423ab34cc6 100644 --- a/packages/validation/tests/unit/decorator.test.ts +++ b/packages/validation/tests/unit/decorator.test.ts @@ -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'; @@ -21,11 +22,12 @@ const outboundSchema = { }; describe('Decorator: validator', () => { - it('should validate inbound and outbound successfully', () => { + it('validates both inbound and outbound successfully', async () => { // Prepare class TestClass { @validator({ inboundSchema, outboundSchema }) - multiply(input: { value: number }): { result: number } { + async multiply(input: { value: number }): Promise<{ result: number }> { + await setTimeout(1); // simulate some processing time return { result: input.value * 2 }; } } @@ -33,17 +35,18 @@ describe('Decorator: validator', () => { const input = { value: 5 }; // Act - const output = instance.multiply(input); + const output = await instance.multiply(input); // Assess expect(output).toEqual({ result: 10 }); }); - it('should throw error on inbound validation failure', () => { + it('throws an error on inbound validation failure', async () => { // Prepare class TestClass { @validator({ inboundSchema, outboundSchema }) - multiply(input: { value: number }): { result: number } { + async multiply(input: { value: number }): Promise<{ result: number }> { + await setTimeout(1); // simulate some processing time return { result: input.value * 2 }; } } @@ -53,28 +56,34 @@ describe('Decorator: validator', () => { }; // Act & Assess - expect(instance.multiply(invalidInput)).toThrow(SchemaValidationError); + await expect(instance.multiply(invalidInput)).rejects.toThrow( + SchemaValidationError + ); }); - it('should throw error on outbound validation failure', () => { + it('throws an error on outbound validation failure', async () => { // Prepare class TestClassInvalid { @validator({ inboundSchema, outboundSchema }) - multiply(_input: { value: number }) { + async multiply(_input: { value: number }) { + await setTimeout(1); // simulate some processing time return { result: 'invalid' }; } } const instance = new TestClassInvalid(); // Act & Assess - expect(instance.multiply({ value: 5 })).toThrow(SchemaValidationError); + await expect(instance.multiply({ value: 5 })).rejects.toThrow( + SchemaValidationError + ); }); - it('should no-op when no schemas are provided', () => { + it('results in a no-op when no schemas are provided', async () => { // Prepare class TestClassNoOp { @validator({}) - echo(input: unknown): unknown { + async echo(input: unknown): Promise { + await setTimeout(1); // simulate some processing time return input; } } @@ -82,17 +91,18 @@ describe('Decorator: validator', () => { const data = { foo: 'bar' }; // Act - const result = instance.echo(data); + const result = await instance.echo(data); // Assess expect(result).toEqual(data); }); - it('should validate inbound only', () => { + it('validates the inbound schema only', async () => { // Prepare class TestClassInbound { @validator({ inboundSchema }) - process(input: { value: number }): { data: string } { + async process(input: { value: number }): Promise<{ data: string }> { + await setTimeout(1); // simulate some processing time return { data: JSON.stringify(input) }; } } @@ -100,17 +110,18 @@ describe('Decorator: validator', () => { const input = { value: 10 }; // Act - const output = instance.process(input); + const output = await instance.process(input); // Assess expect(output).toEqual({ data: JSON.stringify(input) }); }); - it('should validate outbound only', () => { + it('validates the outbound schema only', async () => { // Prepare class TestClassOutbound { @validator({ outboundSchema }) - process(_input: { text: string }): { result: number } { + async process(_input: { text: string }): Promise<{ result: number }> { + await setTimeout(1); // simulate some processing time return { result: 42 }; } } @@ -118,7 +129,7 @@ describe('Decorator: validator', () => { const input = { text: 'hello' }; // Act - const output = instance.process(input); + const output = await instance.process(input); // Assess expect(output).toEqual({ result: 42 });