Skip to content

Add Optional Strict Input Checking to u8aToU8a#1994

Merged
ap211unitech merged 3 commits intomasterfrom
fix/u8aTou8a-edgecase
Jun 30, 2025
Merged

Add Optional Strict Input Checking to u8aToU8a#1994
ap211unitech merged 3 commits intomasterfrom
fix/u8aTou8a-edgecase

Conversation

@ap211unitech
Copy link
Copy Markdown
Member

@ap211unitech ap211unitech commented Jun 18, 2025

📝 Description

This PR introduces an opt-in strict parameter to the u8aToU8a function to prevent silently converting null or undefined inputs into empty Uint8Array instances.

💡 Motivation

While working with this function, we noticed that passing null or undefined was producing empty arrays instead of throwing errors, leading to hard-to-trace issues. This behavior caused 134 test failures. Instead of modifying every test and potentially introducing inconsistencies, we chose to add a strict checking mode.

🔧 Changes

  • Adds a strict boolean parameter (default: false) to u8aToU8a.
  • When strict is enabled, the function throws an error if the input is null or undefined.
  • Default behavior is preserved to maintain backward compatibility.

✅ Benefits

This approach allows developers to opt into stricter input validation when needed, reducing the chances of subtle bugs or random runtime issues in downstream applications.

// Example usage
u8aToU8a(null); // Returns new Uint8Array()
u8aToU8a(undefined); // Returns new Uint8Array()

u8aToU8a(null, true); // Throws: 'u8aToU8a: Expected non-null, non-undefined value'
u8aToU8a(undefined, true); // Throws: 'u8aToU8a: Expected non-null, non-undefined value'

@ap211unitech ap211unitech self-assigned this Jun 18, 2025
@ap211unitech ap211unitech changed the title Add error handling for null and undefined inputs in u8aToU8a function Add Optional Strict Input Checking to u8aToU8a Jun 24, 2025
TarikGul
TarikGul previously approved these changes Jun 24, 2025
Copy link
Copy Markdown
Contributor

@valentinfernandez1 valentinfernandez1 left a comment

Choose a reason for hiding this comment

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

Awesome find 🔥

Copy link
Copy Markdown
Member

@TarikGul TarikGul left a comment

Choose a reason for hiding this comment

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

🚀

@ap211unitech ap211unitech merged commit 538359f into master Jun 30, 2025
4 checks passed
@ap211unitech ap211unitech deleted the fix/u8aTou8a-edgecase branch June 30, 2025 10:19
@polkadot-js-bot
Copy link
Copy Markdown

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@polkadot-js polkadot-js locked as resolved and limited conversation to collaborators Jul 2, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants