Skip to content

Commit 63a7865

Browse files
committed
wip
1 parent b6ff068 commit 63a7865

9 files changed

Lines changed: 82 additions & 18 deletions

File tree

docs/how_to/dictionaries/is_dict.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,10 @@ don't need to initialize it, you can just load :data:`is_dict_validator`.
1818
Valid(val={'a': 1, 'b': None})
1919

2020
>>> is_dict_validator(None)
21-
Invalid(err_type=TypeErr(expected_type=<class 'dict'>), ...)
21+
Invalid(
22+
err_type=TypeErr(expected_type=<class 'dict'>),
23+
value=None,
24+
validator=IsDictValidator()
25+
)
26+
2227

docs/how_to/extension.rst

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@ Here's how our :class:`Validator` can be used:
4444
>>> float_validator(5.5)
4545
Valid(val=5.5)
4646
>>> float_validator(5)
47-
Invalid(err_type=TypeErr(expected_type=<class 'float'>), value=5, ...)
47+
Invalid(
48+
err_type=TypeErr(expected_type=<class 'float'>),
49+
value=5,
50+
validator=<SimpleFloatValidator object at ...>
51+
)
4852

4953
Predicates
5054
----------
@@ -108,7 +112,13 @@ In the code above, if :class:`Predicate<koda_validate.Predicate>` is specified,
108112
>>> validator(3.14)
109113
Valid(val=3.14)
110114
>>> validator(1.1)
111-
Invalid(err_type=PredicateErrs(predicates=[FloatMin(min=2.5)]), value=1.1, ...)
115+
Invalid(
116+
err_type=PredicateErrs(predicates=[
117+
FloatMin(min=2.5),
118+
]),
119+
value=1.1,
120+
validator=<SimpleFloatValidator object at ...>
121+
)
112122

113123
We limited the Validator to one :class:`Predicate` for simplicity. In Koda Validate, :class:`Validator`\s
114124
that accept predicates typically allow of a ``list`` of :class:`Predicate`\s. Because :class:`Predicate`\s

docs/how_to/performance.rst

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,17 @@ The validator should behave as the wrapped :class:`Validator` normally would:
118118
>>> cached_int_validator(5) # cache hit
119119
Valid(val=5)
120120
>>> cached_int_validator("a string") # cache miss
121-
Invalid(err_type=TypeErr(expected_type=<class 'int'>), ...)
121+
Invalid(
122+
err_type=TypeErr(expected_type=<class 'int'>),
123+
value='a string',
124+
validator=IntValidator()
125+
)
122126
>>> cached_int_validator("a string") # cache hit
123-
Invalid(err_type=TypeErr(expected_type=<class 'int'>), ...)
127+
Invalid(
128+
err_type=TypeErr(expected_type=<class 'int'>),
129+
value='a string',
130+
validator=IntValidator()
131+
)
124132

125133
.. note::
126134

docs/how_to/results.rst

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ generic parameter for the same purpose. So, a ``Validator[int]`` will always ret
2525
.. note::
2626

2727
``ValidationResult[int]`` is a more concise way to express ``Union[Valid[int], Invalid]``,
28-
to which it is exactly equivalent.
28+
to which it is exactly equivalent.
2929

3030
Branching on Validity
3131
---------------------
@@ -101,10 +101,14 @@ ValidationResult.map()
101101
Sometimes you might want to convert the data contained by :class:`Valid` into another
102102
type. ``.map`` allows you to do that without a lot of boilerplate:
103103

104+
.. testsetup:: valid-map
105+
106+
from koda_validate import IntValidator
107+
104108
.. doctest:: valid-map
105-
>>> validator = IntValidator()
106-
>>> validator(5).map(str)
107-
Valid(val="5")
109+
110+
>>> IntValidator()(5).map(str)
111+
Valid(val='5')
108112

109113

110114
Working with ``Invalid``

docs/index.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ Easy enough. Let's see how it works:
3535
Valid(val='a string')
3636

