Download the PHP package gabrielberthier/pieck without Composer
On this page you can find all versions of the php package gabrielberthier/pieck. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download gabrielberthier/pieck
More information about gabrielberthier/pieck
Files in gabrielberthier/pieck
Package pieck
Short Description The PHP validation library that converts data to Plain Old PHP Objects
License MIT
Informations about the package pieck
Pieck
Pieck is a validation library that parses incoming payloads into POPOs (Plain Old PHP Objects). Similar to Pydantic, Zod, Class Validator and other libraries, its objective is to convert raw input to idiomatic user-land objects, providing runtime data validation, parsing, and serialization using PHP types, ensuring data conforms to defined schemas for robust applications, especially in APIs, by catching errors early, simplifying complex data handling (like nested JSON and arrays). Also, it provides features like automatic conversion, custom validation, and JSON Schema generation, making PHP code more reliable and developer-friendly.
Requirements
PHP 8.3+
Features
✅ Validates data via types
✅ Converts raw arrays/objects into plain objects
✅ Support for custom validation rules
✅ Method validation
✅ Collection/Array conversion
✅ To/From JSON
✅ Out of the box converts DateTime interfaces, array types, scalar types.
Installation
How it works
It uses reflection to map entries to object properties.
Also, it is possible to extend DataClass (in a pydantic-link fashion) to use methods as modelValidate, modelValidateJson, toJson and modelDump.
Attributes
Take advantage of attributes to extend properties and validations. Pieck comes with two main Attributes: Field and Validate. Field declares how a property should be treated and Validate declares how to validate a class based on methods.
Field
Target: properties.
Properties:
coerce:- default: true
- if set to true, will attempt to coerce a value into the declared field type
defaultFactory:- default: null
- declares a factory of type ObjectFactoryInterface, that receives a value and converts it to its declarative property type.
rules:- default: []
- Receives a set of rules that must be satisfied in order to pass field validation.
collectionOf:- default: null
- If set, will try to convert the value to a collection of the given type.
- Ex:
#[Field(collectionOf: 'int')]or#[Field(collectionOf: Pieces::class)]
serializationAlias:- default: null
- If set, it will be used when serializing the object via
modelDumpandtoJson.
Validate
Target: methods.
Properties:
field: The field you want to receive as param. Leaving this empty will set the args to receive the entire input passed.message: The message to set case it fails.
Use this to validate the object's own properties, income values and to set behaviours while in validation.
Example:
Validate specific field:
If you raise an AssertionError, it will be converted in an error message:
Custom Rules
Declare custom rules and pass them to Field attribute, just like: