Example #1
0
        public void StringDisallowed()
        {
            string schemaJson = @"{
  ""type"":""array"",
  ""items"":{
    ""disallow"":[""number""]
  },
  ""maxItems"":3
}";

            string json = "['pie',1.1]";

            Json.Schema.ValidationEventArgs validationEventArgs = null;

            JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));

            reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
            reader.Schema = JsonSchema.Parse(schemaJson);

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

            Assert.IsTrue(reader.Read());
            Assert.AreEqual(JsonToken.String, reader.TokenType);
            Assert.AreEqual(null, validationEventArgs);

            Assert.IsTrue(reader.Read());
            Assert.AreEqual(JsonToken.Float, reader.TokenType);
            Assert.AreEqual(@"Type Float is disallowed. Line 1, position 11.", validationEventArgs.Message);

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

            Assert.IsNotNull(validationEventArgs);
        }
Example #2
0
        public void NullNotInEnum()
        {
            string schemaJson = @"{
  ""type"":""array"",
  ""items"":{
    ""type"":""null"",
    ""enum"":[]
  },
  ""maxItems"":3
}";

            string json = "[null]";

            Json.Schema.ValidationEventArgs validationEventArgs = null;

            JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));

            reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
            reader.Schema = JsonSchema.Parse(schemaJson);

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

            Assert.IsTrue(reader.Read());
            Assert.AreEqual(JsonToken.Null, reader.TokenType);
            Assert.AreEqual(@"Value null is not defined in enum. Line 1, position 6.", validationEventArgs.Message);

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

            Assert.IsNotNull(validationEventArgs);
        }
Example #3
0
        public void ArrayCountLessThanMinimumItems()
        {
            string schemaJson = @"{
  ""type"":""array"",
  ""minItems"":2,
  ""maxItems"":3
}";

            string json = "[null]";

            Json.Schema.ValidationEventArgs validationEventArgs = null;

            JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));

            reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
            reader.Schema = JsonSchema.Parse(schemaJson);

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

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

            Assert.IsTrue(reader.Read());
            Assert.AreEqual(JsonToken.EndArray, reader.TokenType);
            Assert.AreEqual("Array item count 1 is less than minimum count of 2. Line 1, position 6.", validationEventArgs.Message);

            Assert.IsNotNull(validationEventArgs);
        }
        /// <summary>
        /// Verifies Json literal content
        /// </summary>
        /// <param name="content">the Json literal to be verified</param>
        /// <param name="result">output paramter of test result</param>
        /// <returns>true if verification passes; false otherwiser</returns>
        public bool Verify(string content, out TestResult result)
        {
            using (var stringReader = new StringReader(content))
            {
                using (JsonTextReader rdr = new JsonTextReader(stringReader))
                {
                    using (JsonValidatingReader vr = new JsonValidatingReader(rdr))
                    {
                        vr.Schema = this.schema;

                        try
                        {
                            while (vr.Read())
                            {
                                // Ignore
                            }

                            result = new TestResult();
                            return(true);
                        }
                        catch (JsonSchemaException jex)
                        {
                            result = new TestResult()
                            {
                                LineNumberInError = jex.LineNumber, ErrorDetail = jex.Message
                            };
                            return(false);
                        }
                    }
                }
            }
        }
Example #5
0
        public void ExtendsStringGreaterThanMaximumLength()
        {
            string schemaJson = @"{
  ""extends"":{
    ""type"":""string"",
    ""minLength"":5,
    ""maxLength"":10
  },
  ""maxLength"":9
}";

            List <string> errors = new List <string>();
            string        json   = "'The quick brown fox jumps over the lazy dog.'";

            Json.Schema.ValidationEventArgs validationEventArgs = null;

            JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));

            reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; errors.Add(validationEventArgs.Message); };
            reader.Schema = JsonSchema.Parse(schemaJson);

            Assert.IsTrue(reader.Read());
            Assert.AreEqual(JsonToken.String, reader.TokenType);
            Assert.AreEqual(1, errors.Count);
            Assert.AreEqual("String 'The quick brown fox jumps over the lazy dog.' exceeds maximum length of 9. Line 1, position 46.", errors[0]);

            Assert.IsNotNull(validationEventArgs);
        }
Example #6
0
        public void DisableAdditionalProperties()
        {
            string schemaJson = @"{
  ""description"":""A person"",
  ""type"":""object"",
  ""properties"":
  {
    ""name"":{""type"":""string""}
  },
  ""additionalProperties"":false
}";

            string json = "{'name':'James','additionalProperty1':null,'additionalProperty2':null}";

            Json.Schema.ValidationEventArgs validationEventArgs = null;

            JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));

            reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
            reader.Schema = JsonSchema.Parse(schemaJson);

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

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

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

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

            Assert.IsTrue(reader.Read());
            Assert.AreEqual(JsonToken.Null, reader.TokenType);
            Assert.AreEqual(null, reader.Value);
            Assert.AreEqual("Property 'additionalProperty1' has not been defined and the schema does not allow additional properties. Line 1, position 38.", validationEventArgs.Message);

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

            Assert.IsTrue(reader.Read());
            Assert.AreEqual(JsonToken.Null, reader.TokenType);
            Assert.AreEqual(null, reader.Value);
            Assert.AreEqual("Property 'additionalProperty2' has not been defined and the schema does not allow additional properties. Line 1, position 65.", validationEventArgs.Message);

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

            Assert.IsNotNull(validationEventArgs);
        }
Example #7
0
        public void ExtendsMissingNonoptionalProperties()
        {
            string json = "{}";

            List <string> errors = new List <string>();

            JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));

            reader.ValidationEventHandler += (sender, args) => { errors.Add(args.Message); };
            reader.Schema = GetExtendedSchema();

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

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

            Assert.AreEqual(1, errors.Count);
            Assert.AreEqual("Non-optional properties are missing from object: secondproperty, firstproperty. Line 1, position 2.", errors[0]);
        }
Example #8
0
        public void MissingNonoptionalProperties()
        {
            string schemaJson = @"{
  ""description"":""A person"",
  ""type"":""object"",
  ""properties"":
  {
    ""name"":{""type"":""string""},
    ""hobbies"":{""type"":""string""},
    ""age"":{""type"":""integer""}
  }
}";

            string json = "{'name':'James'}";

            Json.Schema.ValidationEventArgs validationEventArgs = null;

            JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));

            reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
            reader.Schema = JsonSchema.Parse(schemaJson);

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

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

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

            Assert.IsTrue(reader.Read());
            Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
            Assert.AreEqual("Non-optional properties are missing from object: hobbies, age. Line 1, position 16.", validationEventArgs.Message);

            Assert.IsNotNull(validationEventArgs);
        }
