timacdonald/rule-builder
A Laravel package for building custom validation rules with a fluent, object-oriented API. Compose reusable rule objects, keep validation logic clean and testable, and use them directly in form requests or validators.
composer.json before upgrading. v4.0.0 is a major release, so treat it as a breaking change even though the release note is short.composer require <vendor>/<package>:^4.0. If the package ships a service provider or config file, publish it as documented and clear caches (php artisan config:clear, composer dump-autoload).CHANGELOG.md, and the src/Rules directory. The rules are usually small classes implementing Laravel’s ValidationRule contract; reading one or two gives you the intended usage pattern immediately.FormRequest alongside Laravel’s built-in rules:use Vendor\Package\Rules\SomeRule;
public function rules(): array
{
return [
'email' => ['required', 'email', new SomeRule()],
'nickname' => ['sometimes', 'string', 'max:255'], // formerly: ['optional', ...]
];
}
optional rule before writing any new rules.rules() rather than relying on string rule names. This keeps IDE support, constructor arguments, and custom messages explicit.['required', 'string', new SomeRule(), 'max:255']. Use bail when later rules are expensive and should not run after a failure.optional deliberately. The removed rule most likely expressed “validate this field only when it is present.” In v4, map that intent to Laravel’s native rules:
sometimes — run the remaining rules only if the field is present.nullable — allow null while still validating non-null values.exclude — remove the field from validated data entirely.present — require the key to exist, even if its value is empty.optional in rule arrays, new Optional, and Optional::class. Add regression tests around any field that previously used the removed rule.Validator::make() in feature/unit tests. Cover the “field missing,” “field null,” and “field present with invalid value” cases — those are exactly the cases where the removed optional rule mattered.$validator = Validator::make($request->all(), [
'phone' => ['nullable', new PhoneRule()],
]);
message() or expose translation keys, keep overrides in lang/ or pass a third argument to Validator::make() rather than editing vendor files.optional rule is removed in v4.0.0. This is the primary breaking change. Any validation array containing 'optional', new Optional, or Optional::class will fail or behave differently after upgrading. Audit before deploying.optional with nullable. They are not equivalent. If the old rule skipped validation when the key was absent, sometimes is usually the correct replacement. If it allowed explicit null values, use nullable. If it stripped the field from validated output, use exclude.optional can appear as a string rule, a class import, a constructor call, a config array, or a test fixture. Search for optional, Optional, and optional with case-insensitive grep.composer why-not <vendor>/<package> 4.0 if the upgrade will not resolve.passes() method and confirm whether it receives the raw value, the full data array, or the validator instance. Rules implementing DataAwareRule or ValidatorAwareRule behave differently from simple ValidationRule implementations.config:cache can mask changes. Clear config and opcode caches after upgrading.ValidationRule contract or extending the package’s base rule class over modifying vendor code. If the package exposes macros or custom rule registries, use those to keep upgrades painless.optional and which native rule replaced it in each context. Future contributors will otherwise reintroduce the removed rule from old examples or tutorials.How can I help you explore Laravel packages today?