Skip to Content
Arktype

Arktype validation example

Form input handling and Arktype validation. The request input is validated on the client-side before being sent to the server where it is validated again.

Result

Code

UserArktypeController.ts
ArktypeFormExample.tsx
1import { prefix, post, openapi, type VovkOutput, createStandardValidation, KnownAny } from 'vovk';
2import { type } from 'arktype';
3
4const withArk = createStandardValidation({
5 toJSONSchema: (model: type) => model.toJsonSchema(),
6});
7
8@prefix('users-arktype')
9export default class UserArktypeController {
10 @openapi({
11 summary: 'Update user (Arktype)',
12 description: 'Update user by ID with Arktype validation',
13 })
14 @post('{id}')
15 static updateUser = withArk({
16 body: type({
17 name: type('string').describe('User full name'),
18 age: type('0 < number < 120').describe('User age'),
19 email: type('string.email').describe('User email'),
20 }),
21 params: type({
22 id: type('string.uuid').describe('User ID'),
23 }),
24 query: type({
25 notify: type('"email" | "push" | "none"').describe('Notification type'),
26 }),
27 output: type({
28 success: type('boolean').describe('Success status'),
29 }),
30 async handle(req, { id }) {
31 const { name, age } = await req.json();
32 const notify = req.nextUrl.searchParams.get('notify');
33
34 // do something with the data
35 console.log(`Updating user ${id}:`, { name, age, notify });
36 return {
37 success: true,
38 } satisfies VovkOutput<typeof UserArktypeController.updateUser>;
39 },
40 });
41}
1import { prefix, post, openapi, type VovkOutput, createStandardValidation, KnownAny } from 'vovk';
2import { type } from 'arktype';
3
4const withArk = createStandardValidation({
5 toJSONSchema: (model: type) => model.toJsonSchema(),
6});
7
8@prefix('users-arktype')
9export default class UserArktypeController {
10 @openapi({
11 summary: 'Update user (Arktype)',
12 description: 'Update user by ID with Arktype validation',
13 })
14 @post('{id}')
15 static updateUser = withArk({
16 body: type({
17 name: type('string').describe('User full name'),
18 age: type('0 < number < 120').describe('User age'),
19 email: type('string.email').describe('User email'),
20 }),
21 params: type({
22 id: type('string.uuid').describe('User ID'),
23 }),
24 query: type({
25 notify: type('"email" | "push" | "none"').describe('Notification type'),
26 }),
27 output: type({
28 success: type('boolean').describe('Success status'),
29 }),
30 async handle(req, { id }) {
31 const { name, age } = await req.json();
32 const notify = req.nextUrl.searchParams.get('notify');
33
34 // do something with the data
35 console.log(`Updating user ${id}:`, { name, age, notify });
36 return {
37 success: true,
38 } satisfies VovkOutput<typeof UserArktypeController.updateUser>;
39 },
40 });
41}
Last updated on