Skip to content

Validators

Usage

A validator is a callable that accepts a string as input, and returns None on success, or a string on failure. If a string is returned, that string will be used as an error message. For example, the following example will validate that the user's input starts with the text "Hello":

def must_say_hello(value):
    if value.lower().startswith("hello"):
        return None
    return "Why didn't you say hello?"

Toga provides built-in validators for a range of common validation types, as well as some base classes that can be used as a starting point for custom validators.

A list of validators can then be provided to any widget that performs validation, such as the TextInput widget. In the following example, a TextInput will validate that the user has entered text that starts with "hello", and has provided at least 10 characters of input:

import toga
from toga.validators import MinLength

widget = toga.TextInput(validators=[
    must_say_hello,
    MinLength(10)
])

Whenever the input changes, all validators will be evaluated in the order they have been specified. The first validator to fail will put the widget into an "error" state, and the error message returned by that validator will be displayed to the user.

Reference

Source code in core/src/toga/validators.py
 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
class BooleanValidator:
    def __init__(self, error_message: str, allow_empty: bool = True):
        """An abstract base class for defining a simple validator.

        Subclasses should implement the `is_valid()` method

        :param error_message: The error to display to the user when the input isn't
            valid.
        :param allow_empty: Optional; Is no input considered valid? Defaults to `True`
        """
        self.error_message = error_message
        self.allow_empty = allow_empty

    def __call__(self, input_string: str) -> str | None:
        if self.allow_empty and input_string == "":
            return None
        return None if self.is_valid(input_string) else self.error_message

    @abstractmethod
    def is_valid(self, input_string: str) -> bool:
        """Is the input string valid?

        :param input_string: The string to validate.
        :returns: `True` if the input is valid.
        """

__init__(error_message, allow_empty=True)

An abstract base class for defining a simple validator.

Subclasses should implement the is_valid() method

:param error_message: The error to display to the user when the input isn't valid. :param allow_empty: Optional; Is no input considered valid? Defaults to True

Source code in core/src/toga/validators.py
 8
 9
10
11
12
13
14
15
16
17
18
def __init__(self, error_message: str, allow_empty: bool = True):
    """An abstract base class for defining a simple validator.

    Subclasses should implement the `is_valid()` method

    :param error_message: The error to display to the user when the input isn't
        valid.
    :param allow_empty: Optional; Is no input considered valid? Defaults to `True`
    """
    self.error_message = error_message
    self.allow_empty = allow_empty

is_valid(input_string) abstractmethod

Is the input string valid?

:param input_string: The string to validate. :returns: True if the input is valid.

Source code in core/src/toga/validators.py
25
26
27
28
29
30
31
@abstractmethod
def is_valid(self, input_string: str) -> bool:
    """Is the input string valid?

    :param input_string: The string to validate.
    :returns: `True` if the input is valid.
    """
Source code in core/src/toga/validators.py
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
class CountValidator:
    def __init__(
        self,
        count: int | None,
        expected_existence_message: str,
        expected_non_existence_message: str,
        expected_count_message: str,
        allow_empty: bool = True,
    ):
        """An abstract base class for validators that are based on counting
        instances of some content in the overall content.

        Subclasses should implement the `count()` method to identify the content of
        interest.

        :param count: Optional; The expected count.
        :param expected_existence_message: The error message to show if matches are
            expected, but were not found.
        :param expected_non_existence_message: The error message to show if matches were
            not expected, but were found.
        :param expected_count_message: The error message to show if matches were
            expected, but a different number were found.
        :param allow_empty: Optional; Is no input considered valid? Defaults to `True`
        """
        self.expected_count = count
        self.expected_existence_message = expected_existence_message
        self.expected_non_existence_message = expected_non_existence_message
        self.expected_count_message = expected_count_message
        self.allow_empty = allow_empty

    def __call__(self, input_string: str) -> str | None:
        if self.allow_empty and input_string == "":
            return None
        actual_count = self.count(input_string)
        if actual_count == 0 and self.expected_count != 0:
            return self.expected_existence_message
        if actual_count != 0 and self.expected_count == 0:
            return self.expected_non_existence_message
        if self.expected_count is not None and actual_count != self.expected_count:
            return self.expected_count_message
        return None

    @abstractmethod
    def count(self, input_string: str) -> int:
        """Count the instances of content of interest in the input string.

        :param input_string: The string to inspect for content of interest.
        :returns: The number of instances of content that the validator is looking for.
        """

__init__(count, expected_existence_message, expected_non_existence_message, expected_count_message, allow_empty=True)

An abstract base class for validators that are based on counting instances of some content in the overall content.

