Skip to content

Can we get more info from the missing required fields validator? #1030

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

Closed
ghost opened this issue Dec 14, 2022 · 1 comment
Closed

Can we get more info from the missing required fields validator? #1030

ghost opened this issue Dec 14, 2022 · 1 comment

Comments

@ghost
Copy link

ghost commented Dec 14, 2022

Consider:

import jsonschema

schema = {
	"type": "object",
	"properties": {
		"cat": {
			"type": "string"
		},
		"dog": {
			"type": "string"
		}
	},
	"required": ["cat","dog"]
}

data = {
	"cat": "socks"
}

validator = jsonschema.Draft7Validator(schema)

for e in validator.iter_errors(data):
	print("ERROR")
	for k,v in e.__dict__.items():
		print(k + " = " + str(v))

The output is:

ERROR
message = 'dog' is a required property
path = deque([])
relative_path = deque([])
schema_path = deque(['required'])
relative_schema_path = deque(['required'])
context = []
cause = None
validator = required
validator_value = ['cat', 'dog']
instance = {'cat': 'socks'}
schema = {'type': 'object', 'properties': {'cat': {'type': 'string'}, 'dog': {'type': 'string'}}, 'required': ['cat', 'dog']}
parent = None
_type_checker = <TypeChecker types={'array', 'boolean', 'integer', 'null', 'number', 'object', 'string'}>

In this case, only the "dog" field is missing but there is no field on the error that tells me that without taking apart the message (which I don't want to do as the message is more likely to change that a separate field).

validator_value lists both fields so that isn't useful.

Comparing the validator_value items to keys in instance may work, but that gets complex if there are 3 or more required fields:

import jsonschema

schema = {
	"type": "object",
	"properties": {
		"cat": {
			"type": "string"
		},
		"dog": {
			"type": "string"
		},
		"ferret": {
			"type": "string"
		}
	},
	"required": ["cat","dog", "ferret"]
}

data = {
	"cat": "socks"
}

validator = jsonschema.Draft7Validator(schema)

for e in validator.iter_errors(data):
	print("ERROR")
	for k,v in e.__dict__.items():
		print(k + " = " + str(v))

Gives:

ERROR
message = 'dog' is a required property
path = deque([])
relative_path = deque([])
schema_path = deque(['required'])
relative_schema_path = deque(['required'])
context = []
cause = None
validator = required
validator_value = ['cat', 'dog', 'ferret']
instance = {'cat': 'socks'}
schema = {'type': 'object', 'properties': {'cat': {'type': 'string'}, 'dog': {'type': 'string'}, 'ferret': {'type': 'string'}}, 'required': ['cat', 'dog', 'ferret']}
parent = None
_type_checker = <TypeChecker types={'array', 'boolean', 'integer', 'null', 'number', 'object', 'string'}>
ERROR
message = 'ferret' is a required property
path = deque([])
relative_path = deque([])
schema_path = deque(['required'])
relative_schema_path = deque(['required'])
context = []
cause = None
validator = required
validator_value = ['cat', 'dog', 'ferret']
instance = {'cat': 'socks'}
schema = {'type': 'object', 'properties': {'cat': {'type': 'string'}, 'dog': {'type': 'string'}, 'ferret': {'type': 'string'}}, 'required': ['cat', 'dog', 'ferret']}
parent = None
_type_checker = <TypeChecker types={'array', 'boolean', 'integer', 'null', 'number', 'object', 'string'}>

Could we have a new field added to this error to just state what is missing? (in the first example - "dog")

Happy to have a go if you think this is a good idea.

Thank you!

@Julian
Copy link
Member

Julian commented Dec 21, 2022

Hi, this is #119 -- which essentially is going to be solved by doing #119 (comment) -- a small PR to get that started likely is indeed welcome, though it's also on my own TODO list after I finish working on the $ref implementation.

Certainly if you care to give it a shot though it's welcome in small chunks! Otherwise keep an eye on that issue.

@Julian Julian closed this as completed Dec 21, 2022
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

1 participant