Example #9
0
        public void ThrowExceptionWhenNoValidationEventHandler()
        {
            string schemaJson = @"{
  ""type"":""integer"",
  ""maximum"":5
}";

            JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader("10")));

            reader.Schema = JsonSchema.Parse(schemaJson);

            Assert.IsTrue(reader.Read());
        }
Example #10
0
        public void MissingNonRequiredProperties()
        {
            string schemaJson = @"{
  ""description"":""A person"",
  ""type"":""object"",
  ""properties"":
  {
    ""name"":{""type"":""string"",""required"":true},
    ""hobbies"":{""type"":""string"",""required"":false},
    ""age"":{""type"":""integer""}
  }
}";

            string json = "{'name':'James'}";

            Json.Schema.ValidationEventArgs validationEventArgs = null;

            JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));

            reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
            reader.Schema = JsonSchema.Parse(schemaJson);

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

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

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

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

            Assert.IsNull(validationEventArgs);
        }
Example #11
0
        public void FloatExceedsMaxDecimalPlaces()
        {
            string schemaJson = @"{
  ""type"":""array"",
  ""items"":{
    ""type"":""number"",
    ""maxDecimal"":2
  }
}";

            string json = "[1.1,2.2,4.001]";

            Json.Schema.ValidationEventArgs validationEventArgs = null;

            JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));

            reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
            reader.Schema = JsonSchema.Parse(schemaJson);

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

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

            Assert.IsTrue(reader.Read());
            Assert.AreEqual(JsonToken.Float, reader.TokenType);
            Assert.AreEqual(null, validationEventArgs);

            Assert.IsTrue(reader.Read());
            Assert.AreEqual(JsonToken.Float, reader.TokenType);
            Assert.AreEqual(@"Float 4.001 exceeds the maximum allowed number decimal places of 2. Line 1, position 15.", validationEventArgs.Message);

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

            Assert.IsNotNull(validationEventArgs);
        }
Example #12
0
 public static void Validate(this JToken source, JsonSchema schema, ValidationEventHandler validationEventHandler)
 {
     ValidationUtils.ArgumentNotNull(source, "source");
     ValidationUtils.ArgumentNotNull(schema, "schema");
     using (JsonValidatingReader jsonValidatingReader = new JsonValidatingReader(source.CreateReader()))
     {
         jsonValidatingReader.Schema = schema;
         if (validationEventHandler != null)
         {
             jsonValidatingReader.ValidationEventHandler += validationEventHandler;
         }
         while (jsonValidatingReader.Read())
         {
         }
     }
 }
Example #13
0
    /// <summary>
    /// Validates the specified <see cref="JToken"/>.
    /// </summary>
    /// <param name="source">The source <see cref="JToken"/> to test.</param>
    /// <param name="schema">The schema to test with.</param>
    /// <param name="validationEventHandler">The validation event handler.</param>
    public static void Validate(this JToken source, JsonSchema schema, ValidationEventHandler validationEventHandler)
    {
      ValidationUtils.ArgumentNotNull(source, "source");
      ValidationUtils.ArgumentNotNull(schema, "schema");

      using (JsonValidatingReader reader = new JsonValidatingReader(source.CreateReader()))
      {
        reader.Schema = schema;
        if (validationEventHandler != null)
          reader.ValidationEventHandler += validationEventHandler;

        while (reader.Read())
        {
        }
      }
    }
Example #14
0
 public static void Validate(this JToken source, JsonSchema schema, ValidationEventHandler validationEventHandler)
 {
     Class203.smethod_2(source, "source");
     Class203.smethod_2(schema, "schema");
     using (JsonValidatingReader reader = new JsonValidatingReader(source.CreateReader()))
     {
         reader.Schema = schema;
         if (validationEventHandler != null)
         {
             reader.ValidationEventHandler += validationEventHandler;
         }
         while (reader.Read())
         {
         }
     }
 }
Example #15
0
        internal static void Validate(this JToken source, JsonSchema schema, ValidationEventHandler validationEventHandler)
        {
            ValidationUtils.ArgumentNotNull(source, nameof(source));
            ValidationUtils.ArgumentNotNull(schema, nameof(schema));

            using (JsonValidatingReader reader = new JsonValidatingReader(source.CreateReader()))
            {
                reader.Schema = schema;
                if (validationEventHandler != null)
                {
                    reader.ValidationEventHandler += validationEventHandler;
                }

                while (reader.Read())
                {
                }
            }
        }
Example #16
0
        public void FloatLessThanMinimumValue()
        {
            string schemaJson = @"{
  ""type"":""number"",
  ""minimum"":5
}";

            string json = "1.1";

            Json.Schema.ValidationEventArgs validationEventArgs = null;

            JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));

            reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
            reader.Schema = JsonSchema.Parse(schemaJson);

            Assert.IsTrue(reader.Read());
            Assert.AreEqual(JsonToken.Float, reader.TokenType);
            Assert.AreEqual("Float 1.1 is less than minimum value of 5. Line 1, position 3.", validationEventArgs.Message);

            Assert.IsNotNull(validationEventArgs);
        }
Example #17
0
        public void StringDoesNotMatchPattern()
        {
            string schemaJson = @"{
  ""type"":""string"",
  ""pattern"":""foo""
}";

            string json = "'The quick brown fox jumps over the lazy dog.'";

            Json.Schema.ValidationEventArgs validationEventArgs = null;

            JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));

            reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
            reader.Schema = JsonSchema.Parse(schemaJson);

            Assert.IsTrue(reader.Read());
            Assert.AreEqual(JsonToken.String, reader.TokenType);
            Assert.AreEqual("String 'The quick brown fox jumps over the lazy dog.' does not match regex pattern 'foo'. Line 1, position 46.", validationEventArgs.Message);

            Assert.IsNotNull(validationEventArgs);
        }
Example #18
0
        public void IntegerGreaterThanMaximumValue()
        {
            string schemaJson = @"{
  ""type"":""integer"",
  ""maximum"":5
}";

            string json = "10";

            Json.Schema.ValidationEventArgs validationEventArgs = null;

            JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));

            reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
            reader.Schema = JsonSchema.Parse(schemaJson);

            Assert.IsTrue(reader.Read());
            Assert.AreEqual(JsonToken.Integer, reader.TokenType);
            Assert.AreEqual("Integer 10 exceeds maximum value of 5. Line 1, position 2.", validationEventArgs.Message);

            Assert.IsNotNull(validationEventArgs);
        }
Example #19
0
        public void StringLessThanMinimumLength()
        {
            string schemaJson = @"{
  ""type"":""string"",
  ""minLength"":5,
  ""maxLength"":50,
}";

            string json = "'pie'";

            Json.Schema.ValidationEventArgs validationEventArgs = null;

            JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));

            reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
            reader.Schema = JsonSchema.Parse(schemaJson);

            Assert.IsTrue(reader.Read());
            Assert.AreEqual(JsonToken.String, reader.TokenType);
            Assert.AreEqual("String 'pie' is less than minimum length of 5. Line 1, position 5.", validationEventArgs.Message);

            Assert.IsNotNull(validationEventArgs);
        }
Example #20
0
        public void InvalidDataType()
        {
            string schemaJson = @"{
  ""type"":""string"",
  ""minItems"":2,
  ""maxItems"":3,
  ""items"":{}
}";

            string json = "[null,null,null,null]";

            Json.Schema.ValidationEventArgs validationEventArgs = null;

            JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));

            reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
            reader.Schema = JsonSchema.Parse(schemaJson);

            Assert.IsTrue(reader.Read());
            Assert.AreEqual(JsonToken.StartArray, reader.TokenType);
            Assert.AreEqual(@"Invalid type. Expected String but got Array. Line 1, position 1.", validationEventArgs.Message);

            Assert.IsNotNull(validationEventArgs);
        }
Example #21
0
        public void sdfsdf()
        {
            string schemaJson = @"{
  ""type"":""array"",
  ""items"": [{""type"":""string""},{""type"":""integer""}],
  ""additionalProperties"": false
}";

            string json = @"[1, 'a', null]";

            Json.Schema.ValidationEventArgs validationEventArgs = null;

            JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));

            reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
            reader.Schema = JsonSchema.Parse(schemaJson);

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

            Assert.IsTrue(reader.Read());
            Assert.AreEqual(JsonToken.Integer, reader.TokenType);
            Assert.AreEqual("Invalid type. Expected String but got Integer. Line 1, position 3.", validationEventArgs.Message);

            Assert.IsTrue(reader.Read());
            Assert.AreEqual(JsonToken.String, reader.TokenType);
            Assert.AreEqual("Invalid type. Expected Integer but got String. Line 1, position 7.", validationEventArgs.Message);

            Assert.IsTrue(reader.Read());
            Assert.AreEqual(JsonToken.Null, reader.TokenType);
            Assert.AreEqual("Index 3 has not been defined and the schema does not allow additional items. Line 1, position 14.", validationEventArgs.Message);

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

            Assert.IsFalse(reader.Read());
        }
    public void IntValidForNumber()
    {
      string schemaJson = @"{
  ""type"":""array"",
  ""items"":{
    ""type"":""number""
  }
}";

      string json = "[1]";

      Json.Schema.ValidationEventArgs validationEventArgs = null;

      JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
      reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
      reader.Schema = JsonSchema.Parse(schemaJson);

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

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

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

      Assert.IsNull(validationEventArgs);
    }
    public void StringDoesNotMatchPattern()
    {
      string schemaJson = @"{
  ""type"":""string"",
  ""pattern"":""foo""
}";

      string json = "'The quick brown fox jumps over the lazy dog.'";

      Json.Schema.ValidationEventArgs validationEventArgs = null;

      JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
      reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
      reader.Schema = JsonSchema.Parse(schemaJson);

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.String, reader.TokenType);
      Assert.AreEqual("String 'The quick brown fox jumps over the lazy dog.' does not match regex pattern 'foo'. Line 1, position 46.", validationEventArgs.Message);
      Assert.AreEqual("", validationEventArgs.Path);

      Assert.IsNotNull(validationEventArgs);
    }
    public void StringLessThanMinimumLength()
    {
      string schemaJson = @"{
  ""type"":""string"",
  ""minLength"":5,
  ""maxLength"":50,
}";

      string json = "'pie'";

      Json.Schema.ValidationEventArgs validationEventArgs = null;

      JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
      reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
      reader.Schema = JsonSchema.Parse(schemaJson);

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.String, reader.TokenType);
      Assert.AreEqual("String 'pie' is less than minimum length of 5. Line 1, position 5.", validationEventArgs.Message);

      Assert.IsNotNull(validationEventArgs);
    }
    public void ReadAsInt32InArray()
    {
      string schemaJson = @"{
  ""type"":""array"",
  ""items"":{
    ""type"":""integer""
  },
  ""maxItems"":1
}";

      string json = "[1,2]";

      Json.Schema.ValidationEventArgs validationEventArgs = null;

      JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
      reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
      reader.Schema = JsonSchema.Parse(schemaJson);

      reader.Read();
      Assert.AreEqual(JsonToken.StartArray, reader.TokenType);

      reader.ReadAsInt32();
      Assert.AreEqual(JsonToken.Integer, reader.TokenType);
      Assert.AreEqual(null, validationEventArgs);

      reader.ReadAsInt32();
      Assert.AreEqual(JsonToken.Integer, reader.TokenType);
      Assert.AreEqual(null, validationEventArgs);

      reader.ReadAsInt32();
      Assert.AreEqual(JsonToken.EndArray, reader.TokenType);
      Assert.AreEqual("Array item count 2 exceeds maximum count of 1. Line 1, position 5.", validationEventArgs.Message);
      Assert.AreEqual("", validationEventArgs.Path);
    }
    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
}";

      JsonSchemaResolver resolver = new JsonSchemaResolver();
      JsonSchema firstSchema = JsonSchema.Parse(first, resolver);
      JsonSchema secondSchema = JsonSchema.Parse(second, resolver);

      JsonSchemaModelBuilder modelBuilder = new JsonSchemaModelBuilder();

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

      Json.Schema.ValidationEventArgs validationEventArgs = null;
      List<string> errors = new List<string>();

      JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
      reader.ValidationEventHandler += (sender, args) =>
        {
          validationEventArgs = args;
          errors.Add(validationEventArgs.Path + " - " + validationEventArgs.Message);
        };
      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());
      Assert.AreEqual(null, validationEventArgs);

      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.AreEqual("secondproperty - String 'secasecasecasecaseca' exceeds maximum length of 10. Line 3, position 42.", errors[0]);

      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(4, errors.Count);
      Assert.AreEqual("thirdproperty.thirdproperty_firstproperty - String 'aaa' is less than minimum length of 7. Line 5, position 40.", errors[1]);
      Assert.AreEqual("thirdproperty.thirdproperty_firstproperty - String 'aaa' does not match regex pattern 'hi'. Line 5, position 40.", errors[2]);
      Assert.AreEqual("thirdproperty.thirdproperty_firstproperty - String 'aaa' does not match regex pattern 'hi2u'. Line 5, position 40.", errors[3]);

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

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.String, reader.TokenType);
      Assert.AreEqual("three", reader.Value.ToString());
      Assert.AreEqual(5, errors.Count);
      Assert.AreEqual("thirdproperty.additional - String 'three' is less than minimum length of 6. Line 6, position 25.", errors[4]);

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

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

      Assert.IsFalse(reader.Read());
    }
    public void PatternPropertiesNoAdditionalProperties()
    {
      string schemaJson = @"{
  ""type"":""object"",
  ""patternProperties"": {
     ""hi"": {""type"":""string""},
     ""ho"": {""type"":""string""}
  },
  ""additionalProperties"": false
}";

      string json = @"{
  ""hi"": ""A string!"",
  ""hide"": ""A string!"",
  ""ho"": 1,
  ""hey"": ""A string!""
}";

      Json.Schema.ValidationEventArgs validationEventArgs = null;

      JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
      reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
      reader.Schema = JsonSchema.Parse(schemaJson);

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

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
      Assert.AreEqual(null, validationEventArgs);

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

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
      Assert.AreEqual(null, validationEventArgs);

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

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
      Assert.AreEqual(null, validationEventArgs);

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.Integer, reader.TokenType);
      Assert.AreEqual("Invalid type. Expected String but got Integer. Line 4, position 10.", validationEventArgs.Message);

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
      Assert.AreEqual("Property 'hey' has not been defined and the schema does not allow additional properties. Line 5, position 9.", validationEventArgs.Message);

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

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

      Assert.IsFalse(reader.Read());
    }