Subclasses should implement the count() method to identify the content of interest.

:param count: Optional; The expected count. :param expected_existence_message: The error message to show if matches are expected, but were not found. :param expected_non_existence_message: The error message to show if matches were not expected, but were found. :param expected_count_message: The error message to show if matches were expected, but a different number were found. :param allow_empty: Optional; Is no input considered valid? Defaults to True

Source code in core/src/toga/validators.py
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
def __init__(
    self,
    count: int | None,
    expected_existence_message: str,
    expected_non_existence_message: str,
    expected_count_message: str,
    allow_empty: bool = True,
):
    """An abstract base class for validators that are based on counting
    instances of some content in the overall content.

    Subclasses should implement the `count()` method to identify the content of
    interest.

    :param count: Optional; The expected count.
    :param expected_existence_message: The error message to show if matches are
        expected, but were not found.
    :param expected_non_existence_message: The error message to show if matches were
        not expected, but were found.
    :param expected_count_message: The error message to show if matches were
        expected, but a different number were found.
    :param allow_empty: Optional; Is no input considered valid? Defaults to `True`
    """
    self.expected_count = count
    self.expected_existence_message = expected_existence_message
    self.expected_non_existence_message = expected_non_existence_message
    self.expected_count_message = expected_count_message
    self.allow_empty = allow_empty

count(input_string) abstractmethod

Count the instances of content of interest in the input string.

:param input_string: The string to inspect for content of interest. :returns: The number of instances of content that the validator is looking for.

Source code in core/src/toga/validators.py
76
77
78
79
80
81
82
@abstractmethod
def count(self, input_string: str) -> int:
    """Count the instances of content of interest in the input string.

    :param input_string: The string to inspect for content of interest.
    :returns: The number of instances of content that the validator is looking for.
    """

Bases: CountValidator

Source code in core/src/toga/validators.py
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
class Contains(CountValidator):
    def __init__(
        self,
        substring: str,
        count: int | None = None,
        error_message: str | None = None,
        allow_empty: bool = True,
    ):
        """A validator confirming that the string contains one or more
        copies of a substring.

        :param substring: The substring that must exist in the input.
        :param count: Optional; The exact number of matches that are expected.
        :param error_message: Optional; the error message to display when the input
            doesn't contain the substring (or the requested count of substrings).
        :param allow_empty: Optional; Is no input considered valid? Defaults to `True`
        """
        if error_message is not None:
            expected_non_existence_message = error_message
            expected_count_message = error_message
            expected_existence_message = error_message
        else:
            expected_existence_message = f"Input should contain {substring!r}"
            expected_non_existence_message = f"Input should not contain {substring!r}"
            expected_count_message = (
                f"Input should contain {substring!r} exactly {count} times"
            )

        super().__init__(
            count=count,
            expected_existence_message=expected_existence_message,
            expected_non_existence_message=expected_non_existence_message,
            expected_count_message=expected_count_message,
            allow_empty=allow_empty,
        )
        self.substring = substring

    def count(self, input_string: str) -> int:
        return input_string.count(self.substring)

__init__(substring, count=None, error_message=None, allow_empty=True)

A validator confirming that the string contains one or more copies of a substring.

:param substring: The substring that must exist in the input. :param count: Optional; The exact number of matches that are expected. :param error_message: Optional; the error message to display when the input doesn't contain the substring (or the requested count of substrings). :param allow_empty: Optional; Is no input considered valid? Defaults to True

Source code in core/src/toga/validators.py
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
def __init__(
    self,
    substring: str,
    count: int | None = None,
    error_message: str | None = None,
    allow_empty: bool = True,
):
    """A validator confirming that the string contains one or more
    copies of a substring.

    :param substring: The substring that must exist in the input.
    :param count: Optional; The exact number of matches that are expected.
    :param error_message: Optional; the error message to display when the input
        doesn't contain the substring (or the requested count of substrings).
    :param allow_empty: Optional; Is no input considered valid? Defaults to `True`
    """
    if error_message is not None:
        expected_non_existence_message = error_message
        expected_count_message = error_message
        expected_existence_message = error_message
    else:
        expected_existence_message = f"Input should contain {substring!r}"
        expected_non_existence_message = f"Input should not contain {substring!r}"
        expected_count_message = (
            f"Input should contain {substring!r} exactly {count} times"
        )

    super().__init__(
        count=count,
        expected_existence_message=expected_existence_message,
        expected_non_existence_message=expected_non_existence_message,
        expected_count_message=expected_count_message,
        allow_empty=allow_empty,
    )
    self.substring = substring

Bases: CountValidator

Source code in core/src/toga/validators.py
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
class ContainsDigit(CountValidator):
    def __init__(
        self,
        count: int | None = None,
        error_message: str | None = None,
        allow_empty: bool = True,
    ):
        """A validator confirming that the string contains digits.

        :param count: Optional; if provided, the exact count of digits that must exist.
            If not provided, the existence of any digit will make the string valid.
        :param error_message: Optional; the error message to display when the input
            doesn't contain digits (or the requested count of digits).
        :param allow_empty: Optional; Is no input considered valid? Defaults to `True`
        """
        if error_message is not None:
            expected_non_existence_message = error_message
            expected_count_message = error_message
            expected_existence_message = error_message
        else:
            expected_existence_message = "Input should contain at least one digit"
            expected_non_existence_message = "Input should not contain digits"
            expected_count_message = f"Input should contain exactly {count} digits"

        super().__init__(
            count=count,
            expected_existence_message=expected_existence_message,
            expected_non_existence_message=expected_non_existence_message,
            expected_count_message=expected_count_message,
            allow_empty=allow_empty,
        )

    def count(self, input_string: str) -> int:
        return len([char for char in input_string if char.isdigit()])

__init__(count=None, error_message=None, allow_empty=True)

A validator confirming that the string contains digits.

:param count: Optional; if provided, the exact count of digits that must exist. If not provided, the existence of any digit will make the string valid. :param error_message: Optional; the error message to display when the input doesn't contain digits (or the requested count of digits). :param allow_empty: Optional; Is no input considered valid? Defaults to True

Source code in core/src/toga/validators.py
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
def __init__(
    self,
    count: int | None = None,
    error_message: str | None = None,
    allow_empty: bool = True,
):
    """A validator confirming that the string contains digits.

    :param count: Optional; if provided, the exact count of digits that must exist.
        If not provided, the existence of any digit will make the string valid.
    :param error_message: Optional; the error message to display when the input
        doesn't contain digits (or the requested count of digits).
    :param allow_empty: Optional; Is no input considered valid? Defaults to `True`
    """
    if error_message is not None:
        expected_non_existence_message = error_message
        expected_count_message = error_message
        expected_existence_message = error_message
    else:
        expected_existence_message = "Input should contain at least one digit"
        expected_non_existence_message = "Input should not contain digits"
        expected_count_message = f"Input should contain exactly {count} digits"

    super().__init__(
        count=count,
        expected_existence_message=expected_existence_message,
        expected_non_existence_message=expected_non_existence_message,
        expected_count_message=expected_count_message,
        allow_empty=allow_empty,
    )

Bases: CountValidator

Source code in core/src/toga/validators.py
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
class ContainsLowercase(CountValidator):
    def __init__(
        self,
        count: int | None = None,
        error_message: str | None = None,
        allow_empty: bool = True,
    ):
        """A validator confirming that the string contains lower case letters.

        :param count: Optional; if provided, the exact count of lower case letters that
            must exist. If not provided, the existence of any lower case letter will
            make the string valid.
        :param error_message: Optional; the error message to display when the input
            doesn't contain lower case letters (or the requested count of lower case
            letters).
        :param allow_empty: Optional; Is no input considered valid? Defaults to `True`
        """
        if error_message is not None:
            expected_non_existence_message = error_message
            expected_count_message = error_message
            expected_existence_message = error_message
        else:
            expected_existence_message = (
                "Input should contain at least one lower case character"
            )
            expected_non_existence_message = (
                "Input should not contain lower case characters"
            )
            expected_count_message = (
                f"Input should contain exactly {count} lower case characters"
            )

        super().__init__(
            count=count,
            expected_existence_message=expected_existence_message,
            expected_non_existence_message=expected_non_existence_message,
            expected_count_message=expected_count_message,
            allow_empty=allow_empty,
        )

    def count(self, input_string: str) -> int:
        return len([char for char in input_string if char.islower()])

__init__(count=None, error_message=None, allow_empty=True)

A validator confirming that the string contains lower case letters.

:param count: Optional; if provided, the exact count of lower case letters that must exist. If not provided, the existence of any lower case letter will make the string valid. :param error_message: Optional; the error message to display when the input doesn't contain lower case letters (or the requested count of lower case letters). :param allow_empty: Optional; Is no input considered valid? Defaults to True

