Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request : allow me to specify to JSON.NET Schema to use the JSchemaReaderSettings "BaseUri" property (instead of "$id") #222

Open
marcscheuner-bfh opened this issue Jul 13, 2020 · 0 comments

Comments

@marcscheuner-bfh
Copy link

marcscheuner-bfh commented Jul 13, 2020

I'm using JSON.NET Schema (v3.0.10) to validate some JSON from an API.

My project-response.json schema file looks something like this:

{
  "$id": "http://www.whereever.com/services/projects/project-response.json",
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
	....
	"lead": {
      "description": "The lead person",
      "$ref": "common-types.json#/definitions/personType"
    },
    ....
  }
}

Among other things, it contains references to the common-types.json file in the same directory, that defines some basic types used by various parts of the schema - something like this:

{
  "$id": "http://www.whereever.com/services/projects/common-types.json",
  "$schema": "http://json-schema.org/draft-07/schema#",
  "definitions": {
     ....
    "personType": {
      "type": "object",
      "properties": {
        "firstname": {
          "type": "string",
          "minLength": 1,
          "maxLength": 64
        },
        "lastname": {
          "type": "string",
          "minLength": 1,
          "maxLength": 64
        },
        "email": {
          "type": "string",
          "minLength": 1,
          "maxLength": 256
        }
      },
      "required": [
        "firstname",
        "lastname"
      ]
    }, 

I get a JSON string as the response from API call, and I'd like to validate this JSON against this schema. I have tried to do it like this:

public bool Validate(string jsonData)
{
    string jsonSchemaFileName = Path.Combine(_schemaBaseDirectory, "project-response.json");
    
    // define a Json Text Reader for the schema
    using (StreamReader schemaFile = File.OpenText(jsonSchemaFileName))
    using (JsonTextReader reader = new JsonTextReader(schemaFile))
    {
        JSchemaUrlResolver resolver = new JSchemaUrlResolver();

        JSchemaReaderSettings settings = new JSchemaReaderSettings
                                                 {
                                                     Resolver = resolver,
                                                     BaseUri = new Uri(jsonSchemaFileName)
                                                 };

        JSchema schema = JSchema.Load(jsonReader, settings);

        // validate
        JToken jsonToken = JObject.Parse(jsonData);

        bool isValid = jsonToken.IsValid(schema, out IList<string> errors);

        return isValid;
    }
}

I was expecting that the JSON.NET Schema system would be able to now read the common-types.json from the same location where the project-response.json is located and look up the definitions in that common file - but alas, on the line

JSchema schema = JSchema.Load(jsonReader, settings);

I keep getting an error:

Newtonsoft.Json.Schema.JSchemaReaderException
HResult=0x80131500
Message=Error when resolving schema reference 'common-types.json#/definitions/projectIdType'. Path 'properties.id', line 7, position 11.

Inner Exception 1: WebException: The remote server returned an error: (404) Not Found.

It appears as if the "$id" in the JSON schema file(s) "overrides" the "JSchemaReaderSettings.BaseUri" - which in my case is very unfortunate.

Any hope that there might be an additional setting that would allow me to specify that I want JSON.NET Schema to use the "BaseUri" as defined in the settings, rather than the JSON schema file's "$id" property's URL ?

Thanks!
Marc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant