Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# AGENTS.md

This file provides guidance to AI coding agents when working with code in this repository.

## Commands

### Development
```bash
# Install dependencies
composer install

# Run static analysis
composer phpstan

# Generate PHPStan baseline for new violations
composer phpstan:baseline

# Run tests locally (requires vendor directory)
vendor/bin/codecept run unit

# Run specific test
vendor/bin/codecept run unit ValidatorTest.php

# Run tests with SLIC (if available)
slic use validation
slic composer install --ignore-platform-reqs
slic run unit --ext DotReporter
```

## Architecture Overview

### Core Components

1. **Config** (`src/Config.php`): Static configuration manager
- Must call `Config::setServiceContainer()` before use
- Initialize with `Config::initialize()` to register rules

2. **Validator** (`src/Validator.php`): Main validation engine
- Takes rules array, values array, and labels array
- Key methods: `passes()`, `fails()`, `validated()`, `errors()`
- Returns sanitized data via `validated()`

3. **ValidationRulesRegistrar**: Rule registry accessed via service container
- Prevents duplicate rule registration
- Maps rule IDs to class implementations

### Design Patterns

- **Strategy Pattern**: Each rule implements `ValidationRule` interface
- **Command Pattern**: Rules can return `ExcludeValue` or `SkipValidationRules` commands
- **Abstract Factory**: Rules created from strings via `fromString()` method
- **Registry Pattern**: Rules registered in `ValidationRulesRegistrar`
- **Trait**: `HasValidationRules` adds validation to value objects

### Rule System

Rules can be specified as:
- Strings: `'required'`, `'max:255'`, `'email'`
- Instances: `new Max(255)`, `new Required()`
- Closures: Custom validation logic

Built-in rule categories:
- Basic: Required, Optional, Nullable, Exclude
- Type: Boolean, Integer, Numeric, Email, Currency, DateTime
- Comparison: Min, Max, Size, In, InStrict
- Conditional: ExcludeIf, NullableIf, OptionalIf (with Unless variants)

### Key Interfaces

- `ValidationRule`: Core rule interface with `__invoke()` method
- `Sanitizer`: Rules that modify values during validation
- `ValidatesOnFrontEnd`: Rules that can serialize to JSON for client-side validation
- `ConditionalRule`: Abstract base for conditional validation rules

### WordPress Integration

- Uses `__()` for internationalization with `%TEXTDOMAIN%` placeholder
- Hook system via configurable prefix: `{prefix}register_validation_rules`
- Compatible with WordPress coding standards and PHPStan WordPress rules

### Testing

Tests use custom assertions in `TestCase`:
- `assertValidationRulePassed($rule, $value, $values = [])`
- `assertValidationRuleFailed($rule, $value, $values = [])`

Mock rules available for testing validators (e.g., `MockRequiredRule`).

### Extension Points

1. Create custom rules by implementing `ValidationRule`
2. Register rules via WordPress action hook
3. Custom exceptions via `Config::setValidationExceptionClass()`
4. Extend `ConditionalRule` for conditional validation logic
1 change: 1 addition & 0 deletions CLAUDE.md
Comment thread
d4mation marked this conversation as resolved.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
}
],
"require": {
"stellarwp/container-contract": "1.0.4",
"stellarwp/container-contract": "^1.1",
"stellarwp/field-conditions": "^1.0",
"ext-json": "*"
},
Expand Down
18 changes: 9 additions & 9 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading