The JSON Schema draft specification describes the use of the $schema keyword "as a JSON Schema version identifier and the location of a resource which is itself a JSON Schema." However, when I attempted to verify the draft v3 and v4 support I was unable to see the $schema value being considered. As an example, consider the below test fixture.
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Schema;
using NUnit.Framework;
using System.Collections.Generic;
using System.IO;
namespace JsonSchemaExamples
{
[TestFixture]
public class ValidatorPlay
{
#region schema definitions
private const string myV4Schema = @"{
'$schema': 'http://json-schema.org/draft-04/schema#',
'type': 'object',
'properties':
{
'first_name': { 'type': 'string' },
'address':
{
'type': 'object',
'properties':
{
'street_address': { 'type': 'string' },
'state': { 'type': 'string' }
}
}
},
'required' : ['first_name']
}";
private const string myV3Schema = @"{
'$schema': 'http://json-schema.org/draft-03/schema#',
'type': 'object',
'properties':
{
'first_name':
{
'type': 'string',
'required': true
},
'address':
{
'type': 'object',
'properties':
{
'street_address': { 'type': 'string' },
'state': { 'type': 'string' }
}
}
}
}";
#endregion
#region json to validate
private JObject validPerson = JObject.Parse(@"{
'first_name' : 'Mark',
'age': 32,
'address' :
{
'street': '1 Main Street',
'postcode': 'AB12 0CD'
}
}");
private JObject invalidPerson = JObject.Parse(@"{
'Name' : 'Mark',
'age': 32,
'address' :
{
'street': '1 Main Street',
'postcode': 'AB12 0CD'
}
}");
#endregion
[TestCase(myV4Schema)]
[TestCase(myV3Schema)]
public void CheckDraftSupport(string schemaToUse)
{
JSchemaUrlResolver resolver = new JSchemaUrlResolver();
JSchema schema = JSchema.Parse(schemaToUse, resolver);
Assert.That(validPerson.IsValid(schema));
Assert.That(!invalidPerson.IsValid(schema));
}
}
}
If I change the $schema values within myV4Schema to point to v3 or vice versa with myV3Schema both my tests continue to pass. I would have expected them to fail and report a malformed schema (due to the required attribute).
What have I misunderstood or what am I doing wrong?
Validation that a schema meets the spec using "$schema" will come soon in a future version. Right now both V3 and V4 are always supported.
Update: $schema is used in the latest version on NuGet.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With