> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/spatie/laravel-data/llms.txt
> Use this file to discover all available pages before exploring further.

# Creating a Rule Inferrer

> Build custom rule inferrers to automatically infer validation rules for data properties

Rule inferrers automatically generate validation rules for properties within a data object.

## RuleInferrer Interface

Implement the `RuleInferrer` interface:

```php theme={null}
interface RuleInferrer
{
    public function handle(DataProperty $property, PropertyRules $rules, ValidationContext $context): PropertyRules;
}
```

### Parameters

* **property** - `DataProperty` object representing the property ([read more](/advanced-usage/internal-structures))
* **rules** - `PropertyRules` collection of previously inferred rules
* **context** - `ValidationContext` containing:
  * `payload` - Current payload for the data object being validated
  * `fullPayload` - Full payload being validated
  * `validationPath` - Path from full payload to current payload

## Working with PropertyRules

### Adding Rules

```php theme={null}
$rules->add(new Min(42));
```

### Automatic Rule Replacement

When adding a rule of the same type, the previous version is removed:

```php theme={null}
$rules->add(new Min(42));
$rules->add(new Min(314)); 

$rules->all(); // [new Min(314)]
```

### Adding String Rules

```php theme={null}
$rules->add(new Rule('min:42'));
```

### Checking for Rule Types

```php theme={null}
$rules->hasType(Min::class);
```

### Removing Rules

```php theme={null}
$rules->removeType(Min::class);
```

## Example Implementation

```php theme={null}
use Spatie\LaravelData\Support\DataProperty;
use Spatie\LaravelData\Support\Validation\PropertyRules;
use Spatie\LaravelData\Support\Validation\ValidationContext;
use Spatie\LaravelData\Support\Validation\RuleInferrer;
use Spatie\LaravelData\Support\Validation\ValidationRule\Min;
use Spatie\LaravelData\Support\Validation\ValidationRule\Max;

class AgeRuleInferrer implements RuleInferrer
{
    public function handle(
        DataProperty $property,
        PropertyRules $rules,
        ValidationContext $context
    ): PropertyRules {
        if ($property->name === 'age') {
            $rules->add(new Min(0));
            $rules->add(new Max(120));
        }

        return $rules;
    }
}
```

## Registration

Rule inferrers must be registered in the `config/data.php` config file:

```php theme={null}
'rule_inferrers' => [
    // Default inferrers...
    App\Data\RuleInferrers\AgeRuleInferrer::class,
],
```

## Return Value

<Note>
  A rule inferrer must always return a `PropertyRules` collection.
</Note>
