@7nohe/laravel-zodgen

中文文档
0.2.2发布于 2024/09/16MITTypeScriptESM + CJS近 30 天下载 6433

The library lets you generate Zod schemas from Laravel FormRequests.

原始使用文档
文档目录11 节
  1. Features
  2. Supported Versions
  3. Installation
  4. Usage
  5. Example
  6. Available options
  7. Inertia Vue Form
  8. Development
  9. Setup example project
  10. Debug
  11. License

Laravel Zodgen

This is a package that generates Zod schemas from Laravel FormRequests. This allows you to share your validation rules between your Laravel backend and your TypeScript frontend.

Features

  • Generate Zod schemas from Laravel FormRequests
  • Support Nested arrays and objects

Supported Versions

This library supports the following versions:

  • Laravel 9.x and 10.x
  • TypeScript 5.0 and above

Installation

$ npm install -D @7nohe/laravel-zodgen

Usage

Edit package.json

{
    "scripts": {
        "zodgen": "laravel-zodgen"
    },
}
$ npm run zodgen

Example

For example, you have the FormRequest class below.

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class StorePostRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, \Illuminate\Contracts\Validation\Rule|array|string>
     */
    public function rules(): array
    {
        return [
            'title' => 'required|string|max:255',
            'body' => 'required|string',
        ];
    }
}

After running the zodgen command, you will get the following output.

export const StorePostRequest = z.object({
    title: z.string().max(255).nonempty(),
    body: z.string().nonempty()
});

Optionally you can use --coercion option to coerce primitive values.

export const StorePostRequest = z.object({
    title: z.coerce.string().max(255).nonempty(),
    body: z.coerce.string().nonempty()
});

Available options

Usage: laravel-zodgen [options]

Generate Zod schemas from Laravel FormRequests

Options:
  -V, --version         output the version number
  -o, --output <value>  Output directory (default: "resources/js")
  --form-request-path   Path for Laravel's FormRequest classes
  --coercion            Coercion for primitives (default: false)
  -h, --help            display help for command

Inertia Vue Form

This library is an extension of Inertia's useForm(), enabling form validation using Zod. You can directly use the code generated from Laravel Zodgen in this library.

Development

Setup example project

$ cd examples/laravel10-app
$ cp .env.example .env
$ docker run --rm \
    -u "$(id -u):$(id -g)" \
    -v "$(pwd):/var/www/html" \
    -w /var/www/html \
    laravelsail/php81-composer:latest \
    composer install --ignore-platform-reqs
$ ./vendor/bin/sail up -d
$ ./vendor/bin/sail php artisan key:generate
$ ./vendor/bin/sail php artisan migrate --seed
$ ./vendor/bin/sail npm install

Debug

$ pnpm install
$ sh debug.sh

License

MIT

版本与发布记录

@7nohe/laravel-zodgen 共发布 10 个版本,首次发布于 2023/04/02;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.2.2latest2024/09/162 年前
0.2.12024/05/262 年前
0.2.02024/05/062 年前
0.1.62024/03/232 年前
0.1.52023/04/303 年前
0.1.42023/04/303 年前

关于 @7nohe/laravel-zodgen 的常见问题

@7nohe/laravel-zodgen 是什么?

The library lets you generate Zod schemas from Laravel FormRequests.

如何安装 @7nohe/laravel-zodgen?

在项目目录运行 npm install @7nohe/laravel-zodgen,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 laravel-zodgen,可以用 npx laravel-zodgen 直接运行。

@7nohe/laravel-zodgen 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@7nohe/laravel-zodgen 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@7nohe/laravel-zodgen 可以商用吗?

@7nohe/laravel-zodgen 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@7nohe/laravel-zodgen 的下载量有多少?

根据 npm 官方下载统计,@7nohe/laravel-zodgen 在 2026/10/05 – 2026/11/03 这 30 天内被下载 6,433 次,日均约 214 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@7nohe/laravel-zodgen 还在维护吗?

最新版本 0.2.2 发布于 2024/09/16(2 年前);至今共发布 10 个版本,其中过去 12 个月发布了 0 个;首次发布于 2023/04/02。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。