3737
>>> my_first_validator(0)
38-
Invalid(err_type=TypeErr(expected_type=<class 'str'>), ...)
38+
Invalid(
39+
err_type=TypeErr(expected_type=<class 'str'>),
40+
value=0,
41+
validator=StringValidator()
42+
)
3943

4044
For both valid and invalid cases, a value is returned -- no exceptions
4145
are raised.

docs/philosophy/predicates.rst

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,20 @@ Usage:
3030
Valid(val=6)
3131

3232
>>> int_validator("a string")
33-
Invalid(err_type=TypeErr(expected_type=<class 'int'>), ...)
33+
Invalid(
34+
err_type=TypeErr(expected_type=<class 'int'>),
35+
value='a string',
36+
validator=IntValidator(Min(minimum=5, exclusive_minimum=False))
37+
)
3438

3539
>>> int_validator(4)
36-
Invalid(err_type=PredicateErrs(predicates=[Min(minimum=5, exclusive_minimum=False)]), ...)
40+
Invalid(
41+
err_type=PredicateErrs(predicates=[
42+
Min(minimum=5, exclusive_minimum=False),
43+
]),
44+
value=4,
45+
validator=IntValidator(Min(minimum=5, exclusive_minimum=False))
46+
)
3747

3848
As you can see the value ``4`` passes the ``int`` type check but fails to pass the ``Min(5)`` predicate.
3949

docs/philosophy/validators.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,11 @@ Usage:
5454
Valid(val=5)
5555

5656
>>> int_validator("not an integer")
57-
Invalid(err_type=TypeErr(expected_type=<class 'int'>), ...)
58-
57+
Invalid(
58+
err_type=TypeErr(expected_type=<class 'int'>),
59+
value='not an integer',
60+
validator=IntValidator()
61+
)
5962

6063
Having this simple function signature-based definition for validation is useful, because it means we can *compose*
6164
validators. Perhaps the simplest example of this is how ``ListValidator`` accepts a validator for the items of the ``list``:

koda_validate/bytes.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,19 @@ class BytesValidator(_ToTupleStandardValidator[bytes]):
1313
>>> from koda_validate import *
1414
>>> validator = BytesValidator(not_blank, MaxLength(100), preprocessors=[strip])
1515
>>> validator(b"")
16-
Invalid(err_type=PredicateErrs(predicates=[NotBlank()]), ...)
16+
Invalid(
17+
err_type=PredicateErrs(predicates=[
18+
NotBlank(),
19+
]),
20+
value=b'',
21+
validator=BytesValidator(NotBlank(), MaxLength(length=100), preprocessors=[Strip()])
22+
)
1723
>>> validator("")
18-
Invalid(err_type=TypeErr(expected_type=<class 'bytes'>), ...)
24+
Invalid(
25+
err_type=TypeErr(expected_type=<class 'bytes'>),
26+
value='',
27+
validator=BytesValidator(NotBlank(), MaxLength(length=100), preprocessors=[Strip()])
28+
)
1929
>>> validator(b' ok ')
2030
Valid(val=b'ok')
2131

koda_validate/string.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,19 @@ class StringValidator(_ToTupleStandardValidator[str]):
1818
>>> from koda_validate import *
1919
>>> validator = StringValidator(not_blank, MaxLength(100), preprocessors=[strip])
2020
>>> validator("")
21-
Invalid(err_type=PredicateErrs(predicates=[NotBlank()]), ...)
21+
Invalid(
22+
err_type=PredicateErrs(predicates=[
23+
NotBlank(),
24+
]),
25+
value='',
26+
validator=StringValidator(NotBlank(), MaxLength(length=100), preprocessors=[Strip()])
27+
)
2228
>>> validator(None)
23-
Invalid(err_type=TypeErr(expected_type=<class 'str'>), ...)
29+
Invalid(
30+
err_type=TypeErr(expected_type=<class 'str'>),
31+
value=None,
32+
validator=StringValidator(NotBlank(), MaxLength(length=100), preprocessors=[Strip()])
33+
)
2434
>>> validator(" ok ")
2535
Valid(val='ok')
2636

0 commit comments

Comments
 (0)