Skip to content

Add type guard for Yup ValidationError before accessing inner property#1000

Merged
levalleux-ludo merged 3 commits into998-check-exchangepolicy-for-bundlefrom
copilot/sub-pr-999
Feb 13, 2026
Merged

Add type guard for Yup ValidationError before accessing inner property#1000
levalleux-ludo merged 3 commits into998-check-exchangepolicy-for-bundlefrom
copilot/sub-pr-999

Conversation

Copy link
Contributor

Copilot AI commented Feb 13, 2026

The code accessed e.inner without verifying the error type, risking runtime failures if non-Yup errors are thrown during validation.

Changes

  • Type guard function: Added isValidationErrorWithInner() to safely check for Yup ValidationError with inner array
  • Fallback error handling: Returns structured error object with message when error lacks inner property
  • Applied to two catch blocks: Main validation (lines 119-137) and bundle item validation (lines 162-182)
// Before
catch (e) {
  result = {
    isValid: false,
    errors: e.inner?.map((error) => ({ ...error })) || []
  };
}

// After
catch (e) {
  result = {
    isValid: false,
    errors: isValidationErrorWithInner(e)
      ? e.inner.map((error) => ({ ...error }))
      : [{ message: e instanceof Error ? e.message : "Validation failed with unknown error", path: "", value: undefined }]
  };
}

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

Copilot AI and others added 2 commits February 13, 2026 12:18
…erty

Co-authored-by: levalleux-ludo <7184124+levalleux-ludo@users.noreply.github.com>
Co-authored-by: levalleux-ludo <7184124+levalleux-ludo@users.noreply.github.com>
Copilot AI changed the title [WIP] Update check exchange policy for bundle offers PR Add type guard for Yup ValidationError before accessing inner property Feb 13, 2026
Copilot AI requested a review from levalleux-ludo February 13, 2026 12:21
@levalleux-ludo levalleux-ludo marked this pull request as ready for review February 13, 2026 13:30
Copilot AI review requested due to automatic review settings February 13, 2026 13:30
@levalleux-ludo levalleux-ludo merged commit 033ea70 into 998-check-exchangepolicy-for-bundle Feb 13, 2026
1 check was pending
@levalleux-ludo levalleux-ludo deleted the copilot/sub-pr-999 branch February 13, 2026 13:30
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR improves error handling in the checkExchangePolicy function by adding a type guard to safely check for Yup ValidationError with an inner array before accessing it. This prevents potential runtime failures when non-Yup errors are thrown during validation.

Changes:

  • Added isValidationErrorWithInner() type guard function to safely detect Yup ValidationError objects
  • Updated two catch blocks to use the type guard with fallback error handling for non-Yup errors
  • Minor package-lock.json update (yaml package marked as peer dependency)

Reviewed changes

Copilot reviewed 1 out of 2 changed files in this pull request and generated 4 comments.

File Description
packages/core-sdk/src/offers/checkExchangePolicy.ts Adds type guard function and applies it to validation error handling in two catch blocks with fallback error structures
package-lock.json Automatic lockfile update marking yaml as peer dependency (unrelated to main PR changes)

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

}) || []
isValidationErrorWithInner(e)
? e.inner.map((error) => {
return { ...error };
Copy link

Copilot AI Feb 13, 2026

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.

Suggested change
return { ...error };
return {
message: error.message,
path: error.path ?? "",
value: error.value
};

Copilot uses AI. Check for mistakes.
Comment on lines +128 to +135
: [
{
message:
e instanceof Error ? e.message : "Validation failed with unknown error",
path: "",
value: undefined
}
]
Copy link

Copilot AI Feb 13, 2026

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 uses AI. Check for mistakes.
Comment on lines +71 to +77
): 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)
);
Copy link

Copilot AI Feb 13, 2026

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?: string and value?: unknown as optional, but the CheckExchangePolicyResult type (lines 5-12) requires these properties to be non-optional (path: string and value: unknown). This type mismatch could lead to runtime errors when consumers expect these properties to always be present. Additionally, the type guard only validates that inner is an array but doesn't verify that the array elements have the required structure with a message property.

