feat(util): add validateResultNumber helper - #874
Conversation
Adds a shared helper to validate raw numeric provider values before Number() coercion. Invalid values (null, undefined, NaN, empty strings, non-numeric types, infinity) produce a 502 provider error response. 0 is accepted by default with an opt-out flag. Includes unit tests covering the behavior table.
|
👋 danwilliams-cll, thanks for creating this pull request! To help reviewers, please consider creating future PRs as drafts first. This allows you to self-review and make any final changes before notifying the team. Once you're ready, you can mark it as "Ready for review" to request feedback. Thanks! |
NPM Publishing labels 🏷️🔵 This PR has the |
| isWrongType || trimmed === '' || !Number.isFinite(num) || (!acceptZeroValue && num === 0) | ||
|
|
||
| if (isInvalid) { | ||
| logger.warn(`${errorMessage} (received: ${JSON.stringify(value)})`) |
There was a problem hiding this comment.
We probably should avoid stringifying in logs, should be able to pass this out as an object (`{ msg: "asd", otherprop: obj })and it will only be parsed to a string if the log level is set to it (usually a warn will do it anyways but just as a general comment
| /** | ||
| * Type guard for the error branch of ValidatedResultNumber. | ||
| */ | ||
| export const isProviderError = ( | ||
| v: ValidatedResultNumber, | ||
| ): v is { statusCode: 502; errorMessage: string } => 'statusCode' in v |
There was a problem hiding this comment.
This is only used for tests, should live below
Adds a reusable helper to validate numeric provider results before coercion, returning a structured 502 error for null/undefined/NaN/empty/objects/arrays/booleans/Infinity. Includes behavior-table unit tests.