Source code in core/src/toga/validators.py
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
def __init__(
    self,
    count: int | None = None,
    error_message: str | None = None,
    allow_empty: bool = True,
):
    """A validator confirming that the string contains lower case letters.

    :param count: Optional; if provided, the exact count of lower case letters that
        must exist. If not provided, the existence of any lower case letter will
        make the string valid.
    :param error_message: Optional; the error message to display when the input
        doesn't contain lower case letters (or the requested count of lower case
        letters).
    :param allow_empty: Optional; Is no input considered valid? Defaults to `True`
    """
    if error_message is not None:
        expected_non_existence_message = error_message
        expected_count_message = error_message
        expected_existence_message = error_message
    else:
        expected_existence_message = (
            "Input should contain at least one lower case character"
        )
        expected_non_existence_message = (
            "Input should not contain lower case characters"
        )
        expected_count_message = (
            f"Input should contain exactly {count} lower case characters"
        )

    super().__init__(
        count=count,
        expected_existence_message=expected_existence_message,
        expected_non_existence_message=expected_non_existence_message,
        expected_count_message=expected_count_message,
        allow_empty=allow_empty,
    )

Bases: CountValidator

Source code in core/src/toga/validators.py
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
class ContainsSpecial(CountValidator):
    def __init__(
        self,
        count: int | None = None,
        error_message: str | None = None,
        allow_empty: bool = True,
    ):
        """A validator confirming that the string contains "special" (i.e.,
        non-alphanumeric) characters.

        :param count: Optional; if provided, the exact count of special characters that
            must exist. If not provided, the existence of any special character will
            make the string valid.
        :param error_message: Optional; the error message to display when the input
            doesn't contain special characters (or the requested count of special
            characters).
        :param allow_empty: Optional; Is no input considered valid? Defaults to `True`
        """
        if error_message is not None:
            expected_non_existence_message = error_message
            expected_count_message = error_message
            expected_existence_message = error_message
        else:
            expected_existence_message = (
                "Input should contain at least one special character"
            )
            expected_non_existence_message = (
                "Input should not contain special characters"
            )
            expected_count_message = (
                f"Input should contain exactly {count} special characters"
            )

        super().__init__(
            count=count,
            expected_existence_message=expected_existence_message,
            expected_non_existence_message=expected_non_existence_message,
            expected_count_message=expected_count_message,
            allow_empty=allow_empty,
        )

    def count(self, input_string: str) -> int:
        return len(
            [
                char
                for char in input_string
                if not char.isalpha() and not char.isdigit() and not char.isspace()
            ]
        )

__init__(count=None, error_message=None, allow_empty=True)

A validator confirming that the string contains "special" (i.e., non-alphanumeric) characters.

:param count: Optional; if provided, the exact count of special characters that must exist. If not provided, the existence of any special character will make the string valid. :param error_message: Optional; the error message to display when the input doesn't contain special characters (or the requested count of special characters). :param allow_empty: Optional; Is no input considered valid? Defaults to True

Source code in core/src/toga/validators.py
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
def __init__(
    self,
    count: int | None = None,
    error_message: str | None = None,
    allow_empty: bool = True,
):
    """A validator confirming that the string contains "special" (i.e.,
    non-alphanumeric) characters.

    :param count: Optional; if provided, the exact count of special characters that
        must exist. If not provided, the existence of any special character will
        make the string valid.
    :param error_message: Optional; the error message to display when the input
        doesn't contain special characters (or the requested count of special
        characters).
    :param allow_empty: Optional; Is no input considered valid? Defaults to `True`
    """
    if error_message is not None:
        expected_non_existence_message = error_message
        expected_count_message = error_message
        expected_existence_message = error_message
    else:
        expected_existence_message = (
            "Input should contain at least one special character"
        )
        expected_non_existence_message = (
            "Input should not contain special characters"
        )
        expected_count_message = (
            f"Input should contain exactly {count} special characters"
        )

    super().__init__(
        count=count,
        expected_existence_message=expected_existence_message,
        expected_non_existence_message=expected_non_existence_message,
        expected_count_message=expected_count_message,
        allow_empty=allow_empty,
    )

Bases: CountValidator

Source code in core/src/toga/validators.py
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
class ContainsUppercase(CountValidator):
    def __init__(
        self,
        count: int | None = None,
        error_message: str | None = None,
        allow_empty: bool = True,
    ):
        """A validator confirming that the string contains upper case letters.

        :param count: Optional; if provided, the exact count of upper case letters that
            must exist. If not provided, the existence of any upper case letter will
            make the string valid.
        :param error_message: Optional; the error message to display when the input
            doesn't contain upper case letters (or the requested count of upper case
            letters).
        :param allow_empty: Optional; Is no input considered valid? Defaults to `True`
        """
        if error_message is not None:
            expected_non_existence_message = error_message
            expected_count_message = error_message
            expected_existence_message = error_message
        else:
            expected_existence_message = (
                "Input should contain at least one upper case character"
            )
            expected_non_existence_message = (
                "Input should not contain upper case characters"
            )
            expected_count_message = (
                f"Input should contain exactly {count} upper case characters"
            )

        super().__init__(
            count=count,
            expected_existence_message=expected_existence_message,
            expected_non_existence_message=expected_non_existence_message,
            expected_count_message=expected_count_message,
            allow_empty=allow_empty,
        )

    def count(self, input_string: str) -> int:
        return len([char for char in input_string if char.isupper()])

