RedHatProductSecurity / cvelib

A Python library and command line interface for CVE Services.
MIT License
52 stars 24 forks source link

More detailed errors when validating the schema #82

Closed MrSeccubus closed 1 month ago

MrSeccubus commented 1 month ago

As a use I would like to have more detailed errors when a CVE record does not validate.

Where's the code I have in cna-bot for this:

        v = Draft7Validator(schema)
        errors = sorted(v.iter_errors(json_data), key=lambda e: e.message)
        if errors:
            error_str = "Schema validation of CVE record failed. The reason is likely one or more of those listed below:"
            for error in errors:
                for suberror in sorted(error.context, key=lambda e: e.schema_path) :
                    error_str = "{}\n{} : {}".format(error_str, suberror.json_path, suberror.message)

            results.append(error_str)

This also prints out the path of the offending item.

mprpic commented 1 month ago

@MrSeccubus If you're using CveRecord.validate() (from cvelib.cve_api) it will throw an exception that will have an errors attribute that contains all of the jsonschema-identified issues in the validated JSON.

MrSeccubus commented 1 month ago

Thanks, it indeed looks promising.

Was it a deliberate choise to let this method raise an exeception, because in my mind a validation routine getting data that does not validate is not an execption, but an exepected result.

mprpic commented 1 month ago

Yes, the exception is just the delivery mechanism of the errors and can also be used to halt the execution of whatever is validating the content.