Decode Messages from unknown
Hard · Narrowing
Write decodeMessages(input: unknown) that validates untrusted JSON and builds the starter's Message union.
- input must be an array, else the only error is "$: expected array".
- Item i must be a plain object ("$[i]: expected object") with a known type ("$[i].type: unknown message type"); otherwise skip the rest of it.
- Then check id and the fields of that type in starter order: numbers must be positive integers, strings non-empty, url must start with https://, options must be an array of 2 to 4 items, each checked as options[j]. Format: "$[i].<field>: expected positive integer" (or non-empty string, https url, 2-4 options).
- An id used by an earlier item: "$[i].id: duplicate id".
Return { ok: false, errors } (all errors, in order) or { ok: true, messages } with only each type's fields.