(name, selector, has)
| 995 | // selector has distinct values and at most one default, a structural selector |
| 996 | // dispatches on something. Keeps a malformed selector from shipping silently. |
| 997 | function checkSelector(name, selector, has) { |
| 998 | const errors = [] |
| 999 | if (!selector) return [`${name}: union has no selector`] |
| 1000 | if (selector.correlated) return [] // resolved by request id, not the payload — nothing to dispatch |
| 1001 | if (selector.by) { |
| 1002 | const values = selector.variants.map((v) => JSON.stringify(v.value)) |
| 1003 | if (new Set(values).size !== values.length) errors.push(`${name}: selector has duplicate discriminator values`) |
| 1004 | for (const v of selector.variants) |
| 1005 | if (!has(v.ref)) errors.push(`${name}: selector variant ${v.ref} does not resolve`) |
| 1006 | if (selector.default && !has(selector.default)) |
| 1007 | errors.push(`${name}: selector default ${selector.default} does not resolve`) |
| 1008 | } else if (selector.ordered) { |
| 1009 | // A structural selector must actually dispatch from the payload: every arm needs |
| 1010 | // a distinguishing required field, and no arm's `requires` may be a subset of a |
| 1011 | // later arm's — that would shadow the later arm under first-match. A union that |
| 1012 | // cannot satisfy this is not payload-dispatchable and must be `correlated`. |
| 1013 | selector.ordered.forEach((v, i) => { |
| 1014 | if (!has(v.ref)) errors.push(`${name}: selector variant ${v.ref} does not resolve`) |
| 1015 | if (!v.requires.length) |
| 1016 | errors.push(`${name}: structural selector arm ${v.ref} has no required fields to dispatch on`) |
| 1017 | for (let j = i + 1; j < selector.ordered.length; j++) { |
| 1018 | const w = selector.ordered[j] |
| 1019 | if (v.requires.length && w.requires.length && v.requires.every((k) => w.requires.includes(k))) |
| 1020 | errors.push(`${name}: structural selector arm ${v.ref} shadows ${w.ref} (requires is a subset)`) |
| 1021 | } |
| 1022 | }) |
| 1023 | } else { |
| 1024 | errors.push(`${name}: selector is neither discriminated, structural, nor correlated`) |
| 1025 | } |
| 1026 | return errors |
| 1027 | } |
| 1028 | |
| 1029 | // ============================================================ |
| 1030 | // CLI: raw ast + model → flat schema (validated) |
no test coverage detected