Example #28
0
        public void ValidateUnrestrictedArray()
        {
            string schemaJson = @"{
  ""type"":""array""
}";

            string json = "['pie','cake',['nested1','nested2'],{'nestedproperty1':1.1,'nestedproperty2':[null]}]";

            Json.Schema.ValidationEventArgs validationEventArgs = null;

            JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));

            reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
            reader.Schema = JsonSchema.Parse(schemaJson);

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

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

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

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

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

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

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

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

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

            Assert.IsTrue(reader.Read());
            Assert.AreEqual(JsonToken.Float, reader.TokenType);
            Assert.AreEqual(1.1, reader.Value);

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

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

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

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

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

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

            Assert.IsNull(validationEventArgs);
        }
Example #29
0
        public void ExtendsDisallowAdditionProperties()
        {
            string json = "{'firstproperty':'blah','secondproperty':'blah2','additional':'blah3','additional2':'blah4'}";

            Json.Schema.ValidationEventArgs validationEventArgs = null;

            JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));

            reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
            reader.Schema = GetExtendedSchema();

            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());
            Assert.AreEqual(null, validationEventArgs);

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

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

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

            Assert.IsTrue(reader.Read());
            Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
            Assert.AreEqual("additional", reader.Value.ToString());
            Assert.AreEqual("Property 'additional' has not been defined and the schema does not allow additional properties. Line 1, position 62.", validationEventArgs.Message);

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

            Assert.IsTrue(reader.Read());
            Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
            Assert.AreEqual("additional2", reader.Value.ToString());
            Assert.AreEqual("Property 'additional2' has not been defined and the schema does not allow additional properties. Line 1, position 84.", validationEventArgs.Message);

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

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

            Assert.IsFalse(reader.Read());
        }
Example #30
0
        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
}";

            JsonSchemaResolver resolver     = new JsonSchemaResolver();
            JsonSchema         firstSchema  = JsonSchema.Parse(first, resolver);
            JsonSchema         secondSchema = JsonSchema.Parse(second, resolver);

            JsonSchemaModelBuilder modelBuilder = new JsonSchemaModelBuilder();

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

            Json.Schema.ValidationEventArgs validationEventArgs = null;
            List <string> errors = new List <string>();

            JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));

            reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; errors.Add(validationEventArgs.Message); };
            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());
            Assert.AreEqual(null, validationEventArgs);

            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.AreEqual("String 'secasecasecasecaseca' exceeds maximum length of 10. Line 3, position 41.", errors[0]);

            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(4, errors.Count);
            Assert.AreEqual("String 'aaa' is less than minimum length of 7. Line 5, position 39.", errors[1]);
            Assert.AreEqual("String 'aaa' does not match regex pattern 'hi'. Line 5, position 39.", errors[2]);
            Assert.AreEqual("String 'aaa' does not match regex pattern 'hi2u'. Line 5, position 39.", errors[3]);

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

            Assert.IsTrue(reader.Read());
            Assert.AreEqual(JsonToken.String, reader.TokenType);
            Assert.AreEqual("three", reader.Value.ToString());
            Assert.AreEqual(5, errors.Count);
            Assert.AreEqual("String 'three' is less than minimum length of 6. Line 6, position 24.", errors[4]);

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

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

            Assert.IsFalse(reader.Read());
        }
