3

NB! I don't want to validate JSON against JSON Schema!

I have 2 JSON files:

1. JSON Schema file:

{
    "title" : "SomeTitle",
    "type":"object",
    "properties":{
        "City" : {"type" : "string", "editType" : "textarea"}
    }    
}

2. JSON File:

{
    "Elements": [{
            "name": "XYZ",
            "cities": ["XY", "QW", "ER", "TY"]
        }, {
            "name": "ASD",
            "cities": ["AS", "SD", "DF"]
        }
    ]
}

I want to validate these 2 files if they can be used as JSON Schema.

E.g:

First file -> true (Means that it can be used as JSON Schema)
Second file -> false

I have tried:

1) JSchema schema = JSchema.Parse(stringSchema); // It parses without any exception, and Valid property is null

2) James Newton-Kings answer

No luck, How can I validate JSON Schema itself?

kgzdev
  • 2,770
  • 2
  • 18
  • 35
  • 2
    find a json schema that defines the schema for json schema. – Daniel A. White Nov 06 '18 at 16:29
  • Just to clarify, when you try to parse them by `JSchema`, both of them `parses without any exception, and Valid property is null`? Or only one of them? If only one of them, which one? – tweray Nov 06 '18 at 16:30
  • 1
    You said you tried J. Newton-Kings answer (and linked to it). How precisely did that answer not work for you? (Note, just saying "i tried the answer and it didn't work" is not much of help for us to understand your situation and context of your problem) –  Nov 06 '18 at 16:36
  • I guess i found something. Look here: http://json-schema.org/latest/json-schema-core.html#rfc.section.4.3.1 Note the last paragraphs in section 4.3.1: `A JSON Schema MAY contain properties which are not schema keywords. Unknown keywords SHOULD be ignored. An empty schema is a JSON Schema with no properties, or only unknown properties.` If i understand this correctly, your second file should translate into an empty schema `{ }`, which is of course valid (as stated in the same section). (1/2) –  Nov 06 '18 at 17:01
  • @tweray both of them parses without any exception and Valid property is null – kgzdev Nov 06 '18 at 17:03
  • (2/2) After checking your files against the JSON schema (whatever version is current), try parsing them as a JSON schema and check how many (top-level) properties that schema has. Your non-schema should have 0. But, i guess that is not 100% fire-proof, as a non-schema json with a top-level object might contain some property that coincidentally matches the schema. As the JSON schema spec. explicitely states that unknown props. (i.e. props. not being part of a schema definition) should be ignored by default, you would need a schema parser that ignores this rule from the spec to be 100% safe. –  Nov 06 '18 at 17:03
  • @elgonzo thank you for comments. I will try your approach, will parse both of them and will count properties of the schemas. If it translates second file to empty schema then I can say that second file is not valid schema in my case. Will let you know, thanks! – kgzdev Nov 06 '18 at 17:08
  • @elgonzo tried parsing and counting top level properties, but no luck. After parsing I got same json object. – kgzdev Nov 07 '18 at 08:26
  • Are you saying, you have a number of files, and you want to determine which ones are JSON Schemas and which are just JSON? If so, yeah I think you've already worked it out. Any key words which are not part of the spec should be ignored, so in theory ANY JSON is a "valid JSON Schema", although `{}` simply means "anything is allowed". This is fundamental to JSON Schema. Please feel free to join the official Slack channel form the JSON Schema site - JSON Schema core. – Relequestual Nov 07 '18 at 09:36
  • Did you parse as schema, i.e. `var sch = JSchema.Parse(json);`? I did just test it myself (using the json files from your question). If i do that with your 1st json file (your schema), i get 1 for `sch.Properties.Count`; and if i do that with your 2nd json file, then `sch.Properties.Count` will be 0. (But keep in mind that this is not 100% bullet-proof, as i mentioned already. If you parse some other json file/object and it one of its top-level properties coincide with a valid json schema property, well, bad luck) –  Nov 07 '18 at 18:48
  • @elgonzo I will try your solution, will let you know. – kgzdev Nov 08 '18 at 12:17
  • @elgonzo your approach solved my problem, can you please add it as answer, thanks – kgzdev Nov 14 '18 at 15:02

0 Answers0