Suggested change
): 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)
);
): error is { inner: Array<{ message: string; path: string; value: unknown }> } {
if (
typeof error !== "object" ||
error === null ||
!("inner" in error) ||
!Array.isArray((error as { inner: unknown }).inner)
) {
return false;
}
const { inner } = error as { inner: unknown[] };
return inner.every((item) => {
if (typeof item !== "object" || item === null) {
return false;
}
const candidate = item as {
message?: unknown;
path?: unknown;
value?: unknown;
};
return (
typeof candidate.message === "string" &&
typeof candidate.path === "string" &&
"value" in candidate
);
});

Copilot uses AI. Check for mistakes.
Comment on lines +125 to +127
? e.inner.map((error) => {
return { ...error };
})
Copy link

Copilot AI Feb 13, 2026

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.

Suggested change
? e.inner.map((error) => {
return { ...error };
})
? e.inner.map((error) => ({
message: error.message,
path: error.path ?? "",
value: error.value
}))

Copilot uses AI. Check for mistakes.
levalleux-ludo added a commit that referenced this pull request Feb 13, 2026
* feat: adapt check exchange policy for bundle offers

* fix exchangePolicy name/version/returnPeriod in OfferPolicyDetails when bundle offer

* Update packages/react-kit/src/hooks/useCheckExchangePolicy.ts

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update packages/core-sdk/src/offers/checkExchangePolicy.ts

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update packages/core-sdk/src/offers/checkExchangePolicy.ts

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* copilot pr remarks

* Update packages/react-kit/src/components/offerPolicy/OfferPolicyDetails.tsx

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update packages/react-kit/src/hooks/useCheckExchangePolicy.ts

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* copilot pr remarks

* copilot pr remarks

* copilot pr remarks

* Update packages/core-sdk/src/offers/checkExchangePolicy.ts

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update packages/core-sdk/src/offers/checkExchangePolicy.ts

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update packages/react-kit/src/components/offerPolicy/OfferPolicyDetails.tsx

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update data/exchangePolicies/exchangePolicyRules.template.json

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update packages/core-sdk/src/offers/checkExchangePolicy.ts

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Add type guard for Yup ValidationError before accessing inner property (#1000)

* Initial plan

* Add type checking for Yup ValidationError before accessing inner property

Co-authored-by: levalleux-ludo <7184124+levalleux-ludo@users.noreply.github.com>

* Fix spacing in function call

Co-authored-by: levalleux-ludo <7184124+levalleux-ludo@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: levalleux-ludo <7184124+levalleux-ludo@users.noreply.github.com>

* chore: add defensive error type checking for yup validationError handling (#1001)

* Initial plan

* Add error type checking for Yup ValidationError handling

Co-authored-by: levalleux-ludo <7184124+levalleux-ludo@users.noreply.github.com>

* Refactor: extract error validation logic into helper function

Co-authored-by: levalleux-ludo <7184124+levalleux-ludo@users.noreply.github.com>

* Improve error property extraction to be more explicit

Co-authored-by: levalleux-ludo <7184124+levalleux-ludo@users.noreply.github.com>

* Improve type safety in error extraction helper

Co-authored-by: levalleux-ludo <7184124+levalleux-ludo@users.noreply.github.com>

* Add comprehensive JSDoc documentation for error extraction helper

Co-authored-by: levalleux-ludo <7184124+levalleux-ludo@users.noreply.github.com>

* fix lint

* fix test

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: levalleux-ludo <7184124+levalleux-ludo@users.noreply.github.com>
Co-authored-by: Ludovic Levalleux <levalleux_ludo@hotmail.com>

* copilot pr remarks

* some additional fixes

* fix render contractual agreement for bundle offer

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com>
Co-authored-by: levalleux-ludo <7184124+levalleux-ludo@users.noreply.github.com>
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