Есть ли простой способ получить набор всех ключей, которые не прошли проверку jsonschema?
Я использую Draft4Validator из пакета jsonschema и проверяю файл json на наличие некоторых настроек и использую метод iter_errors
для перебора всех ошибок. Он показывает мне все ошибки, но было бы неплохо, если бы был способ получить все ключи, которые не прошли проверку - возможно ли это?
Я попытался пройти через все ошибки и попытаться выкопать ключи из вывода из валидатора, но он становится длинным и утомительным.
РЕДАКТИРОВАТЬ: я пытаюсь использовать ErrorTree в соответствии с этим вопросом: Получить свойство для каждой ошибки jsonschema
def get_errors_in_tree(error_tree):
errors = []
if error_tree.total_errors <= 1:
return map(lambda ve: list(ve.instance.keys()), error_tree.errors.values())
for error in error_tree:
errors.extend(get_errors_in_tree(error_tree[error]))
return errors
Но он все еще не дает мне ВСЕ недействительные ключи, только некоторые из них.
Вот моя схема:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Git Hooks Config",
"description": "Git Hooks Config",
"type" : "object",
"properties" : {
"git_hooks_config" : {
"type" : "object",
"properties" : {
"prepare_commit_msg": {
"type" : "object",
"properties" : {
"template" : { "type" : "string" }
},
"additionalProperties" : false
},
"commit_msg": {
"type" : "object",
"properties" : {
"regex" : { "type" : "string" },
"regex_flags" : {
"type" : "array",
"items" : {
"type" : "string",
"enum": [ "T", "TEMPLATE", "I", "IGNORECASE",
"L", "LOCALE", "M", "MULTILINE",
"S", "DOTALL", "U", "UNICODE", "X",
"VERBOSE", "DEBUG", "A", "ASCII" ]
}
},
"error_message" : { "type" : "string" }
},
"additionalProperties" : false
},
"pre_push": {
"type" : "object",
"properties" : {
"validate_branch_name" : { "type" : "boolean" },
"branch_name_regex" : { "type" : "string" },
"branch_name_regex_flags" : {
"type" : "array",
"items" : {
"type" : "string",
"enum": [ "T", "TEMPLATE", "I", "IGNORECASE",
"L", "LOCALE", "M", "MULTILINE",
"S", "DOTALL", "U", "UNICODE", "X",
"VERBOSE", "DEBUG", "A", "ASCII" ]
}
},
"branch_name_error_message" : { "type" : "string" }
},
"additionalProperties" : false
},
"external_git_hooks": {
"type" : "object",
"properties" : {
"prepare_commit_msg" : {
"type" : "object",
"properties" : {
"relative_path_to_script" : { "type" : "string" },
"when_to_execute" : {
"type" : "string",
"enum" : [ "before", "after" ]
}
},
"additionalProperties" : false
},
"commit_msg" : {
"type" : "object",
"properties" : {
"relative_path_to_script" : { "type" : "string" },
"when_to_execute" : {
"type" : "string",
"enum" : [ "before", "after" ]
}
},
"additionalProperties" : false
},
"pre_commit" : {
"type" : "object",
"properties" : {
"relative_path_to_script" : { "type" : "string" },
"when_to_execute" : {
"type" : "string",
"enum" : [ "before", "after" ]
}
},
"additionalProperties" : false
},
"pre_push" : {
"type" : "object",
"properties" : {
"relative_path_to_script" : { "type" : "string" },
"when_to_execute" : {
"type" : "string",
"enum" : [ "before", "after" ]
}
},
"additionalProperties" : false
}
},
"additionalProperties" : false
}
},
"additionalProperties" : false
},
"repository_properties": {
"type" : "object",
"properties" : {
"unvalidated_patterns" : {
"type" : "array",
"items" : { "type" : "array" }
}
},
"additionalProperties" : false
}
},
"additionalProperties" : false
}
А вот мой конфиг
{
"git_hooks_config" : {
"prepare_commit_msg" : {
"templadste" : "\nIssue: <JIRA_TICKET_NUMBER>"
},
"commit_msg" : {
"regsex" : ".*",
"regeax_flags" : [ "DOTALL" ],
"errdor_message" : "The commit message does not match the supplied regex: '%s'"
},
"pre_push" : {
"validate_branch_name" : true,
"branch_name_regex" : "^(master|(feature|bugfix|release)/[\\w-]{3,80})$",
"branch_name_regex_flags" : [ ],
"branchd_name_error_message" : "The branch name: '%s' does not match the supplied regex: '%s'"
},
"external_git_hooks" : {
"prepare_commit_msg" : {
"when_to_execute" : "after"
},
"commit_msg" : {
"when_to_execute" : "after"
},
"pre_commit" : {
"when_to_execute" : "after"
},
"pre_push" : {
"when_taso_execute" : "after"
}
}
},
"repository_properties" : {
"unvadlidated_patterns" : [
[ "*", "src", "test", "*" ],
[ "*", "src", "generated", "*" ]
]
}
}
Вот что я получаю из моего метода:
[[u'unvadlidated_patterns'], [u'regeax_flags', u'errdor_message', u'regsex'], [u'validate_branch_name', u'branchd_name_error_message', u'branch_name_regex_flags', u'branch_name_regex'], [u'templadste']]
И что я хочу получить, это список следующих записей:
- templadste
- regsex
- regeax_flags
- errdor_message
- branchd_name_error_message
- when_taso_execute
- unvadlidated_patterns
Потому что это те, в которых есть настоящие ошибки.