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

Issue with Optional and non existing file - wrong error message #28

Open
c-holtermann opened this issue Oct 24, 2013 · 5 comments
Open

Comments

@c-holtermann
Copy link

I want to parse command line arguments.
I have this schema:

# Validation of arguments
schema = Schema({
        '<inputHocrFile>': Use(open, error='<inputHocrFile> is not readable'),
        # ignore other keys
        Optional(str): object })

validating

schema.validate({'<inputHocrFile>':"non existing file"})

leads to error message

SchemaError: missed keys set(['<inputHocrFile>'])

I expected

<inputHocrFile> is not readable

Another Schema:

schema=Schema({'<inputHocrFile>': Use(open, error="ERR")})

leads to the expected error message when doing

schema.validate({'<inputHocrFile>':"non existing file"})

while

schema.validate({'<inputHocrFile>':"non existing file","a":""})

leads to

SchemaError: key '<inputHocrFile>' is required

when the correct error message would be about a wrong key "a"

Another Schema:

schema=Schema({'<inputHocrFile>': Use(open, error="ERR"),str:object})
schema.validate({'<inputHocrFile>':"non existing file"})

leads to error message

SchemaError: missed keys set(['<inputHocrFile>'])

when the actual error message(s) should have been about a missing file and

SchemaError: missed keys set([<type 'str'>])

So:
There are some errors in error messaging.

Question:
Is the Syntax

{Optional(str): object}

possible ?

because that's what I want: Optionally other keys.

@keleshev
Copy link
Owner

keleshev commented Dec 3, 2013

Sorry for late response. {Optional(str): object} should be possible. In fact Optional works only with keys. But I'm not sure I understand the other part of your question.

@andialbrecht
Copy link

I came across the same issue with extra values today. Here's a trivial code snippet that illustrates the problem:

>>> from schema import *
>>> schema = Schema({'foo': str})
>>> schema.validate({'foo': 'good', 'bar': 123})
SchemaError: key 'foo' is required

As you can see, "foo" is in data (and a str as requested by the schema), but there's an additional key "bar" that is not specified by the schema. However, the error message is wrong in that case. I'd expect something like "SchemaError: superfluous key 'bar'"

@vidma
Copy link
Collaborator

vidma commented Feb 18, 2014

this shall be fixed in the last commit in the master (from yesterday) in 9a24674.

Below is the output from the master branch, which I consider to be correct:

>>> from schema import *
>>> schema = Schema({'foo': str})
>>> schema.validate({'foo': 'good', 'bar': 123})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "schema.py", line 147, in validate
    e)
schema.SchemaError: wrong keys 'bar' in {'foo': 'good', 'bar': 123}
>>> 

we might still need to go through all the examples, and make sure they are in the tests, but I assume it shall be fine now.

@andialbrecht
Copy link

Great! Thanks.

@sjakobi
Copy link
Contributor

sjakobi commented Sep 26, 2015

Testing the original examples with the current master (e94b714):

1

# Validation of arguments
schema = Schema({
        '<inputHocrFile>': Use(open, error='<inputHocrFile> is not readable'),
        # ignore other keys
        Optional(str): object })

schema.validate({'<inputHocrFile>':"non existing file"})

I expected

is not readable

Current output:

schema.SchemaError: <inputHocrFile> is not readable

So this one is resolved.

2

schema=Schema({'<inputHocrFile>': Use(open, error="ERR")})
schema.validate({'<inputHocrFile>':"non existing file","a":""})

leads to

SchemaError: key '' is required
when the correct error message would be about a wrong key "a"

Current output:

SchemaError: ERR

I'm not sure I'd necessarily expect a complaint about the key "a" here.
Why was that your expectation, @c-holtermann? What do you think about the current error message?

3

schema=Schema({'<inputHocrFile>': Use(open, error="ERR"),str:object})
schema.validate({'<inputHocrFile>':"non existing file"})

leads to error message
SchemaError: missed keys set([''])
when the actual error message(s) should have been about a missing file and
SchemaError: missed keys set([<type 'str'>])

Current output:

SchemaError: ERR

For

schema=Schema({'<inputHocrFile>': Use(open),str:object})
schema.validate({'<inputHocrFile>':"non existing file"})

I do get

SchemaError: open('non existing file') raised FileNotFoundError(2, 'No such file or directory')

though, which I find quite satisfactory.

Your expectation, @c-holtermann, seems to be that you would get an error message for each validation problem when there are several.

While that may be desirable in some cases, it could also turn out problematic, for example when the validation is computationally costly.
In some cases it is ultimately impossible to detect all validation problems for an input on the first run, for example, when an error with a dictionary key hides a problem with the value.

But it seems that there has been quite a bit of discussion on this topic here: #8

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

5 participants