A validator that makes an attribute optional. An optional attribute is one which can be set to `None` in addition to satisfying the requirements of the sub-validator. Args: validator (typing.Callable | tuple[typing.Callable] | list[typing.Callable]):
(validator)
| 212 | |
| 213 | |
| 214 | def optional(validator): |
| 215 | """ |
| 216 | A validator that makes an attribute optional. An optional attribute is one |
| 217 | which can be set to `None` in addition to satisfying the requirements of |
| 218 | the sub-validator. |
| 219 | |
| 220 | Args: |
| 221 | validator |
| 222 | (typing.Callable | tuple[typing.Callable] | list[typing.Callable]): |
| 223 | A validator (or validators) that is used for non-`None` values. |
| 224 | |
| 225 | .. versionadded:: 15.1.0 |
| 226 | .. versionchanged:: 17.1.0 *validator* can be a list of validators. |
| 227 | .. versionchanged:: 23.1.0 *validator* can also be a tuple of validators. |
| 228 | """ |
| 229 | if isinstance(validator, (list, tuple)): |
| 230 | return _OptionalValidator(_AndValidator(validator)) |
| 231 | |
| 232 | return _OptionalValidator(validator) |
| 233 | |
| 234 | |
| 235 | @attrs(repr=False, slots=True, unsafe_hash=True) |