refactor(settings): dedupe positive-integer env-var parsing - #5250
Merged
Conversation
toPositiveIntegerOrDefault and toPositiveIntegerOrUndefined carried the same parse-and-validate body, differing only in what they return for an unset value. Implement the default variant in terms of the undefined one.
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.
Source: reduction — hunted for validation gaps in
apps/api/src/settings/resolve-config.ts(the DUCKDB_THREADS validation gap I found there is already covered by open PR #5130, so I didn't duplicate it) and found this duplication instead while reading the file end to end.toPositiveIntegerOrDefaultandtoPositiveIntegerOrUndefinedhad byte-identical parse-and-throw bodies (parseNumber(value), validateNumber.isSafeInteger(...) && > 0, throw"${name} must be a positive integer"), differing only in what they return whenvalueis unset/empty (a caller-supplied default vs.undefined). NowtoPositiveIntegerOrDefaultjust delegates totoPositiveIntegerOrUndefined(...) ?? defaultValue— same validation, same error message, same call sites (PORT,DATABASE_POOL_MAX,CLICKHOUSE_MAX_MEMORY_USAGE,NATS_TUNNEL_SESSION_TTL_SECONDS,STUDIO_SEAT_ALLOWANCE_CENTS,STUDIO_TOPUP_FEE_PERCENT), net -6 lines.Behavior is unchanged: all 58 existing tests in
resolve-config.test.tspass without modification, including theit.each(["abc", "0", "-1", "1.5", "Infinity"])cases that exercise both the default- and undefined-returning call sites.To confirm:
bun test apps/api/src/settings/resolve-config.test.ts.Locally ran:
bun run fmt,cd apps/api && bunx tsc --noEmit(clean), and the targeted test file above (58 pass). Full CI validates the rest.Summary by cubic
Deduped positive-integer env var parsing by having
toPositiveIntegerOrDefaultcalltoPositiveIntegerOrUndefinedand apply the default. Validation and error messages stay the same; behavior is unchanged.toPositiveIntegerOrUndefined.toPositiveIntegerOrDefaultnow returnstoPositiveIntegerOrUndefined(...) ?? defaultValue.Written for commit d413b4c. Summary will update on new commits.