Issue with Optional and non existing file - wrong error message
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.
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.
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'"
this shall be fixed in the last commit in the master (from yesterday) in https://github.com/halst/schema/commit/9a2467474a900d4b7b5c0501990e7fbfcfc85685.
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.
Great! Thanks.
Testing the original examples with the current master (e94b7144f3016654d1360eb1c070fd2db0d54a43):
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
<inputHocrFile> 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 '<inputHocrFile>' 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(['<inputHocrFile>']) 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: https://github.com/keleshev/schema/issues/8