Download the PHP package square/laravel-hyrule without Composer
On this page you can find all versions of the php package square/laravel-hyrule. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download square/laravel-hyrule
More information about square/laravel-hyrule
Files in square/laravel-hyrule
Package laravel-hyrule
Short Description Fluent API for building validation rules in Laravel
License Apache-2.0
Informations about the package laravel-hyrule
Laravel Hyrule
Hyrule provides an object-oriented, fluent API for building validation rules for use w/ Laravel's Validation component. This unlocks patterns that make it easier to define set of rules to enforce complex, nested data structures that is typical in API development.
Why:
Defining validation rules in Laravel involves manually building arrays. As business logic evolves and validation rules become more complex, those arrays grow in size, and building them also becomes more complex. Before long, you find yourself manipulating arrays: adding or removing rules based on conditions, refactor segments to be re-used, etc. and over time, this pattern can feel really clunky. It doesn't take a lot to make managing validation rule definitions feel like it's getting out of control. This library aims to fix that by offers a better API that helps you for the long-term:
- Fluent API that allows you to define rules ergonomically e.g. add conditionals with ease, no more error-prone array manipulations.
- Composable: Simplifies rule-building logic that can be reused multiple times, at multiple nesting levels. No more passing down & reconstructing dot-notated prefixes.
- Strictness means less surprises: Promote enforcement of data-types, and reject unknown fields by default.
Installation
For PHP 7.4 support, install the
1.*
versions. See 1.x README
Setup
1.) If you do not have package discovery enabled, you will have to manually register the service provider:
2.) Publish the config:
Using the service provider & the default config will allow your app to use
StrictValidator
.
API Basics
Initializing a rule-builder and adding your first field:
Fleshing out the rest of your fields & their rules:
Start validating!
Fields API
Hyrule forces you to define the expected data-type for each field. It supports all ranges of types, from scalar types to non-scalar types.
Scalar Types
Adding scalar fields are as easy as:
Non-Scalar Types
No matter how deep and complex your validation rules go, you can use the same set of APIs:
Objects
Use ->object(...)
to start defining nested fields e.g.
Unknown fields
By default, Hyrule helps you build robust & secure applications by only allowing fields you explicitly defined via the Fields API. This is specifically designed to help you be intentional w/ what you expect from your data. For example, this is another mechanism by which your API can further sanitize user input.
If you expect a field to come through, the library would still require you to specify the data-type. But you don't have to specify other rules:
If you'd like to allow unknown fields through, use this method on the appropriate node(s):
Arrays of scalar values
You guessed it: Start with ->array()
:
As you can see in this example, Hyrule promotes strictness even for what goes in arays.
Arrays of objects
Just define it like any other array field, and use the exact same API to define the nested fields:
File Uploads
As of 2.3, Hyrule supports specifying rules for file uploads:
See the following detailed guides on how to validate file uploads by file-type (MIME type), dimensions, etc.
- File Upload Validation - Images
- File Upload Validation - Other MIME Types
Rules API
First let's talk about what happens when you use the Fields API described above. When you define a field, a node is created & returned by the builder. You can then use the Rules API to add validation rules on a node.
Basic Example
Custom Rules Support
This library helps you build validation rule definitions & does not limit you from using custom rules that doesn't come w/ Laravel:
What's up with ->end()
?
Once you understand that Square\Hyrule\Builder
manages a tree of nodes and that the Fields APIs return child nodes,
all you have to know is that ->end()
returns the parent of the node, and it is the fluent way of traversing back
up the tree:
If you are not a fan of this, you can use the Fields-With API.
Advanced Topics
- Fields-With API
- Apply rules on entire data itself with
StrictValidator
- Reusable builder functions with
->with(...)
- Built-in helper builder functions
Hyrule::if(...)
Hyrule::requiredIf(...)
- Path Expressions with
Hyrule::pathExp()
- Custom validation for shape of an object
- Static analysis w/ PHPStan