InterviewDB Question

Config Validator - Parse and Validate a Nested Configuration Schema

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

  1. How do you handle list fields where each element must match a sub-schema?
  2. Add support for enum constraints ("allowed_values": ["dev", "prod"]).
  3. How would you report all errors at once vs. failing fast on the first error?
  4. 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

  1. How do you handle list fields where each element must match a sub-schema?
  2. Add support for enum constraints ("allowed_values": ["dev", "prod"]).
  3. How would you report all errors at once vs. failing fast on the first error?
  4. 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