Validation

Organizing and executing data validation rules.

Introduction

The Streams platform provides a simple interface to leverage Laravel's own validation. Please make sure you are familiar with basic Laravel validation before proceeding.

Defining Rules

The Streams platform leans heavily on native Laravel validation, streamlines the process, and allows flexibility to adjust this behavior entirely.

Rule Configuration

All Streams rule configurations are defined as an array of rules on the field in which they apply to.

Defining Rules

You may define rules whithin the field configuration:

// streams/contacts.json
{
    "fields": {
        "name": {
            "type": "string",
            "rules": ["required", "max:100"]
        },
        "email": {
            "type": "email",
            "rules": ["unique", "required"]
        },
        "company": {
            "type": "relationship",
            "config": {
                "related": "companies"
            },
            "rules": ["required"]
        }
    }
}

Custom Rules

You may also define custom validation rules for fields.

// streams/contacts.json
{
    "fields": {
        "name": {
            "type": "string",
            "rules": [
                "required",
                "App\\Rules\\Example"
            ]
        }
    }
}

Validating

Being that data validation is a fundamental principle, all validation typically operates around the domain objects.

Entry Validator

You can return a pre-loaded validator instance directly from the entry itself.

use Streams\Core\Support\Facades\Streams;

$entry = Streams::repository('contacts')->find('john-doe');

if ($entry->validator()->passes()) {
    // Yay!
}

Stream Validator

You may also return a validator instance with your own data which can be an entry object or array of data to validate as an entry.

use Streams\Core\Support\Facades\Streams;

$validator = Streams::repository('contacts')->validator([
    'name' => 'John Doe',
    'email' => '[email protected]',
    'company' => 'streams',
]);

if ($entry->validator()->passes()) {
    // Yay!
}