Skip to content

Commit

Permalink
add Password field (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
aminalaee authored Sep 13, 2021
1 parent d6483d8 commit 1027238
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 4 deletions.
5 changes: 5 additions & 0 deletions docs/fields.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ For example: `username = typesystem.String(max_length=100)`
Validates multi-line strings. Takes the same arguments as `String`.
Represented in HTML forms as a `<textarea>`.

### Password

Similar to `String` and takes the same arguments.
Represented in HTML forms as a `<input type="password">`.

## Boolean data types

### Boolean
Expand Down
7 changes: 7 additions & 0 deletions tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
Integer,
Number,
Object,
Password,
String,
Time,
Union,
Expand Down Expand Up @@ -832,3 +833,9 @@ def test_validation_error_is_hashable():
validator = Integer()
_, error = validator.validate_or_error("abc")
hash(error)


def test_password():
validator = Password()
value, _ = validator.validate_or_error("secret")
assert value == "secret"
7 changes: 3 additions & 4 deletions tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Contact(typesystem.Schema):
b = typesystem.String(max_length=10)
c = typesystem.Text()
d = typesystem.Choice(choices=[("abc", "Abc"), ("def", "Def"), ("ghi", "Ghi")])
password = typesystem.Password()


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


def test_password_rendering():
class PasswordForm(typesystem.Schema):
password = typesystem.String(format="password")

form = forms.Form(PasswordForm, values={"password": "secret"})
form = forms.Form(Contact, values={"password": "secret"})
html = str(form)
assert "secret" not in html

Expand Down
2 changes: 2 additions & 0 deletions typesystem/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
Integer,
Number,
Object,
Password,
String,
Text,
Time,
Expand Down Expand Up @@ -40,6 +41,7 @@
"Float",
"Number",
"Object",
"Password",
"Reference",
"String",
"Text",
Expand Down
5 changes: 5 additions & 0 deletions typesystem/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -765,3 +765,8 @@ def validate(self, value: typing.Any, strict: bool = False) -> typing.Any:
class UUID(String):
def __init__(self, **kwargs: typing.Any) -> None:
super().__init__(format="uuid", **kwargs)


class Password(String):
def __init__(self, **kwargs: typing.Any) -> None:
super().__init__(format="password", **kwargs)

0 comments on commit 1027238

Please sign in to comment.