|
1 |
| -import { describe, expectTypeOf, test } from 'vitest'; |
| 1 | +import type { StandardSchemaV1 } from '@standard-schema/spec'; |
| 2 | +import { describe, expectTypeOf, test, expect } from 'vitest'; |
2 | 3 | import { z } from 'zod';
|
3 |
| -import { server$ } from './server-functions'; |
| 4 | +import { server$, schema$ } from './server-functions'; |
4 | 5 | import type { RequestEventBase, ValidatorErrorType } from './types';
|
5 | 6 |
|
6 | 7 | describe('types', () => {
|
@@ -161,4 +162,86 @@ describe('types', () => {
|
161 | 162 | someAnyType?: string;
|
162 | 163 | }>();
|
163 | 164 | });
|
| 165 | + |
| 166 | + describe('Standard schema type with zod', () => { |
| 167 | + const Schema = z.object({ |
| 168 | + id: z.string().uuid(), |
| 169 | + username: z.string().min(4), |
| 170 | + password: z |
| 171 | + .string() |
| 172 | + .min(8) |
| 173 | + .regex(/^[a-zA-Z]+$/), |
| 174 | + verified: z.boolean().default(false), |
| 175 | + createdAt: z.date({ coerce: true }).default(new Date()), |
| 176 | + role: z.enum(['user', 'moderator', 'admin']).default('user'), |
| 177 | + }); |
| 178 | + |
| 179 | + test('Test types', () => { |
| 180 | + type ErrorType = ValidatorErrorType< |
| 181 | + StandardSchemaV1.InferOutput<typeof Schema> |
| 182 | + >['fieldErrors']; |
| 183 | + |
| 184 | + type EqualType = { |
| 185 | + id?: string; |
| 186 | + username?: string; |
| 187 | + password?: string; |
| 188 | + verified?: string; |
| 189 | + role?: string; |
| 190 | + }; |
| 191 | + |
| 192 | + expectTypeOf<ErrorType>().toEqualTypeOf<EqualType>(); |
| 193 | + expectTypeOf<ErrorType>().not.toEqualTypeOf<{ |
| 194 | + someAnyType?: string; |
| 195 | + }>(); |
| 196 | + }); |
| 197 | + |
| 198 | + test('Successful validation', async () => { |
| 199 | + const schema = schema$(Schema); |
| 200 | + const date = new Date(); |
| 201 | + const okResult = await schema.validate(undefined as any, { |
| 202 | + id: '9ff695ee-6604-4db5-af25-98a6ac682705', |
| 203 | + username: 'test', |
| 204 | + password: 'testpassword', |
| 205 | + role: 'moderator', |
| 206 | + createdAt: date.toISOString(), |
| 207 | + }); |
| 208 | + |
| 209 | + expect(okResult).toEqual({ |
| 210 | + success: true, |
| 211 | + data: { |
| 212 | + id: '9ff695ee-6604-4db5-af25-98a6ac682705', |
| 213 | + username: 'test', |
| 214 | + password: 'testpassword', |
| 215 | + verified: false, |
| 216 | + createdAt: date, |
| 217 | + role: 'moderator', |
| 218 | + }, |
| 219 | + }); |
| 220 | + }); |
| 221 | + test('Failed validation', async () => { |
| 222 | + const schema = schema$(Schema); |
| 223 | + const failResult = await schema.validate(undefined as any, { |
| 224 | + id: 'invalid-uuid', |
| 225 | + password: 'short1', |
| 226 | + role: 'missing-role', |
| 227 | + date: 'Invalid date', |
| 228 | + }); |
| 229 | + |
| 230 | + expect(failResult).toEqual({ |
| 231 | + success: false, |
| 232 | + status: 400, |
| 233 | + error: { |
| 234 | + formErrors: [], |
| 235 | + fieldErrors: { |
| 236 | + id: ['Invalid uuid'], |
| 237 | + password: ['String must contain at least 8 character(s)', 'Invalid'], |
| 238 | + username: ['Required'], |
| 239 | + role: [ |
| 240 | + "Invalid enum value. Expected 'user' | 'moderator' | 'admin', received 'missing-role'", |
| 241 | + ], |
| 242 | + }, |
| 243 | + }, |
| 244 | + }); |
| 245 | + }); |
| 246 | + }); |
164 | 247 | });
|
0 commit comments