jatnnik/brainiac

A schema validation library for PHP, heavily inspired by Zod.

Installs: 7

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 0

Open Issues: 0

pkg:composer/jatnnik/brainiac

v2.0.1 2024-10-30 11:31 UTC

This package is auto-updated.

Last update: 2025-12-29 04:02:13 UTC


README

A PHP schema validation library, heavily inspired by Zod.

Quality of Code

Brainiacs code is fully tested and compliant with PHPStan at max level.

Installation

composer require jatnnik/brainiac

Usage

At the moment, the following data types are supported:

You may either use the parse or safe_parse method. parse will throw an exception when validation fails, while safe_parse will always return an associative array with the following shape, based on validation success:

// On validation success.
$success = [
  "success" => true,
  "data" => "parsed value",
];

// On validation failure.
$failure = [
  "success" => false,
  "error" => "error message",
];

Arrays

<?php

use Jatnnik\Brainiac\Brainiac;

Brainiac::array()->parse([4, 2]);
Brainiac::array()->min(1)->parse([4, 2]);
Brainiac::array()->max(10)->parse([4, 2]);
Brainiac::array()->of(Brainiac::number())->parse([4, 2]);

Booleans

<?php

use Jatnnik\Brainiac\Brainiac;

Brainiac::boolean()->parse(true);

Numbers

<?php

use Jatnnik\Brainiac\Brainiac;

Brainiac::number()->parse(42);
Brainiac::number()->min(1)->parse(42);
Brainiac::number()->max(10)->parse(42);
Brainiac::number()->int()->parse(42);
Brainiac::number()->float()->parse(43.5);
Brainiac::number()->positive()->parse(1);
Brainiac::number()->negative()->parse(-1);
Brainiac::number()->literal(42)->parse(42);

Strings

<?php

use Jatnnik\Brainiac\Brainiac;

Brainiac::string()->parse('foo');
Brainiac::string()->min(1)->parse('foo');
Brainiac::string()->max(10)->parse('foo');
Brainiac::string()->literal('foo')->parse('foo');