使用python

时间:2018-05-17 23:34:42

标签: python jsonschema

是否有一种简单的方法来获取未通过jsonschema验证的所有密钥集?

我正在使用jsonschema包中的Draft4Validator,我正在针对某些配置验证json文件,并使用iter_errors方法迭代所有错误。它向我展示了所有错误,但如果有办法获得验证失败的所有密钥会很好 - 这可能吗?

我已经尝试了解所有错误并尝试从验证器的输出中挖出密钥,但它变得冗长乏味。

编辑:我正在尝试使用ErrorTree来解决这个问题:Get the property for each jsonschema errror

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_pa​​tterns

因为这些是其中存在实际错误的那些。

0 个答案:

没有答案