How to set up local file references in python-jsonschema document?

后端 未结 5 2044
失恋的感觉
失恋的感觉 2021-01-02 08:28

I have a set of jsonschema compliant documents. Some documents contain references to other documents (via the $ref attribute). I do not wish to host these docum

5条回答
  •  盖世英雄少女心
    2021-01-02 09:04

    Following up on the answer @chris-w provided, I wanted to do this same thing with jsonschema 3.2.0 but his answer didn't quite cover it I hope this answer helps those who are still coming to this question for help but are using a more recent version of the package.

    To extend a JSON schema using the library, do the following:

    1. Create the base schema:
    base.schema.json
    {
      "$id": "base.schema.json",
      "type": "object",
      "properties": {
        "prop": {
          "type": "string"
        }
      },
      "required": ["prop"]
    }
    
    1. Create the extension schema
    extend.schema.json
    {
      "allOf": [
        {"$ref": "base.schema.json"},
        {
          "properties": {
            "extra": {
              "type": "boolean"
            }
          },
          "required": ["extra"]
        }
      ]
    }
    
    1. Create your JSON file you want to test against the schema
    data.json
    {
      "prop": "This is the property",
      "extra": true
    }
    
    1. Create your RefResolver and Validator for the base Schema and use it to check the data
    #Set up schema, resolver, and validator on the base schema
    baseSchema = json.loads(baseSchemaJSON) # Create a schema dictionary from the base JSON file
    relativeSchema = json.loads(relativeJSON) # Create a schema dictionary from the relative JSON file
    resolver = RefResolver.from_schema(baseSchema) # Creates your resolver, uses the "$id" element
    validator = Draft7Validator(relativeSchema, resolver=resolver) # Create a validator against the extended schema (but resolving to the base schema!)
    
    # Check validation!
    data = json.loads(dataJSON) # Create a dictionary from the data JSON file
    validator.validate(data)
    

    You may need to make a few adjustments to the above entries, such as not using the Draft7Validator. This should work for single-level references (children extending a base), you will need to be careful with your schemas and how you set up the RefResolver and Validator objects.

    P.S. Here is a snipped that exercises the above. Try modifying the data string to remove one of the required attributes:

    import json
    
    from jsonschema import RefResolver, Draft7Validator
    
    base = """
    {
      "$id": "base.schema.json",
      "type": "object",
      "properties": {
        "prop": {
          "type": "string"
        }
      },
      "required": ["prop"]
    }
    """
    
    extend = """
    {
      "allOf": [
        {"$ref": "base.schema.json"},
        {
          "properties": {
            "extra": {
              "type": "boolean"
            }
          },
          "required": ["extra"]
        }
      ]
    }
    """
    
    data = """
    {
    "prop": "This is the property string",
    "extra": true
    }
    """
    
    schema = json.loads(base)
    extendedSchema = json.loads(extend)
    resolver = RefResolver.from_schema(schema)
    validator = Draft7Validator(extendedSchema, resolver=resolver)
    
    jsonData = json.loads(data)
    validator.validate(jsonData)
    

提交回复
热议问题