Example #31
0
        public void ValidateTypes()
        {
            string schemaJson = @"{
  ""description"":""A person"",
  ""type"":""object"",
  ""properties"":
  {
    ""name"":{""type"":""string""},
    ""hobbies"":
    {
      ""type"":""array"",
      ""items"": {""type"":""string""}
    }
  }
}";

            string json = @"{'name':""James"",'hobbies':[""pie"",'cake']}";

            Json.Schema.ValidationEventArgs validationEventArgs = null;

            JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));

            reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
            JsonSchema schema = JsonSchema.Parse(schemaJson);

            reader.Schema = schema;
            Assert.AreEqual(schema, reader.Schema);

            Assert.AreEqual(0, reader.Depth);

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

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

            Assert.AreEqual(1, reader.Depth);

            Assert.IsTrue(reader.Read());
            Assert.AreEqual(JsonToken.String, reader.TokenType);
            Assert.AreEqual("James", reader.Value.ToString());
            Assert.AreEqual(typeof(string), reader.ValueType);
            Assert.AreEqual('"', reader.QuoteChar);

            Assert.IsTrue(reader.Read());
            Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
            Assert.AreEqual("hobbies", reader.Value.ToString());
            Assert.AreEqual('\'', reader.QuoteChar);

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

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

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

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

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

            Assert.IsNull(validationEventArgs);
        }
    public void NoAdditionalProperties()
    {
      string schemaJson = @"{
  ""type"":""array"",
  ""items"": [{""type"":""string""},{""type"":""integer""}],
  ""additionalProperties"": false
}";

      string json = @"[1, 'a', null]";

      Json.Schema.ValidationEventArgs validationEventArgs = null;

      JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
      reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
      reader.Schema = JsonSchema.Parse(schemaJson);

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

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.Integer, reader.TokenType);
      Assert.AreEqual("Invalid type. Expected String but got Integer. Line 1, position 2.", validationEventArgs.Message);

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.String, reader.TokenType);
      Assert.AreEqual("Invalid type. Expected Integer but got String. Line 1, position 7.", validationEventArgs.Message);

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.Null, reader.TokenType);
      Assert.AreEqual("Index 3 has not been defined and the schema does not allow additional items. Line 1, position 13.", validationEventArgs.Message);

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

      Assert.IsFalse(reader.Read());
    }
    public void ArrayCountLessThanMinimumItems()
    {
      string schemaJson = @"{
  ""type"":""array"",
  ""minItems"":2,
  ""maxItems"":3
}";

      string json = "[null]";

      Json.Schema.ValidationEventArgs validationEventArgs = null;

      JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
      reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
      reader.Schema = JsonSchema.Parse(schemaJson);

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

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

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.EndArray, reader.TokenType);
      Assert.AreEqual("Array item count 1 is less than minimum count of 2. Line 1, position 6.", validationEventArgs.Message);

      Assert.IsNotNull(validationEventArgs);
    }
    public void ValidateUnrestrictedArray()
    {
      string schemaJson = @"{
  ""type"":""array""
}";

      string json = "['pie','cake',['nested1','nested2'],{'nestedproperty1':1.1,'nestedproperty2':[null]}]";

      Json.Schema.ValidationEventArgs validationEventArgs = null;

      JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
      reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
      reader.Schema = JsonSchema.Parse(schemaJson);

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

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

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

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

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

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

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

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

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

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.Float, reader.TokenType);
      Assert.AreEqual(1.1, reader.Value);

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

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

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

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

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

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

      Assert.IsNull(validationEventArgs);
    }
    public void StringDisallowed()
    {
      string schemaJson = @"{
  ""type"":""array"",
  ""items"":{
    ""disallow"":[""number""]
  },
  ""maxItems"":3
}";

      string json = "['pie',1.1]";

      Json.Schema.ValidationEventArgs validationEventArgs = null;

      JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
      reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
      reader.Schema = JsonSchema.Parse(schemaJson);

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

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.String, reader.TokenType);
      Assert.AreEqual(null, validationEventArgs);

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.Float, reader.TokenType);
      Assert.AreEqual(@"Type Float is disallowed. Line 1, position 10.", validationEventArgs.Message);

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

      Assert.IsNotNull(validationEventArgs);
    }
    public void DuplicateErrorsTest()
    {
      string schema = @"{
  ""id"":""ErrorDemo.Database"",
  ""properties"":{
    ""ErrorDemoDatabase"":{
      ""type"":""object"",
      ""required"":true,
      ""properties"":{
        ""URL"":{
          ""type"":""string"",
          ""required"":true
        },
        ""Version"":{
          ""type"":""string"",
          ""required"":true
        },
        ""Date"":{
          ""type"":""string"",
          ""format"":""date-time"",
          ""required"":true
        },
        ""MACLevels"":{
          ""type"":""object"",
          ""required"":true,
          ""properties"":{
            ""MACLevel"":{
              ""type"":""array"",
              ""required"":true,
              ""items"":[
                {
                  ""required"":true,
                  ""properties"":{
                    ""IDName"":{
                      ""type"":""string"",
                      ""required"":true
                    },
                    ""Order"":{
                      ""type"":""string"",
                      ""required"":true
                    },
                    ""IDDesc"":{
                      ""type"":""string"",
                      ""required"":true
                    },
                    ""IsActive"":{
                      ""type"":""string"",
                      ""required"":true
                    }
                  }
                }
              ]
            }
          }
        }
      }
    }
  }
}";

      string json = @"{
  ""ErrorDemoDatabase"":{
    ""URL"":""localhost:3164"",
    ""Version"":""1.0"",
    ""Date"":""6.23.2010, 9:35:18.121"",
    ""MACLevels"":{
      ""MACLevel"":[
        {
          ""@IDName"":""Developer"",
          ""Order"":""0"",
          ""IDDesc"":""DeveloperDesc"",
          ""IsActive"":""True""
        },
        {
          ""IDName"":""Technician"",
          ""Order"":""1"",
          ""IDDesc"":""TechnicianDesc"",
          ""IsActive"":""True""
        },
        {
          ""IDName"":""Administrator"",
          ""Order"":""2"",
          ""IDDesc"":""AdministratorDesc"",
          ""IsActive"":""True""
        },
        {
          ""IDName"":""PowerUser"",
          ""Order"":""3"",
          ""IDDesc"":""PowerUserDesc"",
          ""IsActive"":""True""
        },
        {
          ""IDName"":""Operator"",
          ""Order"":""4"",
          ""IDDesc"":""OperatorDesc"",
          ""IsActive"":""True""
        }
      ]
    }
  }
}";

      IList<ValidationEventArgs> validationEventArgs = new List<ValidationEventArgs>();

      JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
      reader.ValidationEventHandler += (sender, args) =>
        {
          validationEventArgs.Add(args);
        };
      reader.Schema = JsonSchema.Parse(schema);

      while (reader.Read())
      {
      }

      Assert.AreEqual(1, validationEventArgs.Count);
    }
    public void DisableAdditionalProperties()
    {
      string schemaJson = @"{
  ""description"":""A person"",
  ""type"":""object"",
  ""properties"":
  {
    ""name"":{""type"":""string""}
  },
  ""additionalProperties"":false
}";

      string json = "{'name':'James','additionalProperty1':null,'additionalProperty2':null}";

      Json.Schema.ValidationEventArgs validationEventArgs = null;

      JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
      reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
      reader.Schema = JsonSchema.Parse(schemaJson);

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

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

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

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

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.Null, reader.TokenType);
      Assert.AreEqual(null, reader.Value);
      Assert.AreEqual("Property 'additionalProperty1' has not been defined and the schema does not allow additional properties. Line 1, position 38.", validationEventArgs.Message);

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

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.Null, reader.TokenType);
      Assert.AreEqual(null, reader.Value);
      Assert.AreEqual("Property 'additionalProperty2' has not been defined and the schema does not allow additional properties. Line 1, position 65.", validationEventArgs.Message);

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

      Assert.IsNotNull(validationEventArgs);
    }
    public void ReadAsInt32InArrayIncomplete()
    {
      string schemaJson = @"{
  ""type"":""array"",
  ""items"":{
    ""type"":""integer""
  },
  ""maxItems"":1
}";

      string json = "[1,2";

      Json.Schema.ValidationEventArgs validationEventArgs = null;

      JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
      reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
      reader.Schema = JsonSchema.Parse(schemaJson);

      reader.Read();
      Assert.AreEqual(JsonToken.StartArray, reader.TokenType);

      reader.ReadAsInt32();
      Assert.AreEqual(JsonToken.Integer, reader.TokenType);
      Assert.AreEqual(null, validationEventArgs);

      reader.ReadAsInt32();
      Assert.AreEqual(JsonToken.Integer, reader.TokenType);
      Assert.AreEqual(null, validationEventArgs);

      reader.ReadAsInt32();
      Assert.AreEqual(JsonToken.None, reader.TokenType);
      Assert.AreEqual(null, validationEventArgs);
    }
    public void ExtendsDisallowAdditionProperties()
    {
      string json = "{'firstproperty':'blah','secondproperty':'blah2','additional':'blah3','additional2':'blah4'}";

      Json.Schema.ValidationEventArgs validationEventArgs = null;

      JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
      reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
      reader.Schema = GetExtendedSchema();

      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());
      Assert.AreEqual(null, validationEventArgs);

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

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

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

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
      Assert.AreEqual("additional", reader.Value.ToString());
      Assert.AreEqual("Property 'additional' has not been defined and the schema does not allow additional properties. Line 1, position 62.", validationEventArgs.Message);

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

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
      Assert.AreEqual("additional2", reader.Value.ToString());
      Assert.AreEqual("Property 'additional2' has not been defined and the schema does not allow additional properties. Line 1, position 84.", validationEventArgs.Message);

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

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

      Assert.IsFalse(reader.Read());
    }
    public void StringIsNotInEnum()
    {
      string schemaJson = @"{
  ""type"":""array"",
  ""items"":{
    ""type"":""string"",
    ""enum"":[""one"",""two""]
  },
  ""maxItems"":3
}";

      string json = "['one','two','THREE']";

      Json.Schema.ValidationEventArgs validationEventArgs = null;

      JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
      reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
      reader.Schema = JsonSchema.Parse(schemaJson);

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

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

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.String, reader.TokenType);
      Assert.AreEqual(null, validationEventArgs);

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.String, reader.TokenType);
      Assert.AreEqual(@"Value ""THREE"" is not defined in enum. Line 1, position 20.", validationEventArgs.Message);
      Assert.AreEqual("[2]", validationEventArgs.Path);

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

      Assert.IsNotNull(validationEventArgs);
    }
    public void ThrowExceptionWhenNoValidationEventHandler()
    {
      ExceptionAssert.Throws<JsonSchemaException>("Integer 10 exceeds maximum value of 5. Line 1, position 2.",
      () =>
      {
        string schemaJson = @"{
  ""type"":""integer"",
  ""maximum"":5
}";

        JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader("10")));
        reader.Schema = JsonSchema.Parse(schemaJson);

        Assert.IsTrue(reader.Read());
      });
    }
    public void IntegerGreaterThanMaximumValue()
    {
      string schemaJson = @"{
  ""type"":""integer"",
  ""maximum"":5
}";

      string json = "10";

      Json.Schema.ValidationEventArgs validationEventArgs = null;

      JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
      reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
      reader.Schema = JsonSchema.Parse(schemaJson);

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.Integer, reader.TokenType);
      Assert.AreEqual("Integer 10 exceeds maximum value of 5. Line 1, position 2.", validationEventArgs.Message);
      Assert.AreEqual("", validationEventArgs.Path);

      Assert.IsNotNull(validationEventArgs);
    }
    public void FloatLessThanMinimumValue()
    {
      string schemaJson = @"{
  ""type"":""number"",
  ""minimum"":5
}";

      string json = "1.1";

      Json.Schema.ValidationEventArgs validationEventArgs = null;

      JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
      reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
      reader.Schema = JsonSchema.Parse(schemaJson);

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.Float, reader.TokenType);
      Assert.AreEqual("Float 1.1 is less than minimum value of 5. Line 1, position 3.", validationEventArgs.Message);

      Assert.IsNotNull(validationEventArgs);
    }
    public void ValidateTypes()
    {
      string schemaJson = @"{
  ""description"":""A person"",
  ""type"":""object"",
  ""properties"":
  {
    ""name"":{""type"":""string""},
    ""hobbies"":
    {
      ""type"":""array"",
      ""items"": {""type"":""string""}
    }
  }
}";

      string json = @"{'name':""James"",'hobbies':[""pie"",'cake']}";

      Json.Schema.ValidationEventArgs validationEventArgs = null;

      JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
      reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
      JsonSchema schema = JsonSchema.Parse(schemaJson);
      reader.Schema = schema;
      Assert.AreEqual(schema, reader.Schema);
      Assert.AreEqual(0, reader.Depth);
      Assert.AreEqual("", reader.Path);

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

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
      Assert.AreEqual("name", reader.Value.ToString());
      Assert.AreEqual("name", reader.Path);
      Assert.AreEqual(1, reader.Depth);

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.String, reader.TokenType);
      Assert.AreEqual("James", reader.Value.ToString());
      Assert.AreEqual(typeof (string), reader.ValueType);
      Assert.AreEqual('"', reader.QuoteChar);
      Assert.AreEqual("name", reader.Path);

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
      Assert.AreEqual("hobbies", reader.Value.ToString());
      Assert.AreEqual('\'', reader.QuoteChar);
      Assert.AreEqual("hobbies", reader.Path);

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.StartArray, reader.TokenType);
      Assert.AreEqual("hobbies", reader.Path);

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.String, reader.TokenType);
      Assert.AreEqual("pie", reader.Value.ToString());
      Assert.AreEqual('"', reader.QuoteChar);
      Assert.AreEqual("hobbies[0]", reader.Path);

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.String, reader.TokenType);
      Assert.AreEqual("cake", reader.Value.ToString());
      Assert.AreEqual("hobbies[1]", reader.Path);

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.EndArray, reader.TokenType);
      Assert.AreEqual("hobbies", reader.Path);

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
      Assert.AreEqual("", reader.Path);

      Assert.IsFalse(reader.Read());
      
      Assert.IsNull(validationEventArgs);
    }
    public void ThrowExceptionWhenNoValidationEventHandler()
    {
      string schemaJson = @"{
  ""type"":""integer"",
  ""maximum"":5
}";

      JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader("10")));
      reader.Schema = JsonSchema.Parse(schemaJson);

      Assert.IsTrue(reader.Read());
    }
    public void FloatExceedsMaxDecimalPlaces()
    {
      string schemaJson = @"{
  ""type"":""array"",
  ""items"":{
    ""type"":""number"",
    ""divisibleBy"":0.1
  }
}";

      string json = "[1.1,2.2,4.001]";

      Json.Schema.ValidationEventArgs validationEventArgs = null;

      JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
      reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
      reader.Schema = JsonSchema.Parse(schemaJson);

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

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

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.Float, reader.TokenType);
      Assert.AreEqual(null, validationEventArgs);

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.Float, reader.TokenType);
      Assert.AreEqual(@"Float 4.001 is not evenly divisible by 0.1. Line 1, position 14.", validationEventArgs.Message);
      Assert.AreEqual("[2]", validationEventArgs.Path);

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

      Assert.IsNotNull(validationEventArgs);
    }
    public void FloatIsNotInEnum()
    {
      string schemaJson = @"{
  ""type"":""array"",
  ""items"":{
    ""type"":""number"",
    ""enum"":[1.1,2.2]
  },
  ""maxItems"":3
}";

      string json = "[1.1,2.2,3.0]";

      Json.Schema.ValidationEventArgs validationEventArgs = null;

      JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
      reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
      reader.Schema = JsonSchema.Parse(schemaJson);

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

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

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.Float, reader.TokenType);
      Assert.AreEqual(null, validationEventArgs);

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.Float, reader.TokenType);
      Assert.AreEqual(@"Value 3.0 is not defined in enum. Line 1, position 12.", validationEventArgs.Message);

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

      Assert.IsNotNull(validationEventArgs);
    }
    public void BooleanNotInEnum()
    {
      string schemaJson = @"{
  ""type"":""array"",
  ""items"":{
    ""type"":""boolean"",
    ""enum"":[true]
  },
  ""maxItems"":3
}";

      string json = "[true,false]";

      Json.Schema.ValidationEventArgs validationEventArgs = null;

      JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
      reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
      reader.Schema = JsonSchema.Parse(schemaJson);

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

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.Boolean, reader.TokenType);
      Assert.AreEqual(null, validationEventArgs);

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.Boolean, reader.TokenType);
      Assert.AreEqual(@"Value false is not defined in enum. Line 1, position 11.", validationEventArgs.Message);
      Assert.AreEqual("[1]", validationEventArgs.Path);

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

      Assert.IsNotNull(validationEventArgs);
    }
    public void ExtendsMissingRequiredProperties()
    {
      string json = "{}";

      List<string> errors = new List<string>();

      JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
      reader.ValidationEventHandler += (sender, args) => { errors.Add(args.Message); };
      reader.Schema = GetExtendedSchema();

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

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

      Assert.AreEqual(1, errors.Count);
      Assert.AreEqual("Required properties are missing from object: secondproperty, firstproperty. Line 1, position 2.", errors[0]);
    }
    public void InvalidDataType()
    {
      string schemaJson = @"{
  ""type"":""string"",
  ""minItems"":2,
  ""maxItems"":3,
  ""items"":{}
}";

      string json = "[null,null,null,null]";

      Json.Schema.ValidationEventArgs validationEventArgs = null;

      JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
      reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
      reader.Schema = JsonSchema.Parse(schemaJson);

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.StartArray, reader.TokenType);
      Assert.AreEqual(@"Invalid type. Expected String but got Array. Line 1, position 1.", validationEventArgs.Message);

      Assert.IsNotNull(validationEventArgs);
    }
