1

I'm trying to validate my json file but my validation doesn't work properly. Please help me to understand what is wrong.

 public static bool ValidJson(string jasonData)
        {
            string myJson = @"{
            'description': 'rehber',
            'type': 'object',
            'properties':
            {
               'isim': {'type':'string', 'required': true },
               'tel': {'type':'string','required': true},
            },
            'additionalProperties': false
         }";

            JsonSchema schema = JsonSchema.Parse(myJson);

            JToken rehber = JToken.Parse(jasonData);
            bool valid = rehber.IsValid(schema);
            return valid;
        }

jasonData that comes to ValidJson function is like that:

 "[\n  {\n    \"isim\": \"Ahmet\",\n    \"tel\": \"+46 637 530 68 94\"\n  },\n  {\n    \"isim\": \"Mahmet\",\n    \"tel\": \"+46 637 530 68 91\"\n  }\n]"

I changed my schema but it shows true all the time. I don't want additional fields in my json file and null values. But this schema doesn't work properly either.

string myJson = @"{
            'type': 'array',
            'title': 'The root schema',
            'description': 'The root schema comprises the entire JSON document.',
            'additionalItems': false,
            'items': {
                'allOf': [
                    {
                    'type': 'object',
                    'title': 'The first anyOf schema',
                    'description': 'An explanation about the purpose of this instance.',
                    'required': [   
                        'isim',
                        'tel'
                            ],
                'additionalProperties': false,
                'properties': {
                        'isim': {
                            'type': 'string',
                            },
                        'tel': {
                            'type': 'string',

                            }
                     }
                    }
                 ],
            }
        }";
Nilay Yilmaz
  • 15
  • 1
  • 6

1 Answers1

0

The reason you cannot pass the validation test is because your json data is actually an array, but not an single object.

If you assume your input data is an array, you may modify the code like:

        public static bool ValidJson(string jsonData)
        {
            string myJson = @"{
            'description': 'rehber',
            'type': 'object',
            'properties':
            {
               'isim': {'type':'string', 'required': true },
               'tel': {'type':'string','required': true},
            },
            'additionalProperties': false
         }";

            JsonSchema schema = JsonSchema.Parse(myJson);
            JArray jArray = JArray.Parse(jsonData);
            
            foreach( var jToken in jArray)
            {
                if ( !jToken.IsValid(schema))
                {
                    return false;
                }
            }

            return true;                        
        }

Please also noted that the JsonSchema & isValid method are deprecated, Newtonsoft has a new project, please check https://www.newtonsoft.com/jsonschema .

wilsonson
  • 73
  • 3