Problem · Parsing

JSON Schema Object Validator

Learn this problem
HardDocuSign logoDocuSignFULLTIMEONSITE INTERVIEW

Problem 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 _type and a properties array.
  • Each property rule has a unique nonempty string name, a type equal to string, number, or boolean, and an optional boolean required that defaults to false.
  • A string rule may include validation, an ASCII regular expression matched against the entire candidate string.
  • A number rule may include limit in the form low-high. Both endpoints are JSON numbers, may be negative, and are inclusive. For example, -10--1 represents the interval from -10 through -1.
  • Fields not described above make the schema invalid. A rule may not use validation or limit with an incompatible type.

Candidate rules

  • The candidate root is an object whose nonempty string _type exactly equals the schema type.
  • Every required property must be present. An optional property may be absent.
  • Every key other than _type must 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) → boolean

Examples

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 = true

The 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 = false

The 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 = false

The schema has no rule for address, so the unknown property is rejected.

Example 4

schemaJson = "{\"_type\":\"Person\",\"properties\":[]}"objectJson = "{not valid JSON}"return = false

The 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 12 significant digits and absolute value at most 10^9.
  • The schema contains at most 100 property rules with unique names.
  • JSON objects may not contain duplicate keys.
  • Recursive JSON nesting is at most 8 levels.
  • A validation pattern uses the common ASCII subset of regular expressions supported by Java, Python, and C++ ECMAScript syntax.

More DocuSign problems

drafts saved locally
public boolean validateObject(String schemaJson, String objectJson) {
  // write your code here
}
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}"
expectedtrue
checking account