Example #51
0
        public void IsValidPerformance()
        {
            JArray a = JArray.Parse(Json);

            a.IsValid(Schema);
            a.IsValid(SchemaV3);

            using (new PerformanceTester("Raw"))
            {
                for (int i = 1; i < ValidationCount; i++)
                {
                    JsonTextReader reader = new JsonTextReader(new StringReader(Json));

                    while (reader.Read())
                    {
                    }
                }
            }

            GC.Collect();

            using (new PerformanceTester("JSchema"))
            {
                for (int i = 1; i < ValidationCount; i++)
                {
                    JsonTextReader reader = new JsonTextReader(new StringReader(Json));

                    JSchemaValidatingReader validatingReader = new JSchemaValidatingReader(reader);
                    validatingReader.Schema = Schema;
                    while (validatingReader.Read())
                    {
                    }
                }
            }

            GC.Collect();

            using (new PerformanceTester("JsonSchema"))
            {
                for (int i = 1; i < ValidationCount; i++)
                {
                    JsonTextReader reader = new JsonTextReader(new StringReader(Json));

                    JsonValidatingReader vr = new JsonValidatingReader(reader);
                    vr.Schema = SchemaV3;
                    while (vr.Read())
                    {
                    }
                }
            }

            XmlSchema schema = XmlSchema.Read(new StringReader(SchemaXml), (sender, args) =>
            {
                throw new Exception(args.Message);
            });
            XmlSchemaSet set = new XmlSchemaSet();

            set.Add(schema);
            set.Compile();

            GC.Collect();

            using (new PerformanceTester("IsValid_XmlSchema"))
            {
                for (int i = 1; i < ValidationCount; i++)
                {
                    XmlReaderSettings settings = new XmlReaderSettings();
                    settings.ValidationType          = ValidationType.Schema;
                    settings.Schemas                 = set;
                    settings.ValidationEventHandler += ValidatingReader_ValidationEventHandler;

                    XmlReader reader = XmlReader.Create(new StringReader(Xml), settings);

                    while (reader.Read())
                    {
                    }
                }
            }
        }
    public void MissingNonRequiredProperties()
    {
      string schemaJson = @"{
  ""description"":""A person"",
  ""type"":""object"",
  ""properties"":
  {
    ""name"":{""type"":""string"",""required"":true},
    ""hobbies"":{""type"":""string"",""required"":false},
    ""age"":{""type"":""integer""}
  }
}";

      string json = "{'name':'James'}";

      Json.Schema.ValidationEventArgs validationEventArgs = null;

      JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
      reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
      reader.Schema = JsonSchema.Parse(schemaJson);

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

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

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

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

      Assert.IsNull(validationEventArgs);
    }
    public void BigIntegerDivisibleBy_Fraction()
    {
      string schemaJson = @"{
  ""type"":""array"",
  ""items"":{
    ""type"":""number"",
    ""divisibleBy"":1.1
  }
}";

      string json = "[999999999999999999999999999999999999999999999999999999999]";

      Json.Schema.ValidationEventArgs validationEventArgs = null;

      JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
      reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
      reader.Schema = JsonSchema.Parse(schemaJson);

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

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.Integer, reader.TokenType);
      Assert.IsNotNull(validationEventArgs);
      Assert.AreEqual(@"Integer 999999999999999999999999999999999999999999999999999999999 is not evenly divisible by 1.1. Line 1, position 58.", validationEventArgs.Message);
      Assert.AreEqual("[0]", validationEventArgs.Path);

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.EndArray, reader.TokenType);
    }
    public void ExtendsStringGreaterThanMaximumLength()
    {
      string schemaJson = @"{
  ""extends"":{
    ""type"":""string"",
    ""minLength"":5,
    ""maxLength"":10
  },
  ""maxLength"":9
}";

      List<string> errors = new List<string>();
      string json = "'The quick brown fox jumps over the lazy dog.'";

      Json.Schema.ValidationEventArgs validationEventArgs = null;

      JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));
      reader.ValidationEventHandler += (sender, args) =>
        {
          validationEventArgs = args;
          errors.Add(validationEventArgs.Message);
        };
      reader.Schema = JsonSchema.Parse(schemaJson);

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.String, reader.TokenType);
      Assert.AreEqual(1, errors.Count);
      Assert.AreEqual("String 'The quick brown fox jumps over the lazy dog.' exceeds maximum length of 9. Line 1, position 46.", errors[0]);

      Assert.IsNotNull(validationEventArgs);
    }
