MediumPro challengePythonJavaScriptTypeScript

Env Schema Validator

Node.jsValidationConfig

Validate process.env-like object against a schema.
Schema: { key: { type: 'string'|'number'|'boolean', required: bool, default? } }.

  • string: kept as-is.
  • number: parse with Number(), fail if NaN.
  • boolean: 'true'/'1' → true; 'false'/'0' → false; else fail.
  • required + missing → throw with message 'Missing: <key>'.
  • Apply defaults if missing.

solve(schema, env) returns parsed object, or throws.

Sample tests

Test #1Optional missing skipped
Input: [{"X":{"type":"number","required":false}},{}]
Output: {}
Test #2Number parsed
Input: [{"PORT":{"type":"number","required":true}},{"PORT":"3000"}]
Output: {"PORT":3000}
Test #3Default used
Input: [{"DEBUG":{"type":"boolean","default":false}},{}]
Output: {"DEBUG":false}
Test #4String passthrough
Input: [{"NAME":{"type":"string","required":true}},{"NAME":"app"}]
Output: {"NAME":"app"}
Test #5Boolean from "1"
Input: [{"FLAG":{"type":"boolean"}},{"FLAG":"1"}]
Output: {"FLAG":true}