拉拉维尔·佐德根
这是一个生成软件包 佐德语Name laravel表格上的计划 这允许您在 Laravel 后端和 TypeScript 前端之间共享验证规则 .
特征
- 从 Laravel 表格生成 Zod 计划请求
- 支持 Nested 数组和对象
支持的版本
该库支持以下版本:
- Laravel 9.x和10.x (英语)
- 脚本 5.0 及以上类型
安装
$ npm install -D @7nohe/laravel-zodgen
使用量
编辑软件包.json
{
"scripts": {
"zodgen": "laravel-zodgen"
},
}
$ npm run zodgen
示例
例如,下面是表格请求类.
<?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',
];
}
}
运行后 zodgen 命令,您将获得以下输出.
export const StorePostRequest = z.object({
title: z.string().max(255).nonempty(),
body: z.string().nonempty()
});
可以选择使用 --coercion 选项来强制原始值.
export const StorePostRequest = z.object({
title: z.coerce.string().max(255).nonempty(),
body: z.coerce.string().nonempty()
});
可用选项
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的扩展 使用格式 (),使用Zod进行表格验证. 您可以在此库直接使用 Laravel Zodgen 生成的代码 .
发展
设置示例项目
$ 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
调试
$ pnpm install
$ sh debug.sh
许可证
麻省理工学院