|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import { validator } from '../../src/decorator.js'; |
| 3 | +import { SchemaValidationError } from '../../src/errors.js'; |
| 4 | + |
| 5 | +const inboundSchema = { |
| 6 | + type: 'object', |
| 7 | + properties: { |
| 8 | + value: { type: 'number' }, |
| 9 | + }, |
| 10 | + required: ['value'], |
| 11 | + additionalProperties: false, |
| 12 | +}; |
| 13 | + |
| 14 | +const outboundSchema = { |
| 15 | + type: 'object', |
| 16 | + properties: { |
| 17 | + result: { type: 'number' }, |
| 18 | + }, |
| 19 | + required: ['result'], |
| 20 | + additionalProperties: false, |
| 21 | +}; |
| 22 | + |
| 23 | +describe('validator decorator', () => { |
| 24 | + it('should validate inbound and outbound successfully', async () => { |
| 25 | + // Prepare |
| 26 | + class TestClass { |
| 27 | + @validator({ inboundSchema, outboundSchema }) |
| 28 | + async multiply(input: { value: number }): Promise<{ result: number }> { |
| 29 | + return { result: input.value * 2 }; |
| 30 | + } |
| 31 | + } |
| 32 | + const instance = new TestClass(); |
| 33 | + const input = { value: 5 }; |
| 34 | + // Act |
| 35 | + const output = await instance.multiply(input); |
| 36 | + // Assess |
| 37 | + expect(output).toEqual({ result: 10 }); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should throw error on inbound validation failure', async () => { |
| 41 | + // Prepare |
| 42 | + class TestClass { |
| 43 | + @validator({ inboundSchema, outboundSchema }) |
| 44 | + async multiply(input: { value: number }): Promise<{ result: number }> { |
| 45 | + return { result: input.value * 2 }; |
| 46 | + } |
| 47 | + } |
| 48 | + const instance = new TestClass(); |
| 49 | + const invalidInput = { value: 'not a number' } as unknown as { |
| 50 | + value: number; |
| 51 | + }; |
| 52 | + // Act & Assess |
| 53 | + await expect(instance.multiply(invalidInput)).rejects.toThrow( |
| 54 | + SchemaValidationError |
| 55 | + ); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should throw error on outbound validation failure', async () => { |
| 59 | + // Prepare |
| 60 | + class TestClassInvalid { |
| 61 | + @validator({ inboundSchema, outboundSchema }) |
| 62 | + async multiply(input: { value: number }): Promise<{ result: number }> { |
| 63 | + return { result: 'invalid' } as unknown as { result: number }; |
| 64 | + } |
| 65 | + } |
| 66 | + const instance = new TestClassInvalid(); |
| 67 | + const input = { value: 5 }; |
| 68 | + // Act & Assess |
| 69 | + await expect(instance.multiply(input)).rejects.toThrow( |
| 70 | + SchemaValidationError |
| 71 | + ); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should no-op when no schemas are provided', async () => { |
| 75 | + // Prepare |
| 76 | + class TestClassNoOp { |
| 77 | + @validator({}) |
| 78 | + async echo(input: unknown): Promise<unknown> { |
| 79 | + return input; |
| 80 | + } |
| 81 | + } |
| 82 | + const instance = new TestClassNoOp(); |
| 83 | + const data = { foo: 'bar' }; |
| 84 | + // Act |
| 85 | + const result = await instance.echo(data); |
| 86 | + // Assess |
| 87 | + expect(result).toEqual(data); |
| 88 | + }); |
| 89 | + |
| 90 | + it('should return descriptor unmodified if descriptor.value is undefined', () => { |
| 91 | + // Prepare |
| 92 | + const descriptor: PropertyDescriptor = {}; |
| 93 | + // Act |
| 94 | + const result = validator({ inboundSchema })( |
| 95 | + null as unknown as object, |
| 96 | + 'testMethod', |
| 97 | + descriptor |
| 98 | + ); |
| 99 | + // Assess |
| 100 | + expect(result).toEqual(descriptor); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should validate inbound only', async () => { |
| 104 | + // Prepare |
| 105 | + class TestClassInbound { |
| 106 | + @validator({ inboundSchema }) |
| 107 | + async process(input: { value: number }): Promise<{ data: string }> { |
| 108 | + return { data: JSON.stringify(input) }; |
| 109 | + } |
| 110 | + } |
| 111 | + const instance = new TestClassInbound(); |
| 112 | + const input = { value: 10 }; |
| 113 | + // Act |
| 114 | + const output = await instance.process(input); |
| 115 | + // Assess |
| 116 | + expect(output).toEqual({ data: JSON.stringify(input) }); |
| 117 | + }); |
| 118 | + |
| 119 | + it('should validate outbound only', async () => { |
| 120 | + // Prepare |
| 121 | + class TestClassOutbound { |
| 122 | + @validator({ outboundSchema }) |
| 123 | + async process(input: { text: string }): Promise<{ result: number }> { |
| 124 | + return { result: 42 }; |
| 125 | + } |
| 126 | + } |
| 127 | + const instance = new TestClassOutbound(); |
| 128 | + const input = { text: 'hello' }; |
| 129 | + // Act |
| 130 | + const output = await instance.process(input); |
| 131 | + // Assess |
| 132 | + expect(output).toEqual({ result: 42 }); |
| 133 | + }); |
| 134 | +}); |
0 commit comments