PHPackages                             aszenz/php-typed-schema - 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. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. aszenz/php-typed-schema

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

aszenz/php-typed-schema
=======================

A php library to parse mixed data into proper types

00[2 PRs](https://github.com/aszenz/php-typed-schema/pulls)PHPCI passing

Since Oct 19Pushed 6mo ago1 watchersCompare

[ Source](https://github.com/aszenz/php-typed-schema)[ Packagist](https://packagist.org/packages/aszenz/php-typed-schema)[ RSS](/packages/aszenz-php-typed-schema/feed)WikiDiscussions main Synced today

READMEChangelogDependenciesVersions (3)Used By (0)

PHP Typed Schema
================

[](#php-typed-schema)

[![ci](https://github.com/aszenz/php-typed-schema/actions/workflows/ci.yml/badge.svg)](https://github.com/aszenz/php-typed-schema/actions/workflows/ci.yml)[![codecov](https://camo.githubusercontent.com/c625893d652b735cda38da398f86f83dd474a41c6643e78fe2cf6b2d1b518eb7/68747470733a2f2f636f6465636f762e696f2f67682f61737a656e7a2f7068702d74797065642d736368656d612f67726170682f62616467652e7376673f746f6b656e3d4f33573451523938524f)](https://codecov.io/gh/aszenz/php-typed-schema)

PHP Typed Schema is a php library to parse unknown data (often as `mixed` type) into properly typed data

Typed Schema has built-in validators's for common use cases and a simple way to compose them to validate any form of data

Install
-------

[](#install)

```
composer require aszenz/php-typed-schema
```

Use cases
---------

[](#use-cases)

### Parsing array into typed dto

[](#parsing-array-into-typed-dto)

Let's say we have a well typed class `Order`

```
/**
 * @psalm-immutable
 */
final class Order
{
    public function __construct(
        public int $id,
        public float $qty,
        public \DateTimeImmutable $date
    ) {
    }
}
```

And we want to create it's object from a mixed data type like an array safely

```
// Assume this array comes from some external data source, and hence it's type is mixed.
/** @var mixed **/
$orderInfo = ['id' => 1, 'quantity' => '123.20', 'order_date' => '02-02-2021'];
```

To convert this array to an object we write a schema for the expected array shape so that we can validate it.

The library provides a function `Decoder::arrayKey('foo', Decoder::int())` which defines an array key `foo` of type `int`.

Essentially we want to validate that the array contains three keys of different value type's.

To represent this schema the library provides several functions like `map2`, `map3`, `map4` that compose different decoders.

The library also provides built-in decoders/validators that parse numeric and date strings into their respective types.

Using them we can define our Order object's decoder/validator

```
// The type of this decoder is Decoder
$orderDecoder = Decoder::map3(
    Decoder::arrayKey('id', Decoder::int()),
    // Notice how array key/value's don't have to match the object properties
    Decoder::arrayKey('quantity', Decoder::numeric()),
    Decoder::arrayKey('order_date', Decoder::dateString('d-m-Y')),
    fn (int $id, float $qty, \DateTimeImmutable $date) => new Order($id, $qty, $date)
);
```

After defining it we can run it on our array and get the dto or an error if the schema didn't match the data.

Running the decoder gives us a `Result` type which can be either an `Ok` or `Error` object, we can unwrap it (throw's exception in case of error) to get our dto.

```
// Psalm/phpstan will correctly infer the  result as Order
$dto = $orderDecoder->run($orderInfo)->unwrap();
```

### Parsing `mixed` array's to list of dto's

[](#parsing-mixed-arrays-to-list-of-dtos)

We can easily convert a list of array's to a list of dto's

Following from our previous example:

```
/** @var mixed */
$ordersInfo = [
    ['id' => 1, 'quantity' => '123.20', 'order_date' => '02-02-2021'],
    ['id' => 2, 'quantity' => '3.20', 'order_date' => '03-04-2021'],
];
```

To convert this into a list of dto's we can reuse our existing decoder defined above:

```
// Psalm/phpstan will correctly infer it as list
$listOfDtos = Decoder::listOf($orderDecoder)->run($ordersInfo)->unwrap();
```

### Parsing json to dto

[](#parsing-json-to-dto)

To validate json safely without any coupling between php objects and json fields, we can define the expected shape of the json and decode it for our use.

```
$jsonData = run($jsonData);

if ($result->isErr()) {
    echo 'Bad json data';
}

$data = $result->unwrap();
```

Comparison with other php validation libraries
----------------------------------------------

[](#comparison-with-other-php-validation-libraries)

Other libraries in the php ecosystem are focused on validation or hydration of data into objects.

Typed schema separates concerns of parsing data and creating objects.

It leaves creating objects (or typed information) to the user, only performing the necessary checks to validate data conforms to the schema.

This approach is explicit and doesn't couple object's properties/constructors with the data source.

Credits
-------

[](#credits)

- Inspired by [Elm's Json Decode library](https://package.elm-lang.org/packages/elm/json/latest/Json.Decode)
- [Parse don't validate](https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-validate/)

###  Health Score

18

—

LowBetter than 8% of packages

Maintenance46

Moderate activity, may be stable

Popularity0

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity17

Early-stage or recently created project

 Bus Factor1

Top contributor holds 92.6% 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.

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/25319264?v=4)[asrar](/maintainers/aszenz)[@aszenz](https://github.com/aszenz)

---

Top Contributors

[![aszenz](https://avatars.githubusercontent.com/u/25319264?v=4)](https://github.com/aszenz "aszenz (50 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (4 commits)")

---

Tags

php-libraryvalidation-library

### Embed Badge

![Health badge](/badges/aszenz-php-typed-schema/health.svg)

```
[![Health](https://phpackages.com/badges/aszenz-php-typed-schema/health.svg)](https://phpackages.com/packages/aszenz-php-typed-schema)
```

###  Alternatives

[mck89/peast

Peast is PHP library that generates AST for JavaScript code

19139.2M47](/packages/mck89-peast)[sauladam/shipment-tracker

Parses tracking information for several carriers, like UPS, USPS, DHL and GLS by simply scraping the data. No need for any kind of API access.

9843.5k](/packages/sauladam-shipment-tracker)[jstewmc/rtf

Read and write Rich Text Format (RTF) documents with PHP

45153.1k6](/packages/jstewmc-rtf)[tcds-io/php-jackson

A lightweight, flexible object serializer for PHP, inspired by FasterXML/jackson

113.2k10](/packages/tcds-io-php-jackson)

PHPackages © 2026

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