runtime
194 / 500
question 194 CSV JSON & JSONL · medium

Validate a service's JSON settings

A service refuses to start when its settings file is wrong, but the crash message is unhelpful. Write a validator that lists every problem in plain words before the service tries to start.

Functionconfig_errors(text)
Invalid JSONreturn only ["invalid JSON at line <lineno>: <msg>"], using the JSONDecodeError's lineno and msg
Not an objectreturn only ["settings must be a JSON object"]
namerequired; must be a non-empty string, else "name must be a non-empty string"
portrequired; must be a whole number from 1 to 65535 (not true, not 8080.0, not "8080"), else "port must be a whole number from 1 to 65535"
debugoptional; if present must be true or false, else "debug must be true or false"
Returnsthe problems in the order name, port, debug; [] when the settings are valid
Examples
callconfig_errors('{"name": "orders-api", "port": 8080, "debug": false}')returns[]
callconfig_errors('{"name": "", "port": 70000}')returns['name must be a non-empty string', 'port must be a whole number from 1 to 65535']
callconfig_errors('{"name": "orders-api", "port": 8080,}')returns['invalid JSON at line 1: Illegal trailing comma before end of object']

Loading your progress…

Validate a service's JSON settings — Python practice · Runtime