InterviewDB
Question
Config Validator - Parse and Validate a Nested Configuration Schema
phone
Question Details
Problem
Implement a configuration validator. Given a JSON config dict and a schema definition, validate the config and return a list of validation errors (empty list = valid).
Schema field types: "string", "int", "bool", "list", "object" (nested). Fields can be marked required: true. Objects have nested schemas.
python
def validate_config(config: dict, schema: dict) -> List[str]:
**Returns** list of error messages, e.g.:
# ["Missing required field: database.host",
# "Field timeout: expected int, got str"]
...
Example:
schema = {
"database": {"type":"object","required":True,"schema":{
"host": {"type":"string","required":True},
"port": {"type":"int", "required":True},
"timeout": {"type":"int", "required":False}
}},
"debug": {"type":"bool","required":False}
}
config = {"database": {"host": "localhost", "port": "3306"}, "debug": True}
validate_config(config, schema)
-> ["database.port: expected int, got str"]
Follow-ups
- How do you handle
listfields where each element must match a sub-schema? - Add support for enum constraints (
"allowed_values": ["dev", "prod"]). - How would you report all errors at once vs. failing fast on the first error?
- How would you generate a default config from the schema (filling in optional fields with defaults)?
Full Details
Problem
Implement a configuration validator. Given a JSON config dict and a schema definition, validate the config and return a list of validation errors (empty list = valid).
Schema field types: "string", "int", "bool", "list", "object" (nested). Fields can be marked required: true. Objects have nested schemas.
python
def validate_config(config: dict, schema: dict) -> List[str]:
**Returns** list of error messages, e.g.:
# ["Missing required field: database.host",
# "Field timeout: expected int, got str"]
...
Example:
schema = {
"database": {"type":"object","required":True,"schema":{
"host": {"type":"string","required":True},
"port": {"type":"int", "required":True},
"timeout": {"type":"int", "required":False}
}},
"debug": {"type":"bool","required":False}
}
config = {"database": {"host": "localhost", "port": "3306"}, "debug": True}
validate_config(config, schema)
-> ["database.port: expected int, got str"]
Follow-ups
- How do you handle
listfields where each element must match a sub-schema? - Add support for enum constraints (
"allowed_values": ["dev", "prod"]). - How would you report all errors at once vs. failing fast on the first error?
- How would you generate a default config from the schema (filling in optional fields with defaults)?
Free preview. Unlock all questions →
Topics
Coding
Onsite
Phone
More from LinkedIn
Reddit
UPDATE: HR specialist viewed my linkedin profile after final interview?
Reddit
[Insights, advice, and my experience] Got offers from Zapier, Samsara, and PayPal
Reddit
Correlation to Hiring Manager viewing profile, and made a hire?
Reddit
Rejected from job and then got a request to chat over LinkedIn?
Reddit
What's the vibe?