Skip to content

fix: coerce numeric enum flags - #840

Open
margaretjgu wants to merge 1 commit into
mainfrom
fix/numeric-union-flags
Open

margaretjgu wants to merge 1 commit into
mainfrom
fix/numeric-union-flags

Conversation

@margaretjgu

Copy link
Copy Markdown
Member

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

@margaretjgu margaretjgu added the auto-loop Opt this PR into the CI and review repair loop label Dec 2, 2026
Copilot AI balanced review requested due to automatic review settings December 2, 2026 22:13
@github-actions

github-actions Bot commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

👋 This PR can use the first-failure repair loop on GitHub Actions and Buildkite.

Labels

  • auto-loop: triage the first CI or Buildkite failure and commit in-repo fixes (also AI review and CodeQL)
  • skip-ai-review: silences the review bot
  • skip-auto-loop: halt the repair loop (also added when you comment /stop)

Comments

  • /stop: halt the repair loop
  • /bad on a finding: the bot writes a short do not re-flag line from the finding and your note into .github/skills/ai-review-memory.md. A reason is optional.
    • Example: /bad false positive. This path is already encoded.

@margaretjgu margaretjgu removed the auto-loop Opt this PR into the CI and review repair loop label Dec 2, 2026
@github-actions

github-actions Bot commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

✅MegaLinter analysis: Success

Descriptor Linter Files Fixed Errors Warnings Elapsed time
✅ COPYPASTE jscpd yes no no 0.6s
✅ REPOSITORY gitleaks yes no no 62.88s
✅ REPOSITORY git_diff yes no no 0.19s
✅ REPOSITORY secretlint yes no no 15.49s
✅ REPOSITORY trivy yes no no 30.41s
✅ TYPESCRIPT eslint 3 0 0 5.59s

Notices

📣 MegaLinter 9.5.0 is out! Discover the new features and security recommendations in the release announcement. (Skip this info by defining SECURITY_SUGGESTIONS: false)

See detailed reports in MegaLinter artifacts
Set VALIDATE_ALL_CODEBASE: true in mega-linter.yml to validate all sources, not only the diff

MegaLinter is graciously provided by OX Security
Show us your support by starring ⭐ the repository

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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

Comment thread src/lib/ajv-validate.ts
}
return acc
}
const rawType = Array.isArray(resolved.type) ? resolved.type[0] : resolved.type
Comment thread src/lib/ajv-validate.ts
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)
Comment thread src/lib/ajv-validate.ts
Comment on lines +246 to +248
if (data != null && typeof data === 'object' && !Array.isArray(data)) {
coerceNumericEnumUnions(compilable, data)
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants