$ show dwightwatson/validating
Validating is a Laravel package that provides a trait for Eloquent models to automatically validate model attributes before saving. It integrates with Laravel's event system via an observer to trigger validation on model saving and restoring events. The package handles unique rule injection by automatically appending the model's ID to unique validation rules, supports custom validation messages, and provides methods to check validity, retrieve errors, and save with various validation behaviors (including exception throwing). The architecture is centered around the ValidatingTrait, which encapsulates validation logic, rule preparation, and validator creation, while delegating event handling to a separate observer class.
The LLM identified 4 files worth highlighting for reuse:
The core validation trait that attaches an observer to Eloquent models to validate before saving. It handles rule preparation (including unique rule injection), custom messages, and provides methods to check validity and save with validation options. Demonstrates skill in handling Laravel's validation system and model lifecycle events.
A trait that prepares unique validation rules by automatically adding the table name, connection, model ID, and key name. It also handles additional WHERE clauses and NULL values correctly. Demonstrates non-obvious skill in parsing and reconstructing Laravel's unique rule syntax.
An Eloquent observer that listens to saving and restoring events to trigger model validation. It fires events to allow cancellation and handles validation results by either throwing exceptions or returning booleans based on model configuration. Demonstrates clean separation of concerns by decoupling validation triggering from the model trait.
A simple extension of Laravel's ValidationException that adds a method to retrieve the model that failed validation. This allows controllers or exception handlers to access the model when dealing with validation failures. Demonstrates a small but useful enhancement to Laravel's built-in exception.