1

I have following scenario of incoming messages from different devices to processing unit which shall process the messages based on message type.

  1. Messages with following Schema definition for hobbies, say schema-hobbies

    JSchema hobbiesSchema = JSchema.Parse(@"{
                   'type': 'object',
                   'properties': {
                   'name': {'type':'string'},
                   'hobbies': {
                      'type': 'array',
                      'items': {'type':'string'}
                   }}}");
    
  2. Messages with following Schema definition for countries, say schema-countries

    JSchema countriesSchema = JSchema.Parse(@"{
                   'type': 'object',
                   'properties': {
                   'name': {'type':'string'},
                   'countries': {
                      'type': 'array',
                      'items': {'type':'string'}
                   }}}");
    
  3. Messages which do not follow any of above Schema definitions

Now, I am able to validate the hobbies messages as follow

    JObject hobbies = JObject.Parse(@"{
    'name': 'James',
    'hobbies': ['.NET', 'Blogging', 'Reading', 'Xbox', 'LOLCATS']
    }");

    IList<string> errorMessages;
    bool valid = hobbies.IsValid(hobbiesSchema , out errorMessages);

    Console.WriteLine(valid);

I shall able to merge both schema and validate different incoming messages. How can I do that?

For example:

If message Json is,

    {
    'name': 'James',
    'hobbies': ['.NET', 'Blogging', 'Reading', 'Xbox', 'LOLCATS']
    }

Output shall be: True

If message Json is,

    {
    'name': 'James',
    'countries': ['India', 'Australia']
    }

Output shall be: True

If message Json is,

    {
    'Time': '03:45 AM',
    'heartbeats': ['30', '45']
    }

Output shall be: False

Note:

  1. Sample schema used in this question is just for reference. Real schema is very complex.
kudlatiger
  • 3,028
  • 8
  • 48
  • 98
  • 2
    Only double quotes are allowed. All the examples are invalid json. – radarbob Jan 18 '19 at 17:44
  • @radarbob you are wrong. This works like charm. example taken from Json.net site. question is about how to merge and json provided here is just a sample example. – kudlatiger Jan 19 '19 at 16:42
  • 1
    I just get that "uh-oh" sense because of unpleasant experience passing text-based defined format data that violates the specification. The more "nodes" sharing corrupt formatting the harder it is to ever correct and made worse by independently evolving parsing. Y'all be careful out there. – radarbob Jan 19 '19 at 21:36
  • Okay. Then please share the best practices or sample which resolves these issues. it helps – kudlatiger Jan 20 '19 at 03:04
  • 1
    @kudlatiger - your question is answered on the same page you took your samples from (https://www.newtonsoft.com/json/help/html/JsonSchema.htm) *Json.NET supports the JSON Schema standard via the JsonSchema and JsonValidatingReader classes. It sits under the Newtonsoft.Json.Schema namespace.* - rather than merging the schemas into an uber schema, just have two validation steps in your validation component. – tom redfern Jan 21 '19 at 15:26
  • 1
    Also, you should not really use single quotes, even though it works with some parsers. More info here: https://stackoverflow.com/a/14355724/569662 – tom redfern Jan 21 '19 at 15:29

0 Answers0