-
Notifications
You must be signed in to change notification settings - Fork 1
Add type guard for Yup ValidationError before accessing inner property #1000
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -65,6 +65,18 @@ export type CheckExchangePolicyRules = { | |||||||||||||||||
| }[]; | ||||||||||||||||||
| }; | ||||||||||||||||||
|
|
||||||||||||||||||
| // Type guard to check if an error is a Yup ValidationError with inner errors | ||||||||||||||||||
| function isValidationErrorWithInner( | ||||||||||||||||||
| error: unknown | ||||||||||||||||||
| ): error is { inner: Array<{ message: string; path?: string; value?: unknown }> } { | ||||||||||||||||||
| return ( | ||||||||||||||||||
| typeof error === "object" && | ||||||||||||||||||
| error !== null && | ||||||||||||||||||
| "inner" in error && | ||||||||||||||||||
| Array.isArray((error as { inner: unknown }).inner) | ||||||||||||||||||
| ); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| export function checkExchangePolicy( | ||||||||||||||||||
| offerData: OfferFieldsFragment, | ||||||||||||||||||
| rules: CheckExchangePolicyRules | ||||||||||||||||||
|
|
@@ -109,10 +121,18 @@ export function checkExchangePolicy( | |||||||||||||||||
| } catch (e) { | ||||||||||||||||||
| result = { | ||||||||||||||||||
| isValid: false, | ||||||||||||||||||
| errors: | ||||||||||||||||||
| e.inner?.map((error) => { | ||||||||||||||||||
| return { ...error }; | ||||||||||||||||||
| }) || [] | ||||||||||||||||||
| errors: isValidationErrorWithInner(e) | ||||||||||||||||||
| ? e.inner.map((error) => { | ||||||||||||||||||
| return { ...error }; | ||||||||||||||||||
| }) | ||||||||||||||||||
|
Comment on lines
+125
to
+127
|
||||||||||||||||||
| ? e.inner.map((error) => { | |
| return { ...error }; | |
| }) | |
| ? e.inner.map((error) => ({ | |
| message: error.message, | |
| path: error.path ?? "", | |
| value: error.value | |
| })) |
Copilot
AI
Feb 13, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are no test cases covering the new error handling path for non-Yup ValidationError scenarios. The fallback error handling (lines 128-135 and 171-180) that returns "Validation failed with unknown error" is not tested. Consider adding test cases to verify that errors without the inner property are handled correctly and return the expected error structure.
Copilot
AI
Feb 13, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When spreading Yup ValidationError objects with the spread operator, additional properties beyond message, path, and value may be copied into the result. While this may not cause immediate issues, it could lead to unexpected behavior if the result is strictly validated elsewhere or if consumers rely on the type definition. Consider explicitly mapping only the expected properties to ensure the result structure exactly matches the CheckExchangePolicyResult type.
| return { ...error }; | |
| return { | |
| message: error.message, | |
| path: error.path ?? "", | |
| value: error.value | |
| }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The type guard's return type declares
path?: stringandvalue?: unknownas optional, but theCheckExchangePolicyResulttype (lines 5-12) requires these properties to be non-optional (path: stringandvalue: unknown). This type mismatch could lead to runtime errors when consumers expect these properties to always be present. Additionally, the type guard only validates thatinneris an array but doesn't verify that the array elements have the required structure with amessageproperty.