Rebasely Beta: data may be purged

justintypescript-ultra-strict Public

Markdown · 1.1 KiB · at 9dcde16

Write parseConfig, which turns a raw object (already parsed from JSON or YAML) into a typed service config.

Fields:

  • host: required, a string that is non-empty after trimming. The returned host is trimmed.
  • port: optional, defaults to 8080. If present it must be an integer from 1 to 65535. A numeric string such as "8080" is not accepted.
  • debug: optional boolean, defaults to false.
  • timeoutMs: optional. If present it must be a positive integer. If absent the config has no timeout.
  • tags: optional array of strings, defaults to an empty array. Every element must be a string.

A field whose value is null or undefined counts as missing. Unknown fields are ignored.

Errors (report the first problem found, checking fields in the order listed above):

  • { kind: "missing", field } when host is missing.
  • { kind: "wrong_type", field } when a present field has the wrong JavaScript type (for tags, also when an element is not a string).
  • { kind: "out_of_range", field } when a field has the right type but an invalid value (empty host, non-integer or out-of-range port, non-positive or non-integer timeout).