Example #55
0
        public void ReaderPerformance()
        {
            string json = @"[
    {
        ""id"": 2,
        ""name"": ""An ice sculpture"",
        ""price"": 12.50,
        ""tags"": [""cold"", ""ice""],
        ""dimensions"": {
            ""length"": 7.0,
            ""width"": 12.0,
            ""height"": 9.5
        },
        ""warehouseLocation"": {
            ""latitude"": -78.75,
            ""longitude"": 20.4
        }
    },
    {
        ""id"": 3,
        ""name"": ""A blue mouse"",
        ""price"": 25.50,
        ""dimensions"": {
            ""length"": 3.1,
            ""width"": 1.0,
            ""height"": 1.0
        },
        ""warehouseLocation"": {
            ""latitude"": 54.4,
            ""longitude"": -32.7
        }
    }
]";

            JsonSchema schema = JsonSchema.Parse(@"{
    ""$schema"": ""http://json-schema.org/draft-04/schema#"",
    ""title"": ""Product set"",
    ""type"": ""array"",
    ""items"": {
        ""title"": ""Product"",
        ""type"": ""object"",
        ""properties"": {
            ""id"": {
                ""description"": ""The unique identifier for a product"",
                ""type"": ""number"",
                ""required"": true
            },
            ""name"": {
                ""type"": ""string"",
                ""required"": true
            },
            ""price"": {
                ""type"": ""number"",
                ""minimum"": 0,
                ""exclusiveMinimum"": true,
                ""required"": true
            },
            ""tags"": {
                ""type"": ""array"",
                ""items"": {
                    ""type"": ""string""
                },
                ""minItems"": 1,
                ""uniqueItems"": true
            },
            ""dimensions"": {
                ""type"": ""object"",
                ""properties"": {
                    ""length"": {""type"": ""number"",""required"": true},
                    ""width"": {""type"": ""number"",""required"": true},
                    ""height"": {""type"": ""number"",""required"": true}
                }
            },
            ""warehouseLocation"": {
                ""description"": ""A geographical coordinate"",
                ""type"": ""object"",
                ""properties"": {
                    ""latitude"": { ""type"": ""number"" },
                    ""longitude"": { ""type"": ""number"" }
                }
            }
        }
    }
}");

            using (var tester = new PerformanceTester("Reader"))
            {
                for (int i = 0; i < 5000; i++)
                {
                    JsonTextReader reader = new JsonTextReader(new StringReader(json));
                    JsonValidatingReader validatingReader = new JsonValidatingReader(reader);
                    validatingReader.Schema = schema;

                    while (validatingReader.Read())
                    {
                    }
                }
            }
        }
