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

Wrong keys exception can point at the wrong (wrong) keys #80

Open
sjakobi opened this issue Sep 25, 2015 · 6 comments
Open

Wrong keys exception can point at the wrong (wrong) keys #80

sjakobi opened this issue Sep 25, 2015 · 6 comments

Comments

@sjakobi
Copy link
Contributor

sjakobi commented Sep 25, 2015

With the current master:

>>> s = Schema({And(str, Use(str.lower), 'id'): int})
>>> data = {'Id': 10, 'Name': 'me'}
>>> s.validate(data)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/simon/src/schema/schema.py", line 152, in validate
    e)
schema.SchemaError: Wrong keys 'Id', 'Name' in {'Name': 'me', 'Id': 10}

The problem is that the error message claims that 'Id' is a wrong key although 'Id' has been validated. The only key that hasn't been validated in this example is 'Name'.

This is due to 'Id''s schema, And(str, Use(str.lower), 'id'), which transforms 'Id' to 'id' which results in 'Id' being included in wrong_keys.

In general, this incorrect error message can always appear if key schemas transform their data.

Any ideas how to fix this? Is there a way to compute wrong_keys that is both simple and precise?

@skorokithakis
Copy link
Collaborator

I'm not sure this is entirely wrong. For example, you may want to specify case-insensitive keys, so that any capitalization of id would work. This would be a good way to do that. If you want to specify that the starting key should always be Id, you can do And("Id", Use(str.lower)).

@sjakobi
Copy link
Contributor Author

sjakobi commented Sep 25, 2015

Using your schema doesn't really change much in this problem:

>>> s = Schema({And("Id", Use(str.lower)): int})
>>> data = {'Id': 10, 'Name': 'me'}
>>> s.validate(data)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "schema.py", line 153, in validate
    e)
schema.SchemaError: Wrong keys 'Id', 'Name' in {'Id': 10, 'Name': 'me'}

'Id', though validated, is still included in the error message, because its schema has transformed it to 'id'.

@skorokithakis
Copy link
Collaborator

The idea is that you'd use the final key, "id".

@sjakobi
Copy link
Contributor Author

sjakobi commented Sep 25, 2015

I'm afraid I don't quite understand what your proposed solution is.
How would you compute wrong_keys?

@skorokithakis
Copy link
Collaborator

Oh, I see what you mean now. I was thinking of the way we validate the values, not the keys. I'll have a look at this in more detail later and get back to you.

@sjakobi sjakobi mentioned this issue Sep 26, 2015
sjakobi added a commit to sjakobi/schema that referenced this issue Oct 9, 2015
sjakobi added a commit to sjakobi/schema that referenced this issue Oct 9, 2015
@sjakobi
Copy link
Contributor Author

sjakobi commented Oct 9, 2015

So, I guess the downside to #80 is that we have additional runtime costs to build matched_input_keys even if we don't end up using it at all.

Does anyone see a way of moving the computation to the point where we already know that we're going to have an exception?

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

No branches or pull requests

2 participants