__init__(count=None, error_message=None, allow_empty=True)

A validator confirming that the string contains upper case letters.

:param count: Optional; if provided, the exact count of upper case letters that must exist. If not provided, the existence of any upper case letter will make the string valid. :param error_message: Optional; the error message to display when the input doesn't contain upper case letters (or the requested count of upper case letters). :param allow_empty: Optional; Is no input considered valid? Defaults to True

Source code in core/src/toga/validators.py
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
def __init__(
    self,
    count: int | None = None,
    error_message: str | None = None,
    allow_empty: bool = True,
):
    """A validator confirming that the string contains upper case letters.

    :param count: Optional; if provided, the exact count of upper case letters that
        must exist. If not provided, the existence of any upper case letter will
        make the string valid.
    :param error_message: Optional; the error message to display when the input
        doesn't contain upper case letters (or the requested count of upper case
        letters).
    :param allow_empty: Optional; Is no input considered valid? Defaults to `True`
    """
    if error_message is not None:
        expected_non_existence_message = error_message
        expected_count_message = error_message
        expected_existence_message = error_message
    else:
        expected_existence_message = (
            "Input should contain at least one upper case character"
        )
        expected_non_existence_message = (
            "Input should not contain upper case characters"
        )
        expected_count_message = (
            f"Input should contain exactly {count} upper case characters"
        )

    super().__init__(
        count=count,
        expected_existence_message=expected_existence_message,
        expected_non_existence_message=expected_non_existence_message,
        expected_count_message=expected_count_message,
        allow_empty=allow_empty,
    )

Bases: MatchRegex

Source code in core/src/toga/validators.py
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
class Email(MatchRegex):
    EMAIL_REGEX = (
        r"^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$"
    )

    def __init__(
        self,
        error_message: str | None = None,
        allow_empty: bool = True,
    ):
        """A validator confirming that the string is an email address.

        /// note | Note

        It's impossible to do *true* RFC-compliant email validation with a regex.
        This validator does a "best effort" validation. It will inevitably allow
        some email addresses that aren't *technically* valid. However, it shouldn't
        *exclude* any valid email addresses.

        ///

        :param error_message: Optional; the error message to display when the input
            isn't a number.
        :param allow_empty: Optional; Is no input considered valid? Defaults to `True`
        """
        if error_message is None:
            error_message = "Input should be a valid email address"
        super().__init__(
            self.EMAIL_REGEX, error_message=error_message, allow_empty=allow_empty
        )

__init__(error_message=None, allow_empty=True)

A validator confirming that the string is an email address.

Note

It's impossible to do true RFC-compliant email validation with a regex. This validator does a "best effort" validation. It will inevitably allow some email addresses that aren't technically valid. However, it shouldn't exclude any valid email addresses.

:param error_message: Optional; the error message to display when the input isn't a number. :param allow_empty: Optional; Is no input considered valid? Defaults to True

Source code in core/src/toga/validators.py
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
def __init__(
    self,
    error_message: str | None = None,
    allow_empty: bool = True,
):
    """A validator confirming that the string is an email address.

    /// note | Note

    It's impossible to do *true* RFC-compliant email validation with a regex.
    This validator does a "best effort" validation. It will inevitably allow
    some email addresses that aren't *technically* valid. However, it shouldn't
    *exclude* any valid email addresses.

    ///

    :param error_message: Optional; the error message to display when the input
        isn't a number.
    :param allow_empty: Optional; Is no input considered valid? Defaults to `True`
    """
    if error_message is None:
        error_message = "Input should be a valid email address"
    super().__init__(
        self.EMAIL_REGEX, error_message=error_message, allow_empty=allow_empty
    )

Bases: BooleanValidator

Source code in core/src/toga/validators.py
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
class EndsWith(BooleanValidator):
    def __init__(
        self,
        substring: str,
        error_message: str | None = None,
        allow_empty: bool = True,
    ):
        """A validator confirming that the string ends with a given substring.

        :param substring: The substring that the input must end with.
        :param error_message: Optional; the error message to display when the string
            doesn't end with the given substring.
        :param allow_empty: Optional; Is no input considered valid? Defaults to `True`
        """
        if error_message is None:
            error_message = f"Input should end with '{substring}'"

        super().__init__(error_message=error_message, allow_empty=allow_empty)
        self.substring = substring

    def is_valid(self, input_string: str) -> bool:
        return input_string.endswith(self.substring)

__init__(substring, error_message=None, allow_empty=True)

A validator confirming that the string ends with a given substring.

:param substring: The substring that the input must end with. :param error_message: Optional; the error message to display when the string doesn't end with the given substring. :param allow_empty: Optional; Is no input considered valid? Defaults to True

Source code in core/src/toga/validators.py
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
def __init__(
    self,
    substring: str,
    error_message: str | None = None,
    allow_empty: bool = True,
):
    """A validator confirming that the string ends with a given substring.

    :param substring: The substring that the input must end with.
    :param error_message: Optional; the error message to display when the string
        doesn't end with the given substring.
    :param allow_empty: Optional; Is no input considered valid? Defaults to `True`
    """
    if error_message is None:
        error_message = f"Input should end with '{substring}'"

    super().__init__(error_message=error_message, allow_empty=allow_empty)
    self.substring = substring

Bases: BooleanValidator

Source code in core/src/toga/validators.py
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
class Integer(BooleanValidator):
    def __init__(
        self,
        error_message: str | None = None,
        allow_empty: bool = True,
    ):
        """A validator confirming that the string is an integer.

        :param error_message: Optional; the error message to display when the input
            isn't an integer.
        :param allow_empty: Optional; Is no input considered valid? Defaults to `True`
        """
        if error_message is None:
            error_message = "Input should be an integer"
        super().__init__(error_message=error_message, allow_empty=allow_empty)

    def is_valid(self, input_string: str) -> bool:
        try:
            int(input_string)
            return True
        except ValueError:
            return False

__init__(error_message=None, allow_empty=True)

A validator confirming that the string is an integer.

:param error_message: Optional; the error message to display when the input isn't an integer. :param allow_empty: Optional; Is no input considered valid? Defaults to True

Source code in core/src/toga/validators.py
481
482
483
484
485
486
487
488
489
490
491
492
493
494
def __init__(
    self,
    error_message: str | None = None,
    allow_empty: bool = True,
):
    """A validator confirming that the string is an integer.

    :param error_message: Optional; the error message to display when the input
        isn't an integer.
    :param allow_empty: Optional; Is no input considered valid? Defaults to `True`
    """
    if error_message is None:
        error_message = "Input should be an integer"
    super().__init__(error_message=error_message, allow_empty=allow_empty)

Bases: BooleanValidator

Source code in core/src/toga/validators.py
 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
class LengthBetween(BooleanValidator):
    def __init__(
        self,
        min_length: int | None,
        max_length: int | None,
        error_message: str | None = None,
        allow_empty: bool = True,
    ):
        """A validator confirming that the length of input falls in a given
        range.

        :param min_length: The minimum length of the string (inclusive).
        :param max_length: The maximum length of the string (inclusive).
        :param error_message: Optional; the error message to display when the length
            isn't in the given range.
        :param allow_empty: Optional; Is no input considered valid? Defaults to `True`
        """
        if error_message is None:
            error_message = (
                f"Input should be between {min_length} and {max_length} characters"
            )
        super().__init__(error_message=error_message, allow_empty=allow_empty)

        if (
            min_length is not None
            and max_length is not None
            and min_length > max_length
        ):
            raise ValueError("Minimum length cannot be less than maximum length")

        self.min_length = min_length
        self.max_length = max_length

    def is_valid(self, input_string: str) -> bool:
        if self.min_length is not None and len(input_string) < self.min_length:
            return False
        if self.max_length is not None and len(input_string) > self.max_length:
            return False

        return True

__init__(min_length, max_length, error_message=None, allow_empty=True)

A validator confirming that the length of input falls in a given range.

:param min_length: The minimum length of the string (inclusive). :param max_length: The maximum length of the string (inclusive). :param error_message: Optional; the error message to display when the length isn't in the given range. :param allow_empty: Optional; Is no input considered valid? Defaults to True

Source code in core/src/toga/validators.py
 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
def __init__(
    self,
    min_length: int | None,
    max_length: int | None,
    error_message: str | None = None,
    allow_empty: bool = True,
):
    """A validator confirming that the length of input falls in a given
    range.

    :param min_length: The minimum length of the string (inclusive).
    :param max_length: The maximum length of the string (inclusive).
    :param error_message: Optional; the error message to display when the length
        isn't in the given range.
    :param allow_empty: Optional; Is no input considered valid? Defaults to `True`
    """
    if error_message is None:
        error_message = (
            f"Input should be between {min_length} and {max_length} characters"
        )
    super().__init__(error_message=error_message, allow_empty=allow_empty)

    if (
        min_length is not None
        and max_length is not None
        and min_length > max_length
    ):
        raise ValueError("Minimum length cannot be less than maximum length")

    self.min_length = min_length
    self.max_length = max_length

Bases: BooleanValidator

Source code in core/src/toga/validators.py
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
class MatchRegex(BooleanValidator):
    def __init__(
        self,
        regex_string: str,
        error_message: str | None = None,
        allow_empty: bool = True,
    ):
        """A validator confirming that the string matches a given regular expression.

        :param regex_string: A regular expression that the input must match.
        :param error_message: Optional; the error message to display when the input
            doesn't match the provided regular expression.
        :param allow_empty: Optional; Is no input considered valid? Defaults to `True`
        """
        if error_message is None:
            error_message = f"Input should match regex: {regex_string!r}"

        super().__init__(error_message=error_message, allow_empty=allow_empty)
        self.regex_string = regex_string

    def is_valid(self, input_string: str) -> bool:
        return bool(re.search(self.regex_string, input_string))

__init__(regex_string, error_message=None, allow_empty=True)

A validator confirming that the string matches a given regular expression.

:param regex_string: A regular expression that the input must match. :param error_message: Optional; the error message to display when the input doesn't match the provided regular expression. :param allow_empty: Optional; Is no input considered valid? Defaults to True

Source code in core/src/toga/validators.py
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
def __init__(
    self,
    regex_string: str,
    error_message: str | None = None,
    allow_empty: bool = True,
):
    """A validator confirming that the string matches a given regular expression.

    :param regex_string: A regular expression that the input must match.
    :param error_message: Optional; the error message to display when the input
        doesn't match the provided regular expression.
    :param allow_empty: Optional; Is no input considered valid? Defaults to `True`
    """
    if error_message is None:
        error_message = f"Input should match regex: {regex_string!r}"

    super().__init__(error_message=error_message, allow_empty=allow_empty)
    self.regex_string = regex_string

Bases: LengthBetween

Source code in core/src/toga/validators.py
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
class MaxLength(LengthBetween):
    def __init__(
        self,
        length: int,
        error_message: str | None = None,
    ):
        """A validator confirming that the length of input does not exceed a maximum
        size.

        :param length: The maximum length of the string (inclusive).
        :param error_message: Optional; the error message to display when the string is
            too long.
        """
        if error_message is None:
            error_message = f"Input is too long (length should be at most {length})"
        super().__init__(
            min_length=None,
            max_length=length,
            error_message=error_message,
        )

__init__(length, error_message=None)

A validator confirming that the length of input does not exceed a maximum size.

:param length: The maximum length of the string (inclusive). :param error_message: Optional; the error message to display when the string is too long.

Source code in core/src/toga/validators.py
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
def __init__(
    self,
    length: int,
    error_message: str | None = None,
):
    """A validator confirming that the length of input does not exceed a maximum
    size.

    :param length: The maximum length of the string (inclusive).
    :param error_message: Optional; the error message to display when the string is
        too long.
    """
    if error_message is None:
        error_message = f"Input is too long (length should be at most {length})"
    super().__init__(
        min_length=None,
        max_length=length,
        error_message=error_message,
    )

Bases: LengthBetween

Source code in core/src/toga/validators.py
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
class MinLength(LengthBetween):
    def __init__(
        self,
        length: int,
        error_message: str | None = None,
        allow_empty: bool = True,
    ):
        """A validator confirming that the length of input exceeds a minimum size.

        :param length: The minimum length of the string (inclusive).
        :param error_message: Optional; the error message to display when the string
            isn't long enough.
        :param allow_empty: Optional; Is no input considered valid? Defaults to `True`
        """
        if error_message is None:
            error_message = f"Input is too short (length should be at least {length})"
        super().__init__(
            min_length=length,
            max_length=None,
            error_message=error_message,
            allow_empty=allow_empty,
        )

__init__(length, error_message=None, allow_empty=True)

A validator confirming that the length of input exceeds a minimum size.

:param length: The minimum length of the string (inclusive). :param error_message: Optional; the error message to display when the string isn't long enough. :param allow_empty: Optional; Is no input considered valid? Defaults to True

