private static void AddSchema(JSchemaPreloadedResolver resolver, string schemaFileName, string id)
        {
            string baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
            string baseRemotePath = Path.Combine(baseDirectory, Path.Combine("Specs", "remotes"));

            string json = File.ReadAllText(Path.Combine(baseRemotePath, schemaFileName));

            JSchema schema = JSchema.Parse(json);

            resolver.Add(new Uri(id), schema.ToString());
        }
        private static async Task IsValidAgainstJsonSchemaAsync(HttpResponseMessage response)
        {
            var swaggerJson = await response.Content.ReadAsStringAsync();

            var resolver = new JSchemaPreloadedResolver();
            resolver.Add(new Uri("http://json-schema.org/draft-04/schema"), File.ReadAllText(@"schema-draft-v4.json"));

            var swaggerSchema = File.ReadAllText(@"swagger-2.0-schema.json");
            var schema = JSchema.Parse(swaggerSchema, resolver);

            var swaggerJObject = JObject.Parse(swaggerJson);
            IList<string> messages;
            var isValid = swaggerJObject.IsValid(schema, out messages);
            isValid.Should().BeTrue();
        }
        private static JSchemaPreloadedResolver GetResolver()
        {
            if (_resolver == null)
            {
                var resolver = new JSchemaPreloadedResolver();
                AddSchema(resolver, "draft3.json", "http://json-schema.org/draft-03/schema");
                AddSchema(resolver, "draft4.json", "http://json-schema.org/draft-04/schema");
                AddSchema(resolver, "integer.json", "http://localhost:1234/integer.json");
                AddSchema(resolver, "folder/folderInteger.json", "http://localhost:1234/folder/folderInteger.json");
                AddSchema(resolver, "subSchemas.json", "http://localhost:1234/subSchemas.json");

                _resolver = resolver;
            }

            return _resolver;
        }
        public void ExtendedComplex()
        {
            string first = @"{
  ""id"":""first"",
  ""type"":""object"",
  ""properties"":
  {
    ""firstproperty"":{""type"":""string""},
    ""secondproperty"":{""type"":""string"",""maxLength"":10},
    ""thirdproperty"":{
      ""type"":""object"",
      ""properties"":
      {
        ""thirdproperty_firstproperty"":{""type"":""string"",""maxLength"":10,""minLength"":7}
      }
    }
  },
  ""additionalProperties"":{}
}";

            string second = @"{
  ""id"":""second"",
  ""type"":""object"",
  ""extends"":{""$ref"":""first""},
  ""properties"":
  {
    ""secondproperty"":{""type"":""any""},
    ""thirdproperty"":{
      ""extends"":{
        ""properties"":
        {
          ""thirdproperty_firstproperty"":{""maxLength"":9,""minLength"":6,""pattern"":""hi2u""}
        },
        ""additionalProperties"":{""maxLength"":9,""minLength"":6,""enum"":[""one"",""two""]}
      },
      ""type"":""object"",
      ""properties"":
      {
        ""thirdproperty_firstproperty"":{""pattern"":""hi""}
      },
      ""additionalProperties"":{""type"":""string"",""enum"":[""two"",""three""]}
    },
    ""fourthproperty"":{""type"":""string""}
  },
  ""additionalProperties"":false
}";

            JSchemaPreloadedResolver resolver = new JSchemaPreloadedResolver();
            JSchema firstSchema = JSchema.Parse(first);
            resolver.Add(firstSchema.Id, firstSchema.ToString());

            JSchema secondSchema = JSchema.Parse(second, resolver);

            string json = @"{
  'firstproperty':'blahblahblahblahblahblah',
  'secondproperty':'secasecasecasecaseca',
  'thirdproperty':{
    'thirdproperty_firstproperty':'aaa',
    'additional':'three'
  }
}";

            SchemaValidationEventArgs validationEventArgs = null;
            List<ValidationError> errors = new List<ValidationError>();

            JSchemaValidatingReader reader = new JSchemaValidatingReader(new JsonTextReader(new StringReader(json)));
            reader.ValidationEventHandler += (sender, args) =>
            {
                validationEventArgs = args;
                errors.Add(validationEventArgs.ValidationError);
            };
            reader.Schema = secondSchema;

            Assert.IsTrue(reader.Read());
            Assert.AreEqual(JsonToken.StartObject, reader.TokenType);

            Assert.IsTrue(reader.Read());
            Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
            Assert.AreEqual("firstproperty", reader.Value.ToString());
            StringAssert.AreEqual(@"firstproperty", errors[0].Path);
            StringAssert.AreEqual(@"Property 'firstproperty' has not been defined and the schema does not allow additional properties. Path 'firstproperty', line 2, position 19.", errors[0].BuildExtendedMessage());
            Assert.AreEqual("firstproperty", errors[0].Value);

            Assert.IsTrue(reader.Read());
            Assert.AreEqual(JsonToken.String, reader.TokenType);
            Assert.AreEqual("blahblahblahblahblahblah", reader.Value.ToString());

            Assert.IsTrue(reader.Read());
            Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
            Assert.AreEqual("secondproperty", reader.Value.ToString());

            Assert.IsTrue(reader.Read());
            Assert.AreEqual(JsonToken.String, reader.TokenType);
            Assert.AreEqual("secasecasecasecaseca", reader.Value.ToString());
            Assert.AreEqual(1, errors.Count);

            Assert.IsTrue(reader.Read());
            Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
            Assert.AreEqual("thirdproperty", reader.Value.ToString());
            Assert.AreEqual(1, errors.Count);

            Assert.IsTrue(reader.Read());
            Assert.AreEqual(JsonToken.StartObject, reader.TokenType);
            Assert.AreEqual(1, errors.Count);

            Assert.IsTrue(reader.Read());
            Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
            Assert.AreEqual("thirdproperty_firstproperty", reader.Value.ToString());
            Assert.AreEqual(1, errors.Count);

            Assert.IsTrue(reader.Read());
            Assert.AreEqual(JsonToken.String, reader.TokenType);
            Assert.AreEqual("aaa", reader.Value.ToString());
            Assert.AreEqual(2, errors.Count);
            StringAssert.AreEqual(@"thirdproperty.thirdproperty_firstproperty", errors[1].Path);
            StringAssert.AreEqual(@"String 'aaa' does not match regex pattern 'hi'. Path 'thirdproperty.thirdproperty_firstproperty', line 5, position 40.", errors[1].BuildExtendedMessage());
            Assert.AreEqual("aaa", errors[1].Value);

            Assert.IsTrue(reader.Read());
            Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
            Assert.AreEqual("additional", reader.Value.ToString());
            Assert.AreEqual(2, errors.Count);

            Assert.IsTrue(reader.Read());
            Assert.AreEqual(JsonToken.String, reader.TokenType);
            Assert.AreEqual("three", reader.Value.ToString());
            Assert.AreEqual(2, errors.Count);

            Assert.IsTrue(reader.Read());
            Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
            Assert.AreEqual(3, errors.Count);
            StringAssert.AreEqual(@"thirdproperty", errors[2].Path);
            StringAssert.AreEqual(@"JSON does not match all schemas from 'allOf'. Invalid schema indexes: 0. Path 'thirdproperty', line 7, position 4.", errors[2].BuildExtendedMessage());
            Assert.AreEqual(null, errors[2].Value);
            Assert.AreEqual(4, errors[2].ChildErrors.Count);
            StringAssert.AreEqual(@"thirdproperty.thirdproperty_firstproperty", errors[2].ChildErrors[0].Path);
            StringAssert.AreEqual(@"String 'aaa' is less than minimum length of 6. Path 'thirdproperty.thirdproperty_firstproperty', line 5, position 40.", errors[2].ChildErrors[0].BuildExtendedMessage());

            Assert.IsTrue(reader.Read());
            Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
            Assert.AreEqual(4, errors.Count);
            StringAssert.AreEqual(@"", errors[3].Path);
            StringAssert.AreEqual(@"JSON does not match all schemas from 'allOf'. Invalid schema indexes: 0. Path '', line 8, position 2.", errors[3].BuildExtendedMessage());
            Assert.AreEqual(null, errors[3].Value);
            Assert.AreEqual(2, errors[3].ChildErrors.Count);

            Assert.IsFalse(reader.Read());
            Assert.AreEqual(4, errors.Count);
        }
        private JSchema GetExtendedSchema()
        {
            string first = @"{
  ""type"":""object"",
  ""properties"":
  {
    ""firstproperty"":{""type"":""string"",""required"":true}
  },
  ""additionalProperties"":{}
}";

            string second = @"{
  ""id"":""second"",
  ""type"":""object"",
  ""extends"":{""$ref"":""first""},
  ""properties"":
  {
    ""secondproperty"":{""type"":""string"",""required"":true}
  },
  ""additionalProperties"":false
}";

            JSchemaPreloadedResolver resolver = new JSchemaPreloadedResolver();
            JSchema firstSchema = JSchema.Parse(first);

            resolver.Add(new Uri("first", UriKind.RelativeOrAbsolute), firstSchema.ToString());

            JSchema secondSchema = JSchema.Parse(second, resolver);

            return secondSchema;
        }
        public void JSchemaPreloadedResolver()
        {
#region JSchemaPreloadedResolver
            string addressSchemaJson = @"{
              'type': 'object',
              'properties': {
                'line1': {'type': 'string'},
                'line2': {'type': 'string'},
                'country': {'type': 'string'}
              }
            }";

            // preload schema with ID 'http://schema.org/address.json'
            JSchemaPreloadedResolver resolver = new JSchemaPreloadedResolver();
            resolver.Add(new Uri("http://schema.org/address.json"), addressSchemaJson);

            // the external ref will use the preloaded schema
            JSchema schema = JSchema.Parse(@"{
              'type': 'object',
              'properties': {
                'name': {'type':'string'},
                'addresses': {
                  'type': 'array',
                  'items': {'$ref': 'http://schema.org/address.json'}
                }
              }
            }", resolver);

            JToken json = JToken.Parse(@"{
              'name': 'James',
              'addresses': [
                {
                  'line1': '99 Smithington Street',
                  'line2': 'Real Town',
                  'Country': 'USA'
                }
              ]
            }");

            IList<string> errorMessages;
            bool isValid = json.IsValid(schema, out errorMessages);
            #endregion
        }