PHPackages                             masum/laravel-json-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. [API Development](/categories/api)
4. /
5. masum/laravel-json-schema

ActiveLibrary[API Development](/categories/api)

masum/laravel-json-schema
=========================

A lightweight JSON Schema builder for Laravel with fluent API

v0.0.2(5mo ago)01MITPHPPHP ^8.1

Since Nov 30Pushed 5mo agoCompare

[ Source](https://github.com/MasumNishat/laravel-json-schema)[ Packagist](https://packagist.org/packages/masum/laravel-json-schema)[ RSS](/packages/masum-laravel-json-schema/feed)WikiDiscussions master Synced 1mo ago

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

Laravel JSON Schema
===================

[](#laravel-json-schema)

A lightweight, fluent JSON Schema builder for Laravel with full validation support.

Installation
------------

[](#installation)

```
composer require masum/laravel-json-schema
```

Quick Start
-----------

[](#quick-start)

### Basic Schema Definition

[](#basic-schema-definition)

```
use Masum\JsonSchema\Schema;

$userSchema = Schema::object()
    ->property('id', Schema::string()->format('uuid'))
    ->property('name', Schema::string()->minLength(1)->maxLength(255))
    ->property('email', Schema::string()->format('email'))
    ->property('age', Schema::number()->integer()->minimum(0)->nullable())
    ->required(['id', 'name', 'email']);

// Convert to JSON
$json = $userSchema->toJson();

// Convert to array
$array = $userSchema->toArray();
```

Validation
----------

[](#validation)

### Direct Validation

[](#direct-validation)

```
$data = [
    'id' => '550e8400-e29b-41d4-a716-446655440000',
    'name' => 'John Doe',
    'email' => 'john@example.com'
];

$result = Schema::validate($data, $userSchema);

if ($result['valid']) {
    // Data is valid
} else {
    // Handle errors
    $errors = $result['errors'];
}
```

### Laravel Validation Rule

[](#laravel-validation-rule)

```
use Illuminate\Http\Request;
use Masum\JsonSchema\Schema;

public function store(Request $request)
{
    $request->validate([
        'user_data' => [Schema::rule($userSchema)],
    ]);

    // Data is valid
}
```

### Form Request Integration

[](#form-request-integration)

```
use Masum\JsonSchema\Helpers;
use Illuminate\Foundation\Http\FormRequest; // Assuming FormRequest is used

class StoreUserRequest extends FormRequest
{
    public function rules()
    {
        return Helpers::createValidationRules($userSchema);
    }
}
```

Advanced Usage
--------------

[](#advanced-usage)

### Compound Schemas

[](#compound-schemas)

```
$schema = Schema::compound()
    ->anyOf([
        Schema::string()->minLength(5),
        Schema::number()->minimum(10)
    ]);
```

### Array Validation

[](#array-validation)

```
$schema = Schema::array()
    ->items(Schema::object()
        ->property('id', Schema::string())
        ->property('value', Schema::number())
    )
    ->minItems(1)
    ->maxItems(10)
    ->uniqueItems(true);
```

### Custom Properties

[](#custom-properties)

```
$schema = Schema::object()
    ->property('id', Schema::string())
    ->custom('$schema', 'http://json-schema.org/draft-07/schema#')
    ->custom('x-custom-property', 'custom-value');
```

Artisan Commands
----------------

[](#artisan-commands)

### Create a New Schema

[](#create-a-new-schema)

```
php artisan make:schema UserSchema
```

### Create Example Schema

[](#create-example-schema)

```
php artisan make:schema UserSchema --example
```

### Validate Data

[](#validate-data)

```
# From argument
php artisan schema:validate UserSchema '{"name": "John", "email": "john@example.com"}'

# From file
php artisan schema:validate UserSchema --file=user-data.json

# From stdin
echo '{"name": "John"}' | php artisan schema:validate UserSchema
```

Configuration
-------------

[](#configuration)

Publish the configuration file:

```
php artisan vendor:publish --provider="Masum\JsonSchema\SchemaServiceProvider" --tag="json-schema-config"
```

Testing
-------

[](#testing)

```
composer test
```

Features
--------

[](#features)

- ✅ Fluent, expressive API
- ✅ Full JSON Schema Draft-07 support
- ✅ Laravel validation integration
- ✅ Compound types (anyOf, oneOf, allOf, not)
- ✅ Custom properties and extensions
- ✅ Artisan commands for schema management
- ✅ Comprehensive validation with detailed errors
- ✅ Nullable field support
- ✅ Format validation (email, URL, UUID, dates)
- ✅ Array and object validation
- ✅ Configuration-driven behavior

License
-------

[](#license)

This package is open-source software licensed under the MIT license.

Example Schemas
---------------

[](#example-schemas)

### Create `schemas/ExampleUserSchema.php`

[](#create-schemasexampleuserschemaphp)

```
