You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
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
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
The text was updated successfully, but these errors were encountered:
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: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: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:
I was expecting that the JSON.NET Schema system would be able to now read the
common-types.json
from the same location where theproject-response.json
is located and look up the definitions in that common file - but alas, on the lineI keep getting an error:
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
The text was updated successfully, but these errors were encountered: