|
| 1 | +import { assertEquals, assertThrows } from '@std/assert' |
| 2 | +import * as Core from '@core/index.ts' |
| 3 | +import * as Middleware from '@middleware/index.ts' |
| 4 | +import * as Validation from '@validation/index.ts' |
| 5 | +import { Define } from '@neabyte/typebox' |
| 6 | + |
| 7 | +function createTestContext(url = 'http://localhost/', requestInit?: RequestInit): Core.Context { |
| 8 | + const request = new Request(url, requestInit) |
| 9 | + return new Core.Context(request, new URL(url), {}) |
| 10 | +} |
| 11 | + |
| 12 | +function jsonRequest(body: unknown, url = 'http://localhost/'): Core.Context { |
| 13 | + return createTestContext(url, { |
| 14 | + method: 'POST', |
| 15 | + headers: new Headers({ 'Content-Type': 'application/json' }), |
| 16 | + body: JSON.stringify(body) |
| 17 | + }) |
| 18 | +} |
| 19 | + |
| 20 | +Deno.test('Validate contract throw maps to 422 (client input never causes 500)', async () => { |
| 21 | + const schema = { |
| 22 | + json: Define((input: { value: number }) => { |
| 23 | + if (input.value === 0) { |
| 24 | + throw new RangeError('value cannot be zero') |
| 25 | + } |
| 26 | + return input |
| 27 | + }) |
| 28 | + } |
| 29 | + const middleware = Middleware.Mware.validator(schema) |
| 30 | + const ctx = jsonRequest({ value: 0 }) |
| 31 | + const next = async (): Promise<Response> => new Response('ok') |
| 32 | + const res = await middleware(ctx, next) |
| 33 | + assertEquals(res !== undefined, true) |
| 34 | + if (res) { |
| 35 | + assertEquals(res.status, 422) |
| 36 | + } |
| 37 | +}) |
| 38 | + |
| 39 | +Deno.test('Validate empty schema throws at build time', () => { |
| 40 | + assertThrows(() => Middleware.Mware.validator({}), Deno.errors.InvalidData) |
| 41 | +}) |
| 42 | + |
| 43 | +Deno.test('Validate failing guard returns 422 with reasons', async () => { |
| 44 | + const schema = { |
| 45 | + json: Define( |
| 46 | + (input: { age: number }) => input, |
| 47 | + (input) => (input.age >= 18 ? true : 'age must be at least 18') |
| 48 | + ) |
| 49 | + } |
| 50 | + const middleware = Middleware.Mware.validator(schema) |
| 51 | + const ctx = jsonRequest({ age: 12 }) |
| 52 | + const next = async (): Promise<Response> => new Response('ok') |
| 53 | + const res = await middleware(ctx, next) |
| 54 | + assertEquals(res !== undefined, true) |
| 55 | + if (res) { |
| 56 | + assertEquals(res.status, 422) |
| 57 | + } |
| 58 | +}) |
| 59 | + |
| 60 | +Deno.test('Validate multi-source schema validates each part', async () => { |
| 61 | + const schema = { |
| 62 | + json: Define((input: { name: string }) => input), |
| 63 | + query: Define((input: Record<string, string>) => input) |
| 64 | + } |
| 65 | + const middleware = Middleware.Mware.validator(schema) |
| 66 | + const ctx = jsonRequest({ name: 'neo' }, 'http://localhost/?page=1') |
| 67 | + let name = '' |
| 68 | + let page = '' |
| 69 | + const next = async (): Promise<Response> => { |
| 70 | + const { json, query } = Validation.Validator.read<typeof schema>(ctx) |
| 71 | + name = json.name |
| 72 | + page = query['page'] ?? '' |
| 73 | + return new Response('ok') |
| 74 | + } |
| 75 | + await middleware(ctx, next) |
| 76 | + assertEquals(name, 'neo') |
| 77 | + assertEquals(page, '1') |
| 78 | +}) |
| 79 | + |
| 80 | +Deno.test('Validate null JSON body against object guard maps to 422 not 500', async () => { |
| 81 | + const schema = { |
| 82 | + json: Define( |
| 83 | + (body: { name: string }) => ({ name: body.name.trim() }), |
| 84 | + (body) => (typeof body.name === 'string' && body.name.length > 0 ? true : 'name required') |
| 85 | + ) |
| 86 | + } |
| 87 | + const middleware = Middleware.Mware.validator(schema) |
| 88 | + const ctx = jsonRequest(null) |
| 89 | + const next = async (): Promise<Response> => new Response('ok') |
| 90 | + const res = await middleware(ctx, next) |
| 91 | + assertEquals(res !== undefined, true) |
| 92 | + if (res) { |
| 93 | + assertEquals(res.status, 422) |
| 94 | + } |
| 95 | +}) |
| 96 | + |
| 97 | +Deno.test('Validate passing guard stores typed data on context', async () => { |
| 98 | + const schema = { |
| 99 | + json: Define( |
| 100 | + (input: { name: string; age: number }) => input, |
| 101 | + (input) => (input.age >= 18 ? true : 'age must be at least 18') |
| 102 | + ) |
| 103 | + } |
| 104 | + const middleware = Middleware.Mware.validator(schema) |
| 105 | + const ctx = jsonRequest({ name: 'neo', age: 30 }) |
| 106 | + let name = '' |
| 107 | + const next = async (): Promise<Response> => { |
| 108 | + const { json } = Validation.Validator.read<typeof schema>(ctx) |
| 109 | + name = json.name |
| 110 | + return new Response('ok') |
| 111 | + } |
| 112 | + const res = await middleware(ctx, next) |
| 113 | + assertEquals(name, 'neo') |
| 114 | + assertEquals(res !== undefined, true) |
| 115 | + if (res) { |
| 116 | + assertEquals(await res.text(), 'ok') |
| 117 | + } |
| 118 | +}) |
| 119 | + |
| 120 | +Deno.test('Validate stacked validators merge validated sources', async () => { |
| 121 | + const querySchema = { query: Define((input: Record<string, string>) => input) } |
| 122 | + const jsonSchema = { json: Define((input: { name: string }) => input) } |
| 123 | + const ctx = jsonRequest({ name: 'neo' }, 'http://localhost/?id=7') |
| 124 | + const passThrough = async (): Promise<Response> => new Response('ok') |
| 125 | + await Middleware.Mware.validator(querySchema)(ctx, passThrough) |
| 126 | + let queryId = '' |
| 127 | + let jsonName = '' |
| 128 | + await Middleware.Mware.validator(jsonSchema)(ctx, async () => { |
| 129 | + const merged = Validation.Validator.read<typeof querySchema & typeof jsonSchema>(ctx) |
| 130 | + queryId = merged.query['id'] ?? '' |
| 131 | + jsonName = merged.json.name |
| 132 | + return new Response('ok') |
| 133 | + }) |
| 134 | + assertEquals(queryId, '7') |
| 135 | + assertEquals(jsonName, 'neo') |
| 136 | +}) |
| 137 | + |
| 138 | +Deno.test('Validator rejects a params source in middleware at registration', () => { |
| 139 | + const paramsSchema = { params: Define((input: Record<string, string>) => input) } |
| 140 | + assertThrows( |
| 141 | + () => Middleware.Mware.validator(paramsSchema), |
| 142 | + Deno.errors.InvalidData, |
| 143 | + 'Validator.check(contract, ctx.params())' |
| 144 | + ) |
| 145 | +}) |
| 146 | + |
| 147 | +Deno.test('Validator throws when no validation ran', () => { |
| 148 | + const ctx = createTestContext() |
| 149 | + assertThrows(() => Validation.Validator.read(ctx), Error) |
| 150 | +}) |
0 commit comments