respect-validation/docs/validators/PropertyExists.md
Henrique Moody 7c681fec66
Fix SPDX headers in all files
I ran the `bin/console spdx --fix` with different strategies for
different files. For most of the core classes, since they've been
drastically rebuilt, I've run it with the `git-blame` strategy, for for
the `src/Validators`, in which the API changed completely but the logic
remains the same, I use the `git-log` strategy.
2026-02-03 15:23:23 +01:00

92 lines
2.8 KiB
Markdown

<!--
SPDX-License-Identifier: MIT
SPDX-FileCopyrightText: (c) Respect Project Contributors
SPDX-FileContributor: Alexandre Gomes Gaigalas <alganet@gmail.com>
SPDX-FileContributor: Henrique Moody <henriquemoody@gmail.com>
-->
# PropertyExists
- `PropertyExists(string $propertyName)`
Validates if an object property exists.
```php
$object = new stdClass;
$object->name = 'The Respect Panda';
$object->email = 'therespectpanda@gmail.com';
v::propertyExists('name')->assert($object);
// Validation passes successfully
v::propertyExists('email')->assert($object);
// Validation passes successfully
v::propertyExists('website')->assert($object);
// → `.website` must be present
```
## Notes
This validator will validate public, private, protected, uninitialised, and static properties.
- To validate a property against a given validator requiring the property to exist, use [Property](Property.md) instead.
- To validate a property against a given validator only if the property exists, use [PropertyOptional](PropertyOptional.md) instead.
## Templates
### `PropertyExists::TEMPLATE_STANDARD`
| Mode | Template |
| ---------: | :------------------------------ |
| `default` | {{subject}} must be present |
| `inverted` | {{subject}} must not be present |
## Template placeholders
| Placeholder | Description |
| ----------- | ---------------------------------------------------------------- |
| `subject` | The validated input or the custom validator name (if specified). |
## Caveats
`PropertyExists` defines the given `$propertyName` as the path, and because it is a standalone validator without children, it's not possible to display a fully custom name with it.
When no custom name is set, the path is displayed as `{{name}}`. When a custom name is set, the validation engine prepends the path to the custom name:
```php
v::propertyExists('foo')->assert([]);
// → `.foo` must be present
v::named('Custom name', v::propertyExists('foo'))->assert([]);
// → `.foo` (<- Custom name) must be present
```
If you want to display only a custom name while checking if a property exists, use [Property](Property.md) with [AlwaysValid](AlwaysValid.md):
```php
v::property('foo', v::named('Custom name', v::alwaysValid()))->assert([]);
// → Custom name must be present
```
## Categorization
- Objects
- Structures
## Changelog
| Version | Description |
| ------: | :----------------------------------- |
| 3.0.0 | Created from [Property](Property.md) |
## See Also
- [AlwaysValid](AlwaysValid.md)
- [Attributes](Attributes.md)
- [Key](Key.md)
- [KeyExists](KeyExists.md)
- [KeyOptional](KeyOptional.md)
- [ObjectType](ObjectType.md)
- [Property](Property.md)
- [PropertyOptional](PropertyOptional.md)