Source code in core/src/toga/validators.py
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
def __init__(
    self,
    length: int,
    error_message: str | None = None,
    allow_empty: bool = True,
):
    """A validator confirming that the length of input exceeds a minimum size.

    :param length: The minimum length of the string (inclusive).
    :param error_message: Optional; the error message to display when the string
        isn't long enough.
    :param allow_empty: Optional; Is no input considered valid? Defaults to `True`
    """
    if error_message is None:
        error_message = f"Input is too short (length should be at least {length})"
    super().__init__(
        min_length=length,
        max_length=None,
        error_message=error_message,
        allow_empty=allow_empty,
    )

Bases: Contains

Source code in core/src/toga/validators.py
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
class NotContains(Contains):
    def __init__(
        self,
        substring: str,
        error_message: str | None = None,
    ):
        """A validator confirming that the string does not contain a substring.

        :param substring: A substring that must not exist in the input.
        :param error_message: Optional; the error message to display when the input
            contains the provided substring.
        """
        super().__init__(
            substring,
            count=0,
            error_message=error_message,
        )

__init__(substring, error_message=None)

A validator confirming that the string does not contain a substring.

:param substring: A substring that must not exist in the input. :param error_message: Optional; the error message to display when the input contains the provided substring.

Source code in core/src/toga/validators.py
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
def __init__(
    self,
    substring: str,
    error_message: str | None = None,
):
    """A validator confirming that the string does not contain a substring.

    :param substring: A substring that must not exist in the input.
    :param error_message: Optional; the error message to display when the input
        contains the provided substring.
    """
    super().__init__(
        substring,
        count=0,
        error_message=error_message,
    )

Bases: BooleanValidator

Source code in core/src/toga/validators.py
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
class Number(BooleanValidator):
    def __init__(
        self,
        error_message: str | None = None,
        allow_empty: bool = True,
    ):
        """A validator confirming that the string is a number.

        :param error_message: Optional; the error message to display when the input
            isn't a number.
        :param allow_empty: Optional; Is no input considered valid? Defaults to `True`
        """
        if error_message is None:
            error_message = "Input should be a number"
        super().__init__(error_message=error_message, allow_empty=allow_empty)

    def is_valid(self, input_string: str) -> bool:
        try:
            float(input_string)
            return True
        except ValueError:
            return False

__init__(error_message=None, allow_empty=True)

A validator confirming that the string is a number.

:param error_message: Optional; the error message to display when the input isn't a number. :param allow_empty: Optional; Is no input considered valid? Defaults to True

Source code in core/src/toga/validators.py
505
506
507
508
509
510
511
512
513
514
515
516
517
518
def __init__(
    self,
    error_message: str | None = None,
    allow_empty: bool = True,
):
    """A validator confirming that the string is a number.

    :param error_message: Optional; the error message to display when the input
        isn't a number.
    :param allow_empty: Optional; Is no input considered valid? Defaults to `True`
    """
    if error_message is None:
        error_message = "Input should be a number"
    super().__init__(error_message=error_message, allow_empty=allow_empty)

Bases: BooleanValidator

Source code in core/src/toga/validators.py
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
class StartsWith(BooleanValidator):
    def __init__(
        self,
        substring: str,
        error_message: str | None = None,
        allow_empty: bool = True,
    ):
        """A validator confirming that the input starts with a given substring.

        :param substring: The substring that the input must start with.
        :param error_message: Optional; the error message to display when the string
            doesn't start with the given substring.
        :param allow_empty: Optional; Is no input considered valid? Defaults to `True`
        """
        if error_message is None:
            error_message = f"Input should start with {substring!r}"

        super().__init__(error_message=error_message, allow_empty=allow_empty)
        self.substring = substring

    def is_valid(self, input_string: str) -> bool:
        return input_string.startswith(self.substring)

__init__(substring, error_message=None, allow_empty=True)

A validator confirming that the input starts with a given substring.

:param substring: The substring that the input must start with. :param error_message: Optional; the error message to display when the string doesn't start with the given substring. :param allow_empty: Optional; Is no input considered valid? Defaults to True

Source code in core/src/toga/validators.py
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
def __init__(
    self,
    substring: str,
    error_message: str | None = None,
    allow_empty: bool = True,
):
    """A validator confirming that the input starts with a given substring.

    :param substring: The substring that the input must start with.
    :param error_message: Optional; the error message to display when the string
        doesn't start with the given substring.
    :param allow_empty: Optional; Is no input considered valid? Defaults to `True`
    """
    if error_message is None:
        error_message = f"Input should start with {substring!r}"

    super().__init__(error_message=error_message, allow_empty=allow_empty)
    self.substring = substring