title: Can Json Schema Validation via Newtonsoft.Json.Schema validate VALUES?
body: <p>I have a small sample.  If my Json is good, it works correctly.  If I change the "tag" (aka, the property name), it works correctly by having invalid messages.  If I change the value of a Guid to be a non-guid-value, the Json Schema Validation does not fail.</p>
  
  <p>Is there a way to fail validation for a Guid value?</p>
  
  <pre><code>public class MyCoolObject
  {
      public Guid TheUuid { get; set; }
  
      public Int32 TheInteger { get; set; }
  
      public DateTime TheDateTime { get; set; }
  }
  </code></pre>
  
  <p>and my test method.  When i = 2 (and I'm setting the string to contain "NOTAGUID-3333-3333-3333-333333333333"), that is when I don't get error messages like I would like to.</p>
  
  <pre><code>    private static void RunJsonSchemaValidate()
      {
  
          /* Note, the TheUuid is of type "string" and format "guid" */
          string jsonSchemaText = @"
                  {
                    ""typeName"": ""MyCoolObject"",
                    ""additionalProperties"": false,
                    ""type"": ""object"",
                    ""required"": [
                      ""TheUuid"",
                      ""TheInteger"",
                      ""TheDateTime""
                    ],
                    ""properties"": {
                      ""TheUuid"": {
                        ""type"": ""string"",
                        ""format"": ""guid""
                      },
                      ""TheInteger"": {
                        ""type"": ""integer""
                      },
                      ""TheDateTime"": {
                        ""type"": ""string"",
                        ""format"": ""date-time""
                      }
                    },
                    ""$schema"": ""http://json-schema.org/draft-04/schema#""
                  }
          ";
  
          Newtonsoft.Json.Schema.JSchema jschem = Newtonsoft.Json.Schema.JSchema.Parse(jsonSchemaText);
  
          for (int i = 0; i &lt; 3; i++)
          {
  
              string jsonContent = string.Empty;
  
              switch (i)
              {
                  case 1:
                      /* bad json, change the property NAME */
                      jsonContent = @"{
                          ""TheUuidXXX"": ""33333333-3333-3333-3333-333333333333"",
                          ""TheInteger"": 2147483647,
                          ""TheDateTime"": ""2017-08-22T15:32:10.7023008-04:00""
                      }";
                      break;
                  case 2:
                      /* bad json, change the property VALUE */
                      jsonContent = @"{
                          ""TheUuid"": ""NOTAGUID-3333-3333-3333-333333333333"",
                          ""TheInteger"": 2147483647,
                          ""TheDateTime"": ""2017-08-22T15:32:10.7023008-04:00""
                      }";
                      break;
                  case 3:
                      /* bad json, bad integer */
                      jsonContent = @"{
                          ""TheUuid"": ""33333333-3333-3333-3333-333333333333"",
                          ""TheInteger"": notAnumber,
                          ""TheDateTime"": ""2017-08-22T15:32:10.7023008-04:00""
                      }";
                      break;
                  case 4:
                      /* bad json, bad date */
                      jsonContent = @"{
                          ""TheUuid"": ""33333333-3333-3333-3333-333333333333"",
                          ""TheInteger"": 2147483647,
                          ""TheDateTime"": ""NOTADATE""
                      }";
                      break;
                  default:
                      /* good json */
                      jsonContent = @"{
                          ""TheUuid"": ""33333333-3333-3333-3333-333333333333"",
                          ""TheInteger"": 2147483647,
                          ""TheDateTime"": ""2017-08-22T15:32:10.7023008-04:00""
                      }";
                      break;
  
              }
  
              /* START THE MEAT OF THIS PROCEDURE */
  
              Newtonsoft.Json.Linq.JObject jobj = Newtonsoft.Json.Linq.JObject.Parse(jsonContent);
  
              IList&lt;string&gt; messages;
              bool valid = jobj.IsValid(jschem, out messages);
  
              /* ENDTHE MEAT OF THIS PROCEDURE */
  
  
              if (!valid)
              {
                  string errorMsg = "i=" + i.ToString() + ":" + string.Join(",", messages);
                  Console.WriteLine(string.Empty);
                  Console.WriteLine(string.Empty);
                  Console.WriteLine(errorMsg);
              }
              else
              {
                  Console.WriteLine(string.Empty);
                  Console.WriteLine(string.Empty);
                  Console.WriteLine("i=" + i.ToString() + ":" + "Good json Yes");
  
                  MyCoolObject thisShouldWorkWhenValidationPasses = Newtonsoft.Json.JsonConvert.DeserializeObject&lt;MyCoolObject&gt;(jsonContent);
  
              }
  
              Console.WriteLine(string.Empty);
              Console.WriteLine("--------------------------------------------------");
              Console.WriteLine(string.Empty);
  
  
          }
  </code></pre>
  
  <p>and the packages</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;packages&gt;
  
    &lt;package id="Newtonsoft.Json" version="10.0.2" targetFramework="net45" /&gt;
    &lt;package id="Newtonsoft.Json.Schema" version="3.0.3" targetFramework="net45" /&gt;
  
  &lt;/packages&gt;
  </code></pre>
  
  <p>So what is happening is that when i=2, the json-schema passes, but then MyCoolObject thisShouldWorkWhenValidationPasses throws an exception....</p>
  
  <blockquote>
    <p>i=2:Good json Yes</p>
    
    <p>Unhandled Exception: Newtonsoft.Json.JsonSerializationException: Error
    converting value "NOTAGUID-3333-3333-3333-333333333333" to type
    'System.Guid'. Path 'TheUuid', line 2, position 77. --->
    System.ArgumentException: Could not cast or convert from System.String
    to System.Guid.</p>
  </blockquote>
  
  <p>:(</p>
  
  <p>I'm trying to have the json-schema fail earlier.</p>
  
  <p>The end-game is to perform a json-schema-validation without exceptions getting thrown.  Then after "everything is clear" try to load the objects.  My real stuff is more complex, but this small demo shows the issue(s).</p>
  
  <p>I also <strong><em>replaced</em></strong> the "meat of this procedure" with the below code</p>
  
  <pre><code>            /* START THE MEAT OF THIS PROCEDURE */
  
              Newtonsoft.Json.JsonTextReader reader = new Newtonsoft.Json.JsonTextReader(new System.IO.StringReader(jsonContent));
  
              Newtonsoft.Json.Schema.JSchemaValidatingReader validatingReader = new Newtonsoft.Json.Schema.JSchemaValidatingReader(reader);
              validatingReader.Schema = JSchema.Parse(schemaJson);
  
              IList&lt;string&gt; messages = new List&lt;string&gt;();
              validatingReader.ValidationEventHandler += (o, a) =&gt; messages.Add(a.Message);
  
              Newtonsoft.Json.JsonSerializer serializer = new Newtonsoft.Json.JsonSerializer();
              /* below is the issue with this code..you still try to serialize the object...and that can throw an exception */
              MyCoolObject p = serializer.Deserialize&lt;MyCoolObject&gt;(validatingReader);
              bool valid = !messages.Any();
  
              /* END THE MEAT OF THIS PROCEDURE */
  </code></pre>
  
  <p>But again, this is subject to exceptions being thrown .. trying to validate.</p>