0.1.4 • Published 9 months ago

vovk-zod v0.1.4

Weekly downloads
-
License
MIT
Repository
-
Last release
9 months ago

vovk-zod exports vovkZod decorator fabric that validates request body and incoming query string with Zod models.

// /src/models/user/UserController.ts
import { z } from 'zod';
import vovkZod from 'vovk-zod';
import { put, type VovkRequest } from 'vovk';
import UserService from './UserService';

const UpdateUserModel = z.object({
    name: z.string(),
    email: z.string(),
}).strict();

const UpdateUserQueryModel = z.object({
    id: z.string(),
}).strict();

export default class UserController {
    private static userService = UserService;

    @put()
    @vovkZod(UpdateUserModel, UpdateUserQueryModel)
    static updateUser(
        req: VovkRequest<z.infer<typeof UpdateUserModel>, z.infer<typeof UpdateUserQueryModel>>
    ) {
        const { name, email } = await req.json();
        const id = req.nextUrl.searchParams.get('id');

        return this.userService.updateUser(id, { name, email });
    }
}
'use client';
import React from 'react';
import { UserController } from 'vovk-client';

const MyPage = () => {
    useEffect(() => {
        void UserController.updateUser({
            query: { id: '696969' },
            body: { name: 'John Doe', email: 'john@example.com' },
            // optionally, disable client validation for debugging purpose
            disableClientValidation: true, 
        }).then(/* ... */);
    }, []);

    return (
        // ...
    )
}

export default MyPage;

When vovk-zod is installed zodValidateOnClient is enabled by default as validateOnClient config option to validate incoming reqests on the client-side. Please check decorators docs for more info.

Working with FormData

The library doesn't support FormData validation, but you can still validate query by setting body validation to null.

// ...

export default class HelloController {
    @post()
    @vovkZod(null, z.object({ something: z.string() }).strict())
    static postFormData(req: VovkRequest<FormData, { something: string }>) {
        const formData = await req.formData();
        const something = req.nextUrl.searchParams.get('something');

        // ...
    }
}

How it works

The library (as well as Vovk.ts itself) is built thanks to fantastic job made by other people.

  • When @vovkZod is initialised, it converts Zod schemas to JSON Schemas with zod-to-json-schema and makes metadata handler to receive it as client validation object.
  • @vovkZod performs Zod validation on server-side.
  • When clientized controller method gets called zodValidateOnClient performs validation on client-side with Ajv. Since client-side and server-side validation is implemented by different libraries, error messages are going to be different.
1.0.0-draft.4

9 months ago

1.0.0-draft.3

9 months ago

1.0.0-draft.2

9 months ago

1.0.0-draft.1

9 months ago

1.0.0-beta.2

11 months ago

1.0.0-beta.3

11 months ago

1.0.0-beta.4

11 months ago

1.0.0-beta.5

11 months ago

1.0.0-beta.0

11 months ago

1.0.0-beta.1

11 months ago

1.0.0-beta.10

10 months ago

1.0.0-beta.6

10 months ago

0.1.5-beta.2

1 year ago

0.1.5-beta.1

1 year ago

0.1.5-beta.0

1 year ago

0.1.4

1 year ago

0.1.3

1 year ago

0.1.2

1 year ago

0.1.1

1 year ago

0.1.0

1 year ago

0.0.12

2 years ago

0.0.13

2 years ago

0.0.14

2 years ago

0.0.11

2 years ago

0.0.10

2 years ago

0.0.9

2 years ago

0.0.8

2 years ago

0.0.7

2 years ago