Skip to content

Nested Conditional Properties for Local file references not working in python-jsonschema #808

@dibdutta

Description

@dibdutta

My base.schema.json looks like below

{"$schema": "http://json-schema.org/draft-07/schema#" }

My defination.schema.json looks like below

{
  "$id": "defination.schema.json",
  "type": "object",
  "properties": {
    "remote_os": {
      "default": "Linux",
      "enum": [
        "Linux",
        "Windows"
      ]
    }
  },
  "required": [
    "remote_os"
  ]
}

Now referenced the definition in update.schema.json

{
  "$id": "update.schema.json",
  "$schema": "http://json-schema.org/draft-07/schema",
  "if": {
    "type": "object",
    "required": [
      "common_data"
    ],
    "properties": {
      "common_data": {
        "$ref": "defination.schema.json",
        "properties": {
          "remote_os": {
            "const": "Linux"
          }
        }
      }
    }
  },
  "then": {
    "type": "object",
    "properties": {
      "dup_file": {
        "type": "string",
        "pattern": "^(.*.)(bin)$"
      }
    }
  },
  "else": {
    "type": "object",
    "properties": {
      "dup_file": {
        "type": "string",
        "pattern": "^(.*.)(exe)$"
      }
    }
  }
}

Basically added condition in JSON schema if {"common_data":{"remote_os":"Windows"}} then "dup_file" name should ended up with ".exe" else ".bin"
Now my RefResolver and Validator script is

from jsonschema import RefResolver
from jsonschema.validators import validator_for
import json

base = json.loads(open('base.schema.json', "r").read())
definitions =json.loads(open('defination.schema.json', "r").read()) # https://jsonschema.dev/s/FZDbO
schema =json.loads(open('update.schema.json', "r").read()) # https://jsonschema.dev/s/lvLFa

schema_store = {
  base.get('$id','base.schema.json') : base,
  definitions.get('$id','defination.schema.json') : definitions,
  schema.get('$id','update.schema.json') : schema,
}

resolver = RefResolver.from_schema(base, store=schema_store)
Validator = validator_for(base)
validator = Validator(schema, resolver=resolver)

data = {
    "common_data": {
        "remote_os": "Windows"
    },
    "dup_file": "ad7a.exe"
}
validator.validate(data)

Getting error : jsonschema.exceptions.ValidationError: 'ad7a.exe' does not match '^(.*.)(bin)$' ,which is wrong.
Since in my validation data {"common_data":{"remote_os":"Windows"}} and "dup_file": "ad7a.exe" , it should not raise any validation error ideally .

Same thing I have tested https://json-schema.hyperjump.io/ it working perfectly fine , suspecting some issue with python-jsonschema only. Any help on this highly appreciated .

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