Skip to content
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

Validate Only and Exclude #826

Merged
merged 8 commits into from Jul 1, 2018
Merged

Conversation

deckar01
Copy link
Member

Validate only and exclude against the declared fields to give developers feedback when they use invalid field names in the schema configuration.

Fixes #636

sloria
sloria previously requested changes May 23, 2018
Copy link
Member

@sloria sloria left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Behavior looks correct. Only suggestion is to change the error type.

else:
message = '{0} is not a valid field for {1}.'
message = message.format(invalid_names, self.__class__.__name__)
raise AttributeError(message)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AttributeError doesn't feel like the correct error. From the docs.

Raised when an attribute reference (see Attribute references) or assignment fails.

I think ValueError is more appropriate.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense. I was thinking of fields as schema class attributes, but that isn't really how they are used.

return ' and '.join(values)
if len(values) == 1:
return values[0]
return 'None'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make more sense to raise an error instead of return 'None' here? We shouldn't ever hit this, but it would be incorrect to get 'None is not a valid field for MySchema'.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a generic utility it seemed like a logical representation of an empty list. It is currently impossible to hit this line from Schema._update_fields().

Maybe we don't need to format the message as a single string though. Developers seem to be just fine with lists and dictionaries as the "message" value for validation exceptions. Any preference for the message structure?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we don't need to format the message as a single string though. Developers seem to be just fine with lists and dictionaries as the "message" value for validation exceptions. Any preference for the message structure?

Yep. I wouldn't bother creating a string.

    if invalid_fields:
        raise AttributeError("Invalid fields for {0}: {1}".format(self.__class__.__name__, invalid_fields))

Validation errors may be exposed to end users (like when using webargs) or even parsed by API clients, so they should be consistent, but this is just meant to be seen by the developer after a typo, so don't overthink it.

:param tuple|list only: Whitelist of the declared fields to select when
instantiating the Schema. If None, all fields are used. Nested fields
can be represented with dot delimiters.
:param tuple|list exclude: Blacklist of of the declared fields to exclude
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blacklist of of the declared fields

"of" is repeated


if invalid_fields:
invalid_names = ['"{}"'.format(name) for name in invalid_fields]
invalid_names = utils.iterable_to_string(invalid_names)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Pylint nazi inside)

Reusing variable name is considered bad practice (R0204 (redefined-variable-type)).

You could append _list or _str to one of them, or just go for the one-liner equivalent.

jeetjitsu and others added 6 commits June 1, 2018 13:33
…rializaton

Only field was not consistent with its treat of fields and declared
fields. To make it consistent fieldnames passed in as only not present
in Schema will be ignored. Two previous tests which were checking for
errors with invalid only fields were dropped as a result. Documentation
has also been updated to explicitly state that invalid fieldnames will
be ignored Documentation has also been updated to explicitly state that
invalid fieldnames will be ignored.
@deckar01 deckar01 dismissed sloria’s stale review June 1, 2018 20:15

Error type changed as requested.

@sloria
Copy link
Member

sloria commented Jun 8, 2018

@deckar01 Would you mind updating the changelog and possibly the upgrading docs as well?

LMK if you don't have time right now; I can do it when I have some time this weekend or next week.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants