-
Notifications
You must be signed in to change notification settings - Fork 40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/strict schema #169
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -115,7 +115,13 @@ def disallow_extra_fields(self, processed_data, original_data): | |
RequestSchema = Schema | ||
|
||
|
||
# Define a constructor to please some IDE type checking | ||
class ResponseSchema(ActuallyRequireOnDumpMixin, Schema): | ||
def __init__(self, **kwargs): | ||
super().__init__(**kwargs) | ||
|
||
|
||
class StrictSchema(RequestSchema, ResponseSchema): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
pass | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing docstring for public class. Once added, the |
||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,7 @@ | |
from flask_rebar import messages | ||
from flask_rebar.utils.request_utils import normalize_schema | ||
from tests.helpers import skip_if_marshmallow_not_v2 | ||
from flask_rebar.validation import ActuallyRequireOnDumpMixin | ||
from flask_rebar.validation import ActuallyRequireOnDumpMixin, StrictSchema | ||
from flask_rebar.validation import CommaSeparatedList | ||
from flask_rebar.validation import DisallowExtraFieldsMixin | ||
from flask_rebar.validation import QueryParamList | ||
|
@@ -117,6 +117,30 @@ def test_required_failed_validate(self): | |
self.assertIn("one_of_validation", ctx.exception.messages) | ||
|
||
|
||
class ActuallyStrictSchema(StrictSchema): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
optional = fields.Str() | ||
required = fields.Str(required=True, allow_none=False) | ||
|
||
|
||
class TestStrictSchema(TestCase): | ||
def test_load_nominal(self): | ||
ActuallyStrictSchema().load({"optional": "foo", "required": "bar"}) | ||
|
||
def test_load_unexpected_field(self): | ||
data, errs = ActuallyStrictSchema().load( | ||
{"optional": "foo", "required": "bar", "foo": "bar"} | ||
) | ||
self.assertEqual(errs, {"_schema": [messages.unsupported_fields(["foo"])]}) | ||
|
||
def test_dump_nominal(self): | ||
ActuallyStrictSchema().dump({"optional": "foo", "required": "bar"}) | ||
|
||
def test_dump_missing_required(self): | ||
with self.assertRaises(ValidationError) as ctx: | ||
compat.dump(ActuallyStrictSchema(), {"optional": "foo"}) | ||
self.assertIn("required", ctx.exception.messages) | ||
|
||
|
||
class StringList(Schema): | ||
foos = CommaSeparatedList(fields.String()) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function call overhead in python is significant. It’s best practice not to introduce this extra no-op function call that reduces performance just for some IDEs that aren’t currently finding the inherited method. Can you follow up with your IDE vendor instead of adding this here?