Skip to content
This repository was archived by the owner on Aug 19, 2025. It is now read-only.

Commit 7c22654

Browse files
committed
Merge branch 'master' into version-0.3-beta
2 parents b43f273 + 1027238 commit 7c22654

File tree

9 files changed

+61
-21
lines changed

9 files changed

+61
-21
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,16 @@ Python 3.6+
3939
$ pip3 install typesystem
4040
```
4141

42-
If you'd like you use the form rendering you'll also want to install `jinja2`.
42+
If you'd like you use the form rendering using `jinja2`:
4343

4444
```shell
45-
$ pip3 install jinja2
45+
$ pip3 install typesystem[jinja2]
4646
```
4747

48-
If you'd like you use the YAML tokenization you'll also want to install `pyyaml`.
48+
If you'd like you use the YAML tokenization using `pyyaml`:
4949

5050
```shell
51-
$ pip3 install pyyaml
51+
$ pip3 install typesystem[pyyaml]
5252
```
5353

5454
## Quickstart

docs/fields.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ For example: `username = typesystem.String(max_length=100)`
7878
Validates multi-line strings. Takes the same arguments as `String`.
7979
Represented in HTML forms as a `<textarea>`.
8080

81+
### Password
82+
83+
Similar to `String` and takes the same arguments.
84+
Represented in HTML forms as a `<input type="password">`.
85+
8186
## Boolean data types
8287

8388
### Boolean
@@ -164,7 +169,7 @@ Used to validate a list of data. For example:
164169

165170
```python
166171
# Validates data like `[8, 7, 0, 8, 4, 5]`
167-
ratings = typesystem.Array(items=typesystem.Integer(min_value=0, max_value=10))
172+
ratings = typesystem.Array(items=typesystem.Integer(minimum=0, maximum=10))
168173
```
169174

170175
**Arguments**:
@@ -215,3 +220,12 @@ owner = typesystem.Reference(to="User", allow_null=True, definitions=definitions
215220

216221
* `to` - Name of schema defined in definitions. **Required**
217222
* `definitions` - `Definitions` instance. **Required**
223+
224+
## Other data types
225+
226+
### UUID
227+
228+
Validates UUID in the format of 32 hexadecimal characters, separated by hyphens.
229+
For example `"cd11b0d7-d8b3-4b5c-8159-70f5c9ea96ab"`.
230+
231+
Returns `uuid.UUID` instances.

docs/index.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,16 @@ Python 3.6+
3333
$ pip3 install typesystem
3434
```
3535

36-
If you'd like you use the form rendering you'll also want to install `jinja2`.
36+
If you'd like you use the form rendering using `jinja2`:
3737

3838
```shell
39-
$ pip3 install jinja2
39+
$ pip3 install typesystem[jinja2]
40+
```
41+
42+
If you'd like you use the YAML tokenization using `pyyaml`:
43+
44+
```shell
45+
$ pip3 install typesystem[pyyaml]
4046
```
4147

4248
## Quickstart

