Skip to content

Commit 0de3a4b

Browse files
authored
docs: add documentation for validateObject method (#2156)
1 parent 88594df commit 0de3a4b

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

docs/2-features/03-validation.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,30 @@ final class Book
7676
}
7777
```
7878

79+
## Validating an existing object instance
80+
81+
When you already have an instantiated object, you may use the `validateObject()` method. Unlike `validateValuesForClass()`, this method takes an object instance and reads the actual values of its public properties directly.
82+
83+
```php
84+
$config = $this->configRepository->find($id);
85+
86+
$this->validator->validateObject($config);
87+
```
88+
89+
This is particularly useful where an object carries validation rules as part of its invariants, and you want to enforce them after the object has been hydrated from a source.
90+
91+
### Behavior differences from `validateValuesForClass()`
92+
93+
There are a few important differences to be aware of:
94+
95+
**It throws instead of returning.** When validation fails, `validateObject()` throws a {`\Tempest\Validation\Exceptions\ValidationFailed`} exception rather than returning a list of failures.
96+
97+
**Uninitialized properties are skipped.** If a public property has not been initialized on the object, it is silently skipped.
98+
99+
**`#[SkipValidation]` is not respected.** Unlike `validateValuesForClass()`, this method does not check for the {b`#[Tempest\Validation\SkipValidation]`} attribute: all initialized public properties are validated.
100+
101+
**Nested objects are not recursed into.** If a property holds another object, that nested object's properties are not validated.
102+
79103
## Validating against specific rules
80104

81105
If you don't have a model or data transfer object to validate data against, you may alternatively use the `validateValues()` and provide an array of rules.

0 commit comments

Comments
 (0)