Example #56
0
        public void ReaderPerformance()
        {
            string json = @"[
    {
        ""id"": 2,
        ""name"": ""An ice sculpture"",
        ""price"": 12.50,
        ""tags"": [""cold"", ""ice""],
        ""dimensions"": {
            ""length"": 7.0,
            ""width"": 12.0,
            ""height"": 9.5
        },
        ""warehouseLocation"": {
            ""latitude"": -78.75,
            ""longitude"": 20.4
        }
    },
    {
        ""id"": 3,
        ""name"": ""A blue mouse"",
        ""price"": 25.50,
        ""dimensions"": {
            ""length"": 3.1,
            ""width"": 1.0,
            ""height"": 1.0
        },
        ""warehouseLocation"": {
            ""latitude"": 54.4,
            ""longitude"": -32.7
        }
    }
]";

            JsonSchema schema = JsonSchema.Parse(@"{
    ""$schema"": ""http://json-schema.org/draft-04/schema#"",
    ""title"": ""Product set"",
    ""type"": ""array"",
    ""items"": {
        ""title"": ""Product"",
        ""type"": ""object"",
        ""properties"": {
            ""id"": {
                ""description"": ""The unique identifier for a product"",
                ""type"": ""number"",
                ""required"": true
            },
            ""name"": {
                ""type"": ""string"",
                ""required"": true
            },
            ""price"": {
                ""type"": ""number"",
                ""minimum"": 0,
                ""exclusiveMinimum"": true,
                ""required"": true
            },
            ""tags"": {
                ""type"": ""array"",
                ""items"": {
                    ""type"": ""string""
                },
                ""minItems"": 1,
                ""uniqueItems"": true
            },
            ""dimensions"": {
                ""type"": ""object"",
                ""properties"": {
                    ""length"": {""type"": ""number"",""required"": true},
                    ""width"": {""type"": ""number"",""required"": true},
                    ""height"": {""type"": ""number"",""required"": true}
                }
            },
            ""warehouseLocation"": {
                ""description"": ""A geographical coordinate"",
                ""type"": ""object"",
                ""properties"": {
                    ""latitude"": { ""type"": ""number"" },
                    ""longitude"": { ""type"": ""number"" }
                }
            }
        }
    }
}");

            using (var tester = new PerformanceTester("Reader"))
            {
                for (int i = 0; i < 5000; i++)
                {
                    JsonTextReader       reader           = new JsonTextReader(new StringReader(json));
                    JsonValidatingReader validatingReader = new JsonValidatingReader(reader);
                    validatingReader.Schema = schema;

                    while (validatingReader.Read())
                    {
                    }
                }
            }
        }
Example #57
0
        public void PatternPropertiesNoAdditionalProperties()
        {
            string schemaJson = @"{
  ""type"":""object"",
  ""patternProperties"": {
     ""hi"": {""type"":""string""},
     ""ho"": {""type"":""string""}
  },
  ""additionalProperties"": false
}";

            string json = @"{
  ""hi"": ""A string!"",
  ""hide"": ""A string!"",
  ""ho"": 1,
  ""hey"": ""A string!""
}";

            Json.Schema.ValidationEventArgs validationEventArgs = null;

            JsonValidatingReader reader = new JsonValidatingReader(new JsonTextReader(new StringReader(json)));

            reader.ValidationEventHandler += (sender, args) => { validationEventArgs = args; };
            reader.Schema = JsonSchema.Parse(schemaJson);

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

            Assert.IsTrue(reader.Read());
            Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
            Assert.AreEqual(null, validationEventArgs);

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

            Assert.IsTrue(reader.Read());
            Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
            Assert.AreEqual(null, validationEventArgs);

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

            Assert.IsTrue(reader.Read());
            Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
            Assert.AreEqual(null, validationEventArgs);

            Assert.IsTrue(reader.Read());
            Assert.AreEqual(JsonToken.Integer, reader.TokenType);
            Assert.AreEqual("Invalid type. Expected String but got Integer. Line 4, position 10.", validationEventArgs.Message);

            Assert.IsTrue(reader.Read());
            Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
            Assert.AreEqual("Property 'hey' has not been defined and the schema does not allow additional properties. Line 5, position 8.", validationEventArgs.Message);

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

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

            Assert.IsFalse(reader.Read());
        }