Skip to content

Unexpected behaviour applying minimum clause to missing property #670

Closed
@alsyia

Description

@alsyia

Hi!

I'm seeing a somewhat weird behaviour when I try to apply a conditional minimum clause to a message where the minimum clause subject is missing.

For example:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
      "amount": {
          "type": "number"
      },
      "sometimesOptional": {
        "type": "string"
      }
  },
  "if": {
      "properties": {
          "amount": {
              "minimum": 1
          }
      }
  },
  "then": {
      "required": ["sometimesOptional"]
  }
}

Applying this schema to an empty JSON doc ({}) raises the following error:

jsonschema.exceptions.ValidationError: 'sometimesOptional' is a required property

Failed validating 'required' in schema['if']['then']:
    {'required': ['sometimesOptional']}

On instance:
    {}

Is this expected? It looks like in the absence of amount the if/minimum clause is considered valid. I checked the spec but could not find anything related to this particular edge case. I'm not sure what the correct behaviour is as I'm not very familiar with JSON schemas. In classic python or javascript testing if None or undefined was greater than 1 would raise an error... Anyway, I thought I might open an issue to know if it's a bug or just a quirk of schemas :)

As a side note, I think it worked in jsonschema 2.6.0 because I've had issues only since I upgraded to 3.2.0.

For now, as a fix, I added "required": ["amount"] in my if clause so that it's only enforced if the attribute is present.

Looking into the code of 3.2.0 it looks logical at least: the minimum validator returns None because the attribute is not found and I think the if validator checks if there was an error validating the schema so...

Thank you for your time and this amazing lib! :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions