vod/vod is a Laravel package for vod, zod style validation for php.
It currently has 6 GitHub stars and 10 downloads on Packagist (latest version 0.1.0).
Install it with composer require vod/vod.
Discover more Laravel packages by vod
or browse all Laravel packages to compare alternatives.
Last updated
Vod is a powerful PHP library for validating and defining object structures. It provides a fluent API for creating schemas, parsing data, and generating TypeScript definitions and JSON schemas.
You can install Vod via Composer:
composer require vod/vod
composer test
To create a schema, use the v() function to access the Vod API:
use function Vod\Vod\v;
$schema = v()->object([
'name' => v()->string(),
'age' => v()->number()->int(),
'email' => v()->string()->optional(),
]);
You can parse data against your schema:
$data = ['name' => 'John', 'age' => 30];
$result = $schema->parse($data);
Vod supports various types:
v()->string(): String typev()->number(): Number type (can be further specified as int() or float())v()->boolean(): Boolean typev()->array(): Array typev()->object(): Object typev()->enum(): Enum typev()->any(): Any typev()->date(): Date typev()->tuple(): Tuple typev()->union(): Union typev()->anyOf(): Alias of Union typev()->intersection(): Intersection typev()->all(): Alias of Intersection typeMake fields optional:
v()->string()->optional()
Set default values:
v()->string()->default('default value')
Note that default values are only used currently if the field is not provided data AND is optional.
If you are using inside of Laravel, you can use the rules method to add rules to your schema. This relies on the Laravel Validator facade, so will only work if it is available.
v()->string()->rules('email')->parse('not an email') // throws an exception
v()->string()->rules('email')->parse('[email protected]') // returns [email protected]
Add definitions to your schema for reusable components:
$schema = v()->object([
'user' => v()->ref('userSchema'),
'posts' => v()->array(v()->ref('postSchema')),
])
->define('userSchema', v()->object([
'id' => v()->number()->int(),
'name' => v()->string(),
'email' => v()->string(),
]))
->define('postSchema', v()->object([
'id' => v()->number()->int(),
'title' => v()->string(),
'content' => v()->string(),
]));
To add a reference to a defined schema, use v()->ref('schemaName').
Note: Definitions can only be added to a top-level object schema. They are not available for nested objects or other types.
Using definitions can help you create more modular and reusable schemas, especially for complex data structures.
Generate TypeScript definitions:
$typescript = $schema->toTypescript();
Generate JSON schemas:
$jsonSchema = $schema->toJsonSchema();
Add descriptions to your schema for better documentation - this is only used in json schemas currently.
$schema = v()->object([
'name' => v()->string()->description('The user\'s full name'),
'age' => v()->number()->int()->description('The user\'s age in years'),
])->description('User information schema');
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.