JSON Schema Object Validator
Learn this problemProblem statement
Implement validateObject for a small object-schema language. The inputs schemaJson and objectJson are JSON strings. Return false if either string is malformed or violates the grammar below; otherwise return whether the candidate object satisfies the schema.
Schema grammar
- The schema root has a nonempty string
_typeand apropertiesarray. - Each property rule has a unique nonempty string
name, atypeequal tostring,number, orboolean, and an optional booleanrequiredthat defaults tofalse. - A string rule may include
validation, an ASCII regular expression matched against the entire candidate string. - A number rule may include
limitin the formlow-high. Both endpoints are JSON numbers, may be negative, and are inclusive. For example,-10--1represents the interval from-10through-1. - Fields not described above make the schema invalid. A rule may not use
validationorlimitwith an incompatible type.
Candidate rules
- The candidate root is an object whose nonempty string
_typeexactly equals the schema type. - Every required property must be present. An optional property may be absent.
- Every key other than
_typemust have a schema rule; unknown properties are rejected. - Each present value must have the rule's exact primitive JSON type. A boolean is not a number.
- String regular expressions use full-string matching, and number limits include both endpoints.
For deterministic cross-language behavior, all input characters and decoded JSON strings are printable ASCII, JSON objects may not contain duplicate keys, numbers have at most 12 significant digits with absolute value at most 10^9, the schema contains at most 100 property rules, and recursive JSON nesting is at most 8 levels.
Function
validateObject(schemaJson: String, objectJson: String) → booleanExamples
Example 1
schemaJson = "{\"_type\":\"Person\",\"properties\":[{\"name\":\"name\",\"type\":\"string\",\"validation\":\"[A-Za-z]+\",\"required\":true},{\"name\":\"age\",\"type\":\"number\",\"limit\":\"10-100\"}]}"objectJson = "{\"_type\":\"Person\",\"name\":\"Steven\",\"age\":35}"return = trueThe type matches, the required name is present and matches the full regular expression, and 35 lies inside the inclusive numeric range.
Example 2
schemaJson = "{\"_type\":\"Person\",\"properties\":[{\"name\":\"name\",\"type\":\"string\",\"required\":true}]}"objectJson = "{\"_type\":\"Dog\",\"name\":\"Steven\"}"return = falseThe candidate type Dog does not equal the schema type Person.
Example 3
schemaJson = "{\"_type\":\"Person\",\"properties\":[{\"name\":\"name\",\"type\":\"string\"}]}"objectJson = "{\"_type\":\"Person\",\"name\":\"Steven\",\"address\":\"unknown\"}"return = falseThe schema has no rule for address, so the unknown property is rejected.
Example 4
schemaJson = "{\"_type\":\"Person\",\"properties\":[]}"objectJson = "{not valid JSON}"return = falseThe candidate input is malformed JSON.
Constraints
1 <= schemaJson.length, objectJson.length <= 20000.- Every input character and decoded JSON string is printable ASCII.
- Numbers and numeric endpoints have at most
12significant digits and absolute value at most10^9. - The schema contains at most
100property rules with unique names. - JSON objects may not contain duplicate keys.
- Recursive JSON nesting is at most
8levels. - A
validationpattern uses the common ASCII subset of regular expressions supported by Java, Python, and C++ ECMAScript syntax.