https://github.com/chubbyphp/chubbyphp-parsing
Allows parsing data of various structures, meaning the population and validation of data into a defined structure. For example, converting an API request into a Data Transfer Object (DTO).
https://github.com/chubbyphp/chubbyphp-parsing
chubbyphp dto parsing population validation zod
Last synced: about 2 months ago
JSON representation
Allows parsing data of various structures, meaning the population and validation of data into a defined structure. For example, converting an API request into a Data Transfer Object (DTO).
- Host: GitHub
- URL: https://github.com/chubbyphp/chubbyphp-parsing
- Owner: chubbyphp
- License: mit
- Created: 2024-01-03T19:33:01.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2025-03-12T13:51:35.000Z (3 months ago)
- Last Synced: 2025-03-27T18:55:26.351Z (2 months ago)
- Topics: chubbyphp, dto, parsing, population, validation, zod
- Language: PHP
- Homepage:
- Size: 262 KB
- Stars: 14
- Watchers: 1
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# chubbyphp-parsing
[](https://github.com/chubbyphp/chubbyphp-parsing/actions/workflows/ci.yml)
[](https://coveralls.io/github/chubbyphp/chubbyphp-parsing?branch=master)
[](https://dashboard.stryker-mutator.io/reports/github.com/chubbyphp/chubbyphp-parsing/master)
[](https://packagist.org/packages/chubbyphp/chubbyphp-parsing)
[](https://packagist.org/packages/chubbyphp/chubbyphp-parsing)
[](https://packagist.org/packages/chubbyphp/chubbyphp-parsing)[](https://sonarcloud.io/dashboard?id=chubbyphp_chubbyphp-parsing)
[](https://sonarcloud.io/dashboard?id=chubbyphp_chubbyphp-parsing)
[](https://sonarcloud.io/dashboard?id=chubbyphp_chubbyphp-parsing)
[](https://sonarcloud.io/dashboard?id=chubbyphp_chubbyphp-parsing)
[](https://sonarcloud.io/dashboard?id=chubbyphp_chubbyphp-parsing)
[](https://sonarcloud.io/dashboard?id=chubbyphp_chubbyphp-parsing)
[](https://sonarcloud.io/dashboard?id=chubbyphp_chubbyphp-parsing)
[](https://sonarcloud.io/dashboard?id=chubbyphp_chubbyphp-parsing)
[](https://sonarcloud.io/dashboard?id=chubbyphp_chubbyphp-parsing)
[](https://sonarcloud.io/dashboard?id=chubbyphp_chubbyphp-parsing)
[](https://sonarcloud.io/dashboard?id=chubbyphp_chubbyphp-parsing)## Description
Allows parsing data of various structures, meaning the population and validation of data into a defined structure. For example, converting an API request into a Data Transfer Object (DTO).
Heavily inspired by the well-known TypeScript library [zod](https://github.com/colinhacks/zod).
## Requirements
* php: ^8.2
## Installation
Through [Composer](http://getcomposer.org) as [chubbyphp/chubbyphp-parsing][1].
```sh
composer require chubbyphp/chubbyphp-parsing "^1.3"
```## Usage
```php
use Chubbyphp\Parsing\Schema\SchemaInterface;/** @var SchemaInterface $schema */
$schema = ...;$schema->nullable();
$schema->preParse(static fn ($input) => $input);
$schema->postParse(static fn (string $output) => $output);
$schema->parse('test');
$schema->safeParse('test');
$schema->catch(static fn (string $output, ParserErrorException $e) => $output);
```### array
```php
use Chubbyphp\Parsing\Parser;$p = new Parser();
$schema = $p->array($p->int());
$data = $schema->parse([1, 2, 3, 4, 5]);
// validations
$schema->length(5);
$schema->minLength(5);
$schema->maxLength(5);
$schema->includes(5);// transformations
$schema->filter(static fn (int $value) => 0 === $value % 2);
$schema->map(static fn (int $value) => $value * 2);
$schema->sort();
$schema->sort(static fn (int $a, int $b) => $b - $a);// conversions
$schema->reduce(static fn (int $sum, int $current) => $sum + $current, 0);
```### backedEnum
```php
use Chubbyphp\Parsing\Parser;enum BackedSuit: string
{
case Hearts = 'H';
case Diamonds = 'D';
case Clubs = 'C';
case Spades = 'S';
}$p = new Parser();
$schema = $p->backedEnum(BackedSuit::class);
$data = $schema->parse('D');
// validations
// transformations
// conversions
$schema->toInt();
$schema->toString();
```### bool
```php
use Chubbyphp\Parsing\Parser;$p = new Parser();
$schema = $p->bool();
$data = $schema->parse(true);
// validations
// transformations
// conversions
$schema->toInt();
$schema->toString();
```### dateTime
```php
use Chubbyphp\Parsing\Parser;$p = new Parser();
$schema = $p->dateTime();
$data = $schema->parse(new \DateTimeImmutable('2024-01-20T09:15:00+00:00'));
// validations
$schema->from(new \DateTimeImmutable('2024-01-20T09:15:00+00:00'));
$schema->to(new \DateTimeImmutable('2024-01-20T09:15:00+00:00'));// transformations
// conversions
$schema->toInt();
$schema->toString();
```### discriminatedUnion
```php
use Chubbyphp\Parsing\Parser;$p = new Parser();
$schema = $p->discriminatedUnion([
$p->object(['_type' => $p->literal('email'), 'address' => $p->string()]),
$p->object(['_type' => $p->literal('phone'), 'number' => $p->string()]),
]);$data = $schema->parse(['_type' => 'phone', 'number' => '+41790000000']);
```### float
```php
use Chubbyphp\Parsing\Parser;$p = new Parser();
$schema = $p->float();
$data = $schema->parse(4.2);
// validations
$schema->gt(5.0);
$schema->gte(5.0);
$schema->lt(5.0);
$schema->lte(5.0);
$schema->positive();
$schema->nonNegative();
$schema->negative();
$schema->nonPositive();// transformations
// conversions
$schema->toInt();
$schema->toString();
```### int
```php
use Chubbyphp\Parsing\Parser;$p = new Parser();
$schema = $p->int();
$data = $schema->parse(1337);
// validations
$schema->gt(5);
$schema->gte(5);
$schema->lt(5);
$schema->lte(5);
$schema->positive();
$schema->nonNegative();
$schema->negative();
$schema->nonPositive();// transformations
// conversions
$schema->toDateTime();
$schema->toFloat();
$schema->toString();
```### lazy
```php
use Chubbyphp\Parsing\Parser;$p = new Parser();
$schema = $p->lazy(static function () use ($p, &$schema) {
return $p->object([
'name' => $p->string(),
'child' => $schema,
])->nullable();
});$data = $schema->parse([
'name' => 'name1',
'child' => [
'name' => 'name2',
'child' => null
],
]);
```### literal
```php
use Chubbyphp\Parsing\Parser;$p = new Parser();
$schema = $p->literal('email'); // supports string|float|int|bool
$data = $schema->parse('email');
```### object
```php
use Chubbyphp\Parsing\Parser;$p = new Parser();
$schema = $p->object(['name' => $p->string()]);
// stdClass object
$data = $schema->parse(['name' => 'example']);// SampleClass object
$data = $schema->parse(['name' => 'example'], SampleNamespace\SampleClass::class);// if the key 'name' does not exist on input, it won't exists on the output
$schema->optional(['name']);// validations
$schema->strict();
$schema->strict(['_id']); // strip _id if given, but complain about any other additional field// transformations
// conversions
```### record
```php
use Chubbyphp\Parsing\Parser;$p = new Parser();
$schema = $p->record($p->string());
$data = $schema->parse([
'key1' => 'value1',
'key2' => 'value2'
]);
```### respectValidation
```sh
composer require respect/validation "^2.4"
``````php
use Chubbyphp\Parsing\Parser;
use Respect\Validation\Validator as v;$p = new Parser();
$schema = $p->respectValidation(v::numericVal()->positive()->between(1, 255));
$data = $schema->parse(5);
```### string
```php
use Chubbyphp\Parsing\Parser;$p = new Parser();
$schema = $p->string();
$data = $schema->parse('example');
// validations
$schema->length(5);
$schema->minLength(5);
$schema->maxLength(5);
$schema->includes('amp');
$schema->startsWith('exa');
$schema->endsWith('mpl');
$schema->match('/^[a-z]+$/i');
$schema->email();
$schema->ipV4();
$schema->ipV6();
$schema->url();
$schema->uuidV4();
$schema->uuidV5();// transformations
$schema->trim();
$schema->trimStart();
$schema->trimEnd();
$schema->toLowerCase();
$schema->toUpperCase();// conversions
$schema->toDateTime();
$schema->toFloat();
$schema->toInt();// examples
$notBlankSchema = $schema->trim()->minSize(1);
```### tuple
```php
use Chubbyphp\Parsing\Parser;$p = new Parser();
$schema = $p->tuple([$p->float(), $p->float()]);
$data = $schema->parse([47.1, 8.2]);
```### union
```php
use Chubbyphp\Parsing\Parser;$p = new Parser();
$schema = $p->union([$p->string(), $p->int()]);
$data = $schema->parse('42');
```## Copyright
2025 Dominik Zogg
[1]: https://packagist.org/packages/chubbyphp/chubbyphp-parsing