setup.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ def get_packages(package):
4141
packages=get_packages('typesystem'),
4242
package_data={'typesystem': ['py.typed', 'templates/forms/*.html']},
4343
install_requires=[],
44+
extras_require={
45+
"jinja2": ["jinja2"],
46+
"pyyaml": ["pyyaml"],
47+
},
4448
python_requires='>=3.6',
4549
classifiers=[
4650
'Development Status :: 3 - Alpha',

tests/test_fields.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from typesystem.base import Message, ValidationError
77
from typesystem.fields import (
8+
UUID,
89
Array,
910
Boolean,
1011
Choice,
@@ -16,6 +17,7 @@
1617
Integer,
1718
Number,
1819
Object,
20+
Password,
1921
String,
2022
Time,
2123
Union,
@@ -715,13 +717,13 @@ def test_datetime():
715717

716718

717719
def test_uuid():
718-
validator = String(format="uuid")
720+
validator = UUID()
719721
value, error = validator.validate_or_error("93e19019-c7a6-45fe-8936-f6f4d550f35f")
720722
assert value == uuid.UUID("93e19019-c7a6-45fe-8936-f6f4d550f35f")
721723

722-
validator = String(format="uuid")
724+
validator = UUID()
723725
value, error = validator.validate_or_error("1245a678-1234-1234-1234-123412341234")
724-
assert error == ValidationError(text="Must be valid UUID format.", code="format")
726+
assert error == ValidationError(text="Must be a valid UUID format.", code="format")
725727

726728

727729
def test_union():
@@ -831,3 +833,9 @@ def test_validation_error_is_hashable():
831833
validator = Integer()
832834
_, error = validator.validate_or_error("abc")
833835
hash(error)
836+
837+
838+
def test_password():
839+
validator = Password()
840+
value, _ = validator.validate_or_error("secret")
841+
assert value == "secret"

tests/test_forms.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"d": typesystem.Choice(
1313
choices=[("abc", "Abc"), ("def", "Def"), ("ghi", "Ghi")]
1414
),
15+
"password": typesystem.Password(),
1516
}
1617
)
1718

@@ -28,18 +29,11 @@ def test_form_rendering():
2829
assert html.count('<input type="text" ') == 1
2930
assert html.count("<textarea ") == 1
3031
assert html.count("<select ") == 1
32+
assert html.count('<input type="password" ') == 1
3133

3234

3335
def test_password_rendering():
34-
password_schema = typesystem.Schema(
35-
fields={
36-
"password": typesystem.String(format="password"),
37-
"active": typesystem.Boolean(read_only=True, default=True),
38-
}
39-
)
40-
41-
form = forms.create_form(password_schema)
42-
form.validate(data={"password": "secret"})
36+
form = forms.create_form(contact, values={"password": "secret"})
4337
html = str(form)
4438
assert "secret" not in html
4539

typesystem/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from typesystem.base import Message, ParseError, Position, ValidationError
22
from typesystem.fields import (
3+
UUID,
34
Any,
45
Array,
56
Boolean,
@@ -12,6 +13,7 @@
1213
Integer,
1314
Number,
1415
Object,
16+
Password,
1517
String,
1618
Text,
1719
Time,
@@ -24,7 +26,7 @@
2426
from typesystem.tokenize.tokenize_json import tokenize_json, validate_json
2527
from typesystem.tokenize.tokenize_yaml import tokenize_yaml, validate_yaml
2628

27-
__version__ = "0.3.0.dev1"
29+
__version__ = "0.3.0.dev2"
2830
__all__ = [
2931
"Array",
3032
"Any",
@@ -39,11 +41,13 @@
3941
"Float",
4042
"Number",
4143
"Object",
44+
"Password",
4245
"Reference",
4346
"String",
4447
"Text",
4548
"Time",
4649
"Union",
50+
"UUID",
4751
# Schemas
4852
"Schema",
4953
"Definitions",

typesystem/fields.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -762,3 +762,13 @@ def validate(self, value: typing.Any) -> typing.Any:
762762
raise self.validation_error("only_null")
763763
raise self.validation_error("const")
764764
return value
765+
766+
767+
class UUID(String):
768+
def __init__(self, **kwargs: typing.Any) -> None:
769+
super().__init__(format="uuid", **kwargs)
770+
771+
772+
class Password(String):
773+
def __init__(self, **kwargs: typing.Any) -> None:
774+
super().__init__(format="password", **kwargs)

typesystem/formats.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ def serialize(self, obj: typing.Any) -> typing.Union[str, None]:
155155

156156

157157
class UUIDFormat(BaseFormat):
158-
errors = {"format": "Must be valid UUID format."}
158+
errors = {"format": "Must be a valid UUID format."}
159159

160160
def is_native_type(self, value: typing.Any) -> bool:
161161
return isinstance(value, uuid.UUID)

0 commit comments

Comments
 (0)