-
-
Notifications
You must be signed in to change notification settings - Fork 589
required, additionalProperties, propertyNames, dependentRequired and any other validators that reference property *names* need a way to indicate the erroring names in ValidationErrors #119
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
Comments
Hey. There isn't unfortunately. There should be. It just doesn't exist because I haven't thought of a nice way to expose it yet. There's an informal agreement that I've wavered back and forth on adding an I'm willing to live with something like that I think. |
Doesn't the property technically exist for Adding the information elsewhere feels like it would make calling code more complicated (more code branches to deal with extra_info). For context, I am building a GUI dynamically from the schemas and then validating the populated values as an object. From the errors I create an error tree that maps back to the GUI component structure and shows error icons (with tooltip) next to the correct field. For required properties I am choosing to display the error next to the missing field rather than at the group level, so this is why I need the property reference. |
I see. Yeah, putting it in (as |
Ah, yes - I forgot it was an array not an object there. So then the logic in my system would be to retrieve the associated schema from |
I agree this would be useful info to have. I'm not so sure about having it in |
Another idea would be that Happy with any solution that makes it easy to get at the appropriate value though. On that note I have noticed that |
Hmm, I think |
I realise I actually meant the other way round, but, yeah, it doesn't seem to work. Have opened a new issue to avoid diverting this one (#120). |
I've came to this issue too, it would be nice to know what property caused an error, not only for the required validator. Maybe a good approach is to return the JSON Path of the property: |
Bumped into this too. What's the way to go here? I can send a PR with what we decide to do. |
I haven't looked at this carefully recently certainly, but I still don't really think there's a good way to do this without just doing |
Voting +1 for this issue. |
Having this issue as well. I like the idea of adding the property name to error.schema_path. Additionally I would suggest something like this: def required_draft4(validator, required, instance, schema):
if not validator.is_type(instance, "object"):
return
for property in required:
if property not in instance:
- yield ValidationError("%r is a required property" % property)
+ yield ValidationError(
+ "%r is a required property" % property,
+ path=[property],
+ ) |
For reference, I currently use the following workaround: # Custom validators
def _required(validator, required, instance, schema):
'''Validate 'required' properties.'''
if not validator.is_type(instance, 'object'):
return
for index, requirement in enumerate(required):
if requirement not in instance:
error = jsonschema.ValidationError(
'{0!r} is a required property'.format(requirement)
)
error.schema_path.append(index)
yield error
# Construct validator as extension of Json Schema Draft 4.
Validator = jsonschema.validators.extend(
validator=jsonschema.validators.Draft4Validator,
validators={
'required': _required
}
) |
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
We also bumped in a similar limitation, accessing the list of fields for errors like As said earlier, with a minimum of indication on the expected strategy we can submit a PR :) Thanks ! |
+1, for now we show directly the error message via our API, but we'll need soon to able to translate and to give more human compliant error messages ;-) |
@leplatrem the expected strategy I think is still to just introduce |
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
@Julian Regarding Have you considered Perhaps specialising ValidationError into That way you could create a subclass Another benefit of specialising How do you feel about that? |
Sure, that sounds reasonable too -- it's trading cognitive burden on understanding the I don't like the fact that we used If you're interested, I'd gladly review a PR implementing thse changes . |
You could add the "required" field into your schema; and once you validate; you know which properties are missing. Worth a try. |
I too am interested in machine-readable properties describing the error. Seems like an ID (REQUIRED_ITEM_MISSING, TYPE_MISMATCH, etc.) and fields like "expected" and "actual" are appropriate:
|
I'm glad I found this issue. This has been my question for the last couple of weeks. I'd been working on a solution to this when I came across this thread. My 2 cents: |
Something like the approach suggested by @groutr sounds great. It would also be great if the error would contain the affected "span" in the original JSON string (so that a GUI could highlight the location of the error), although I'm not sure if this is possible since jsonschema seems to work only on already parsed data (dict, not json string). |
Note that propertyNames is another example (like #119) of something that needs an additional way to say it has errored on a *key* within the instance. So, that's still missing, but path is not the right thing, same as in the earlier instances. It's also becoming somewhat clear that compound validators should possibly grow a way to become lazy...
…ames. These are private, so it's easy to rename them to use preferred modern terminology, namely that individual keywords are called 'keywords' rather than 'validators'. This leaves the latter ('validator') to have one fewer overloaded meaning in this library, leaving it primarily referring to class objects such as DraftNValidator objects, however there are still at least 2 public places where we conflate terminology: * In the VALIDATORS attribute on validator classes, where this attribute is a mapping from str to callable but really the callables are callables for each *keyword* * ValidationError.validator, which is really the *keyword* which failed validation These are of course public API and need deprecating, which hasn't been done thus far mostly to not create API churn simply to rename. In the future however, it's likely that broader deprecations will help us deprecate these. Specifically when we implement fuller support for vocabularies and/or deprecate jsonschema.validators.create in favor of newer objects, we may get a chance to replace VALIDATORS, and when we implement more robust exception types (e.g. to address #119) we likely will deprecate .validator.
Trying to figure out if there is a way to retrieve the name of a missing required property from an error (without hacking it out of error.message).
For example:
In error.path and error.schema_path there is no mention of 'a'. Is there a way to determine that the property missing was 'a' without using error.message?
The text was updated successfully, but these errors were encountered: