Fix falsy option arguments skipping validation - #91
Merged
Conversation
Optional arguments were guarded with `if (value)` and rendered with `value || ""`, so falsy but invalid values such as `null` or an empty color skipped validation and were silently rendered as omitted arguments. Check for `undefined` instead, matching `gradient` and `progressive_blur`. Also validate `colorize.color` and `monochrome.color` as hex colors, and tie the `jpeg_options` progressive cross-check to `optimize_scans` being enabled rather than merely present. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01G8Aa3xeVUCaxKtQJDzZhMo
Fields belonging to one gravity variant are now typed as forbidden
(`?: never`) on the other variants, so invalid combinations such as
`{type: "sm", x_offset: 0}` fail to compile instead of only throwing
at runtime. This also makes cross-variant property access legal on the
union, removing all six `@ts-expect-error` suppressions from the
gravity builder. Runtime behavior is unchanged.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014gcu8oHhTciugXNPUYNoqK
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Third and last finding from the audit that produced #89 and #90: optional arguments guarded with
if (value)and rendered withvalue || "", so any falsy value was treated as "not provided" — including invalid ones.Swept across 11 modules:
resize,size,watermark,watermarkSize,trim,unsharpMasking,pngOptions,jpegOptions,colorize,monochrome,gravity.What changes
null,""and out-of-range0reach their guards instead of slipping past them.0is rendered rather than dropped.{ resize: { resizing_type: "fit", width: 0, height: 100 } }now buildsrs:fit:0:100instead ofrs:fit::100. Both forms mean the same thing to imgproxy —0and an omitted argument are both "calculate this dimension" — so no URL changes meaning. This is unlike Fix gradient dropping optional arguments set to 0 #89, where the dropped0genuinely differed from the default.colorize.colorandmonochrome.colorare now validated as hex colors. Neither had any type validation at all; they now matchtrim.colorandgradient.color.gravityoffset checks use presence, so{ type: "sm", x_offset: 0 }correctly raisesgravity.type is invalid— previously only a non-zero offset triggered that check.Bug found while sweeping
jpeg_optionsthrew "progressive must be true if optimize_scans is true" wheneveroptimize_scanswas merely present. With the truthiness guard that was masked; converting it to a presence check exposed it, so the cross-check is now tied tooptimize_scansactually being enabled.{ progressive: false, optimize_scans: false }is valid and now buildsjpgo:false::::false.Second commit:
Gravityis now an exclusive union (minor)The gravity builder's cross-variant checks needed six
@ts-expect-errorsuppressions, because fields likex_offsetorclass_namesexist on only one member of theGravityunion. Instead of suppressing, the union is now exclusive: each variant types the other variants' fields as?: never.{ type: "sm", x_offset: 0 }or{ type: "no", x: 0.5 }are now compile-time errors for TypeScript users. They have always thrown at runtime, so any code this breaks was already broken — it just failed later.src/options/gravity.tsare removed; runtime behavior is unchanged.Verification
tsc --noEmitclean,eslint src testscleanvitest run→ 97 files / 1114 tests passing (19 new, at least one per swept module)@ts-expect-errorto an existing test — the stricter union now flagsy: 0.5on a non-fpgravity independently ofx, which previously slipped past the excess-property check.Related
gradient, where it changed the resulting imagevideo_thumbnail_animationemittingundefined, andzoom: 0being dropped🤖 Generated with Claude Code
https://claude.ai/code/session_014gcu8oHhTciugXNPUYNoqK