Conversation
italvi
left a comment
There was a problem hiding this comment.
@mmarseu some observations from my side.
Further: If I provide a JSON-file, e.g. an SBOM as schema-path the program crashes with:

Maybe a sanity check, e.g. with checking if something like $schema": "http://json-schema.org/draft-07/schema#" is within the provided schema (without the specific schema, so only json-schema.org), is a good idea here?
This must be what you meant when you said you found an error that has always been there. I was dumb enough to promise I'd fix it 🙄 For some reason I started getting a whole lot of mypy errors in |
There was a problem hiding this comment.
@mmarseu after some testing, I found two "bugs", where I think this one is the most critical one: if the JSON-Schema is e.g. an SBOM without dependencies-array or just an empty JSON, i.e. {}, the validation is successful as jsonschema fallbacks to Draft7Validator if no $schema is found in the provided schema.
But as this is something for the lib itself, I approve the MR. The only thing I could think of: Add a disclaimer in the documentation to be aware of this behavior. But I won't insist on this one, so I leave the decision to you: Merge or change - I can approve it again 😉
Just for documentation purposes: The other bug was that the check for the mutual exclusive usage of schema-type and schema-path only works if a valid choice for schema-type is provided, otherwise an error is thrown that schema-type is not one of (default, custom, strict). But this is an "issue" with argparse.
That's not even a problem of the library, that's just the way JSON Schema is specified. A completely empty JSON object is a valid schema according to the spec. Also, there are no restrictions on additional properties, you can put anything in a schema (probably to support custom extensions).
MERGE!!!
argparse is far from perfect. That may be of the areas where it could be slightly improved. On the other hand, I imagine it could be very hard to separate between multiple errors in the CLI where each one is legitimate in their own right and simple follow-up errors where you really only have to fix the first and the rest falls into place. |

The mutual exclusivity implemented through argparse's
add_mutually_exclusive_group()method has a subtle and undocumented bug (or call it an unexpected behavior, if you like) that affects options with default values.If an option with a default value is part of such a group, the option doesn't count as "present on the command-line" if the user explicitly passed its default value.
So in our case,
--schema-typeand--schema-pathwere meant to be mutually exclusive but--schema-typehas a default value ofdefault. So, this invocation correctly raises an error:but this invocation does not:
because for options with default values, argparse can't tell the difference between
This PR fixes this minor problem by forgoing argparse's built-in default mechanism and replacing it with a hand-coded fallback to the default value in the
invoke_validate()function.It includes a few other minor housekeeping changes:
strictschema type to documentation--schema-pathoption a proper Path-typed object