Suravi
Suravi

Reputation: 321

Json schema email validation failed C# newtonsoft json schema

I'm referring to this document https://www.newtonsoft.com/json/help/html/JsonSchema.htm

             string schemaJson = @"{
            'description': 'A person',
              'type': 'object',
              'properties':
              {
                'email': {'type':'string', 'format':'email'},
                'name': {'type':'string'}
                }
              }
            }";

           JsonSchema schema = JsonSchema.Parse(schemaJson);

            JObject person = JObject.Parse(@"{
              'email' : 'asdasd',
              'name': 'James'
            }");

            bool valid = person.IsValid(schema);

This always return true but I ant to validate email. what is the reason for this?

Upvotes: 0

Views: 412

Answers (1)

Serge
Serge

Reputation: 43860

The JsonSchema type is obsolete. You have to use JSchema.

string schemaJson = @"{
  ""type"": ""object"",
  ""properties"": {
    ""email"": {
      ""type"":    ""string"",
       ""format"": ""email""
       },
    ""name"": {
      ""type"":         ""string""
     }
  }
}";

now you can use this code

    JSchema schema = JSchema.Parse(schemaJson);

    JObject person = JObject.Parse(@"{
              'email' : 'asdasd',
              'name': 'James'
            }");

    bool valid = person.IsValid(schema); // false

Upvotes: 1

Related Questions