python-jsonschema / check-jsonschema

A CLI and set of pre-commit hooks for jsonschema validation with built-in support for GitHub Workflows, Renovate, Azure Pipelines, and more!
https://check-jsonschema.readthedocs.io/en/stable
Other
190 stars 39 forks source link

OpenAPI format, `required` plus `minimum`/`maximum` fields #449

Open danmoser opened 4 days ago

danmoser commented 4 days ago

I'm trying to validate a schema generated my OpenAPI and check-jsonschema is not working with it. In particular, I need to check required plus minimum/maximum fields.

This is the schemafile:

{
  "components": {
    "schemas": {
      "my_data": {
        "type": "object",
        "properties": {
          "longitude": {
            "type": "number",
            "format": "decimal-degrees +/- DDD.D",
            "example": -107.6177275,
            "description": "the longitude of the observatory/instrument",
            "minimum": -180,
            "maximum": 180
          }
        },
        "required": ["longitude"]
      }
    }
  }
}

This is the instance an example of instance file: {"latitude":190.0}.

Currently check-jsonschema is returning ok -- validation done

sirosen commented 4 days ago

This sounds to me like an understanding issue, rather than a tool issue. That file doesn't contain a schema at the root, which means that it's effectively equivalent to the schema {}, and passes on any document.

The fastest, easiest path to get the validation you're looking for is to pull the relevant schema from $.components.schemas.my_data into a distinct file and use that for validation.

I'd be happy to talk about tool enhancements to make this sort of workflow easier, but I want to be careful that any discussion starts from a common understanding of which part of the given document is a schema.[^1] I'll also note, as a warning about a potential issue, that openapi spec extends JSON Schema in several ways, not all of which are easy to support.

[^1]: relatedly, thanks for including a sample document in this issue! It makes it much easier to discuss.