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

Commit 1027238

Browse files
authored
add Password field (#109)
1 parent d6483d8 commit 1027238

File tree

5 files changed

+22
-4
lines changed

5 files changed

+22
-4
lines changed

docs/fields.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ For example: `username = typesystem.String(max_length=100)`
6666
Validates multi-line strings. Takes the same arguments as `String`.
6767
Represented in HTML forms as a `<textarea>`.
6868

69+
### Password
70+
71+
Similar to `String` and takes the same arguments.
72+
Represented in HTML forms as a `<input type="password">`.
73+
6974
## Boolean data types
7075

7176
### Boolean

tests/test_fields.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
Integer,
1818
Number,
1919
Object,
20+
Password,
2021
String,
2122
Time,
2223
Union,
@@ -832,3 +833,9 @@ def test_validation_error_is_hashable():
832833
validator = Integer()
833834
_, error = validator.validate_or_error("abc")
834835
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 & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class Contact(typesystem.Schema):
99
b = typesystem.String(max_length=10)
1010
c = typesystem.Text()
1111
d = typesystem.Choice(choices=[("abc", "Abc"), ("def", "Def"), ("ghi", "Ghi")])
12+
password = typesystem.Password()
1213

1314

1415
forms = typesystem.Jinja2Forms(package="typesystem")
@@ -23,13 +24,11 @@ def test_form_rendering():
2324
assert html.count('<input type="text" ') == 1
2425
assert html.count("<textarea ") == 1
2526
assert html.count("<select ") == 1
27+
assert html.count('<input type="password" ') == 1
2628

2729

2830
def test_password_rendering():
29-
class PasswordForm(typesystem.Schema):
30-
password = typesystem.String(format="password")
31-
32-
form = forms.Form(PasswordForm, values={"password": "secret"})
31+
form = forms.Form(Contact, values={"password": "secret"})
3332
html = str(form)
3433
assert "secret" not in html
3534

typesystem/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
Integer,
1414
Number,
1515
Object,
16+
Password,
1617
String,
1718
Text,
1819
Time,
@@ -40,6 +41,7 @@
4041
"Float",
4142
"Number",
4243
"Object",
44+
"Password",
4345
"Reference",
4446
"String",
4547
"Text",

typesystem/fields.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,3 +765,8 @@ def validate(self, value: typing.Any, strict: bool = False) -> typing.Any:
765765
class UUID(String):
766766
def __init__(self, **kwargs: typing.Any) -> None:
767767
super().__init__(format="uuid", **kwargs)
768+
769+
770+
class Password(String):
771+
def __init__(self, **kwargs: typing.Any) -> None:
772+
super().__init__(format="password", **kwargs)

0 commit comments

Comments
 (0)