JSON Identity Verification
Learn this problemProblem statement
Implement identity verification for one provider response. The array expected contains exactly four strings in this order: first name, last name, date of birth, and Social Security number.
The string providerJson must be a valid JSON object with this required structure:
{"response":{"identity":{"first_name":"...","last_name":"...","date_of_birth":"...","ssn":"..."},"error_codes":["..."]}}Object keys may appear in any order, insignificant JSON whitespace is allowed, and unrelated extra fields are ignored. Each required identity value must be a JSON string, and error_codes must be an array containing only strings.
Compare all four identity values exactly as supplied. Do not trim whitespace, fold letter case, or reformat dates or Social Security numbers.
Return one canonical JSON string with a boolean verified field followed by a reasons array. If either the JSON syntax or any required path or type is invalid, return only the reason malformed_json. Otherwise append applicable reasons in this exact order:
first_namelast_namedate_of_birthssnprovider_error, once whenerror_codesis non-empty
Set verified to true exactly when the reason list is empty. The returned string contains no spaces.
Function
verifyIdentity(expected: String[], providerJson: String) → StringExamples
Example 1
expected = ["Ada","Lovelace","1815-12-10","123-45-6789"]providerJson = "{\"response\":{\"identity\":{\"first_name\":\"Ada\",\"last_name\":\"Lovelace\",\"date_of_birth\":\"1815-12-10\",\"ssn\":\"123-45-6789\"},\"error_codes\":[]}}"return = "{\"verified\":true,\"reasons\":[]}"Every required provider value exactly matches the corresponding expected string, and the provider supplied no error code, so verification succeeds.
Example 2
expected = ["Grace","Hopper","1906-12-09","111-22-3333"]providerJson = "{\"response\":{\"error_codes\":[\"UPSTREAM_TIMEOUT\"],\"identity\":{\"ssn\":\"999-88-7777\",\"date_of_birth\":\"1906-12-09\",\"last_name\":\"hopper\",\"first_name\":\"Grace\"}}}"return = "{\"verified\":false,\"reasons\":[\"last_name\",\"ssn\",\"provider_error\"]}"The last name differs because comparison is case-sensitive, the Social Security number differs, and the non-empty provider error list adds one final provider_error reason.
Example 3
expected = ["Alan","Turing","1912-06-23","222-33-4444"]providerJson = "{\"response\":{\"identity\":{\"first_name\":\"Alan\"},\"error_codes\":[]}}"return = "{\"verified\":false,\"reasons\":[\"malformed_json\"]}"The JSON syntax is valid, but required identity fields are missing. The response therefore follows the malformed-input result and does not attempt partial comparison.
Constraints
expected.length == 4.- Each expected identity string has length from
0through100. 1 <= providerJson.length <= 10^5.providerJsoncontains valid Unicode text and may contain any JSON value before structural validation.- A structurally valid provider response has unique object keys, the required object path shown above, four required string identity values, and a string-only
error_codesarray. - The canonical result contains only the fixed reason codes defined above.