Repository navigation
fix: coerce numeric enum flags - #840
margaretjgu wants to merge 1 commit into
Conversation
|
👋 This PR can use the first-failure repair loop on GitHub Actions and Buildkite. Labels
Comments
|
✅MegaLinter analysis: Success
Notices📣 MegaLinter 9.5.0 is out! Discover the new features and security recommendations in the release announcement. (Skip this info by defining See detailed reports in MegaLinter artifacts MegaLinter is graciously provided by OX Security |
There was a problem hiding this comment.
🟡 Changes recommended
Coercion can turn valid strings into invalid numbers because matching enum values and string alternatives within multi-type schemas are not preserved.
2 open issues
What changed in this PR
Addresses the numeric-flag portion of #839 by coercing numeric strings for number-or-enum schemas in shared input validation.
Changes:
- Adds numeric coercion with same-document reference support.
- Adds validator and command-level regression tests.
| File | Description |
|---|---|
test/lib/ajv-validate.test.ts |
Tests coercion, enum preservation, references, and invalid inputs. |
test/factory.test.ts |
Tests numeric-or-enum flags through command execution. |
src/lib/ajv-validate.ts |
Classifies schema alternatives and coerces numeric strings before validation. |
Review effort: Balanced
| } | ||
| return acc | ||
| } | ||
| const rawType = Array.isArray(resolved.type) ? resolved.type[0] : resolved.type |
| if (typeof value !== 'string' || !CANONICAL_NUMBER.test(value)) continue | ||
| if (prop == null || typeof prop !== 'object') continue | ||
| const kinds = unionKinds(schema, prop, new Set()) | ||
| if (kinds.number && kinds.enum && !kinds.freeString) data[key] = Number(value) |
| if (data != null && typeof data === 'object' && !Array.isArray(data)) { | ||
| coerceNumericEnumUnions(compilable, data) | ||
| } |
There was a problem hiding this comment.
This adds a second union classifier. unionKinds/derefNode repeat what resolveType in src/lib/json-schema-args.ts already does for flags (anyOf/oneOf/$ref/const). If one of them changes, the other can drift, and the flag type and coercion rules would disagree.
I'd reuse the existing classification instead. For example, have resolveType report "number-or-enum, no free string" through a shared helper, and have coerceNumericEnumUnions call that helper rather than unionKinds.

Coerce a numeric string when the flag schema is a number or an enum. Relates to #839.