PHPackages                             nyra/zod - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Validation &amp; Sanitization](/categories/validation)
4. /
5. nyra/zod

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

nyra/zod
========

Zod-style schema validation with static type inference

1.0.0(8mo ago)11331MITPHPPHP ^8.2

Since Sep 16Pushed 8mo agoCompare

[ Source](https://github.com/nyra-dev/zod)[ Packagist](https://packagist.org/packages/nyra/zod)[ RSS](/packages/nyra-zod/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (2)Dependencies (1)Versions (3)Used By (0)

Zod for PHP
===========

[](#zod-for-php)

A PHP-first port of [Zod](https://github.com/colinhacks/zod) that mirrors the fluent schema builder and validation pipeline familiar to TypeScript users.

Quickstart
----------

[](#quickstart)

Add the package to your project with Composer:

```
composer require nyra/zod
```

Basic Usage
-----------

[](#basic-usage)

```
use Nyra\Zod\Z;

$schema = Z::object([
    'id' => Z::number()->int()->positive(),
    'email' => Z::string()->email(),
    'roles' => Z::array(Z::string()->nonempty())->nonempty(),
]);

$payload = $schema->parse([
    'id' => 42,
    'email' => 'dev@example.com',
    'roles' => ['admin'],
]);
```

Defining a Schema
-----------------

[](#defining-a-schema)

Schemas are composed with fluent helpers. Every primitive has refinement methods, nullable/optional wrappers, unions, tuples, and objects.

```
use Nyra\Zod\Z;

$userSchema = Z::object([
    'name' => Z::string()->min(2),
    'age' => Z::coerce()->number()->int()->min(0)->default(18),
    'email' => Z::string()->email()->optional(),
    'address' => Z::object([
        'street' => Z::string()->nonempty(),
        'zip' => Z::string()->regex('/^[0-9]{5}$/'),
    ])->passthrough(),
]);
```

Parsing Data
------------

[](#parsing-data)

Call `parse()` to validate and retrieve the typed value. Use `safeParse()` for success/error separation without exceptions.

```
$result = $userSchema->safeParse($input);

if ($result->success) {
    $user = $result->data; // validated payload
} else {
    // handle $result->error
}
```

Pipeline helpers support preprocessing and transformation:

```
$trimmed = Z::string()
    ->preprocess(static fn ($value) => is_string($value) ? trim($value) : $value)
    ->nonempty();

$upper = Z::string()->transform('strtoupper');
```

Handling Errors
---------------

[](#handling-errors)

`parse()` throws `Nyra\Zod\Errors\ZodError`. Each error contains one or more `ZodIssue` entries with codes, messages, and paths.

```
use Nyra\Zod\Errors\ZodError;

try {
    $userSchema->parse($input);
} catch (ZodError $error) {
    foreach ($error->getIssues() as $issue) {
        printf("[%s] %s at %s\n", $issue->code, $issue->message, implode('.', $issue->path));
    }
}
```

Inferring Types
---------------

[](#inferring-types)

The library returns validated PHP values. Combine schema definitions with PHPDoc or static analysis to keep types aligned:

```
/** @var array{name: string, age: int, email?: string|null} $user */
$user = $userSchema->parse($input);
```

When using Psalm or PHPStan, you can declare custom provider extensions that map schema builders to precise array shapes, mirroring how Zod integrates with TypeScript's type inference.

###  Health Score

36

—

LowBetter than 82% of packages

Maintenance62

Regular maintenance activity

Popularity15

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity49

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~3 days

Total

2

Last Release

240d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/03da561a330aa76d1a09096b2ee0c48ee53ca51d62fa0239469d9615b6855733?d=identicon)[ghostzero](/maintainers/ghostzero)

---

Top Contributors

[![ghostzero](https://avatars.githubusercontent.com/u/6547306?v=4)](https://github.com/ghostzero "ghostzero (13 commits)")

---

Tags

phpschemavalidationzod

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/nyra-zod/health.svg)

```
[![Health](https://phpackages.com/badges/nyra-zod/health.svg)](https://phpackages.com/packages/nyra-zod)
```

###  Alternatives

[evaisse/php-json-schema-generator

A JSON Schema Generator.

20298.5k1](/packages/evaisse-php-json-schema-generator)[romegasoftware/laravel-schema-generator

Generate TypeScript Zod validation schemas from Laravel validation rules

288.2k](/packages/romegasoftware-laravel-schema-generator)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
