1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
|
# Validator
All `InquirerPy` prompts can validate user input and display an error toolbar when the input or selection is invalid.
## Parameters
Each prompt accepts two parameters for validation: [validate](#validate) and [invalid_message](#invalid_message).
Below is an example of ensuring the user doesn't by pass an empty input.
<details>
<summary>Classic Syntax</summary>
```{code-block} python
from InquirerPy import prompt
result = prompt(
[
{
"type": "input",
"message": "Name:",
"validate": lambda result: len(result) > 0,
"invalid_message": "Input cannot be empty.",
}
]
)
```
</details>
<details open>
<summary>Alternate Syntax</summary>
```{code-block} python
from InquirerPy import inquirer
result = inquirer.text(
message="Name:",
validate=lambda result: len(result) > 0,
invalid_message="Input cannot be empty.",
).execute()
```
</details>
Below is another example which ensure that at least 2 options are checked.
<details>
<summary>Classic Syntax</summary>
```{code-block} python
from InquirerPy import prompt
result = prompt(
[
{
"type": "list",
"message": "Select toppings:",
"choices": ["Bacon", "Chicken", "Cheese", "Pineapple"],
"multiselect": True,
"validate": lambda selection: len(selection) >= 2,
"invalid_message": "Select at least 2 toppings.",
}
]
)
```
</details>
<details open>
<summary>Alternate Syntax</summary>
```{code-block} python
from InquirerPy import inquirer
result = inquirer.checkbox(
message="Select toppings:",
choices=["Bacon", "Chicken", "Cheese", "Pineapple"],
validate=lambda selection: len(selection) >= 2,
invalid_message="Select at least 2 toppings.",
).execute()
```
</details>
### validate
```
Union[Callable[[Any], bool], "Validator"]
```
Validation callable or class to validate user input.
#### Callable
```{note}
The `result` provided will vary depending on the prompt types. E.g. `checkbox` prompt will receive a list of checked choices as the result.
```
When providing validate as a {func}`callable`, it will be provided with the current user input and should return a boolean
indicating if the input is valid.
```python
def validator(result) -> bool:
"""Ensure the input is not empty."""
return len(result) > 0
```
#### prompt_toolkit.validation.Validator
```{note}
To maintain API compatibility, for prompts that doesn't have a {class}`string` type result such as `checkbox`, you'll still need to access the result via `document.text`.
```
You can also provide a `prompt_toolkit` {class}`~prompt_toolkit.validation.Validator` instance.
This method removes the need of providing the `invalid_message` parameter.
```python
from prompt_toolkit.validation import ValidationError, Validator
class EmptyInputValidator(Validator):
def validate(self, document):
if not len(document.text) > 0:
raise ValidationError(
message="Input cannot be empty.",
cursor_position=document.cursor_position,
)
```
### invalid_message
```
str
```
The error message you would like to display to user when the input is invalid.
## Pre-built Validators
There's a few pre-built common validator ready to use.
### PathValidator
```{eval-rst}
.. autoclass:: InquirerPy.validator.PathValidator
:noindex:
```
<details>
<summary>Classic Syntax</summary>
```python
from InquirerPy import prompt
from InquirerPy.validator import PathValidator
result = prompt(
[
{
"type": "filepath",
"message": "Enter path:",
"validate": PathValidator("Path is not valid"),
}
]
)
```
</details>
<details open>
<summary>Alternate Syntax</summary>
```python
from InquirerPy import inquirer
from InquirerPy.validator import PathValidator
result = inquirer.filepath(message="Enter path:", validate=PathValidator()).execute()
```
</details>
### EmptyInputValidator
```{eval-rst}
.. autoclass:: InquirerPy.validator.EmptyInputValidator
:noindex:
```
<details>
<summary>Classic Syntax</summary>
```python
from InquirerPy import prompt
from InquirerPy.validator import EmptyInputValidator
result = prompt(
[{"type": "input", "message": "Name:", "validate": EmptyInputValidator()}]
)
```
</details>
<details open>
<summary>Alternate Syntax</summary>
```python
from InquirerPy import inquirer
from InquirerPy.validator import EmptyInputValidator
result = inquirer.text(
message="Name:", validate=EmptyInputValidator("Input should not be empty")
).execute()
```
</details>
### PasswordValidator
```{eval-rst}
.. autoclass:: InquirerPy.validator.PasswordValidator
:noindex:
```
<details>
<summary>Classic Syntax</summary>
```python
from InquirerPy import prompt
from InquirerPy.validator import PasswordValidator
result = prompt(
[
{
"type": "secret",
"message": "New Password:",
"validate": PasswordValidator(
length=8,
cap=True,
special=True,
number=True,
message="Password does not meet compliance",
),
}
]
)
```
</details>
<details open>
<summary>Alternate Syntax</summary>
```python
from InquirerPy import inquirer
from InquirerPy.validator import PasswordValidator
result = inquirer.secret(
message="New Password:",
validate=PasswordValidator(
length=8,
cap=True,
special=True,
number=True,
message="Password does not meet compliance",
),
).execute()
```
</details>
### NumberValidator
```{eval-rst}
.. autoclass:: InquirerPy.validator.NumberValidator
:noindex:
```
<details>
<summary>Classic Syntax</summary>
```python
from InquirerPy import prompt
from InquirerPy.validator import NumberValidator
result = prompt(
[
{
"type": "text",
"message": "Age:",
"validate": NumberValidator(
message="Input should be number", float_allowed=False
),
}
]
)
```
</details>
<details open>
<summary>Alternate Syntax</summary>
```python
from InquirerPy import inquirer
from InquirerPy.validator import NumberValidator
result = inquirer.text(message="Age:", validate=NumberValidator()).execute()
```
</details>
|