valibot
valibot copied to clipboard
[`base64`] optional padding
Problem
Existing base64 implementation requires padding to be aligned with the RFC, but atob function in browsers can parse Base64 string w/o padding as it's optional. It makes it impossible to use transformations with atob() after v.base64() because the Base64 validation will fail.
Example
This string is a JWT payload and can be decoded well by atob(), but the base64 validator will mark it as a wrong Base64 string because there are 90 symbols w/o padding (==).
eyJzdWIiOiIxNDg3IiwibmFtZSI6Ildvb2YgQmFya292Iiwicm9sZSI6ImFkbWluIiwiaWF0IjoxNTE2MjM5MDIyfQ
Workaround
At the moment I'm using simple transformation before v.base64(), but it would be nice to support it natively (maybe additional params, separate validator, etc.)
export function addBase64Padding(value: string) : string {
const lengthRemainder = value.length % 4;
const paddingNeeded = 4 - lengthRemainder;
const paddingString = '='.repeat(paddingNeeded);
const paddedValue = paddingNeeded === 4 ? value : `${value}${paddingString}`;
return paddedValue;
}
Hey! 👋 Thank you for reaching out. Yes, making the padding optional seems more practical. My only concern is that there might be other functions that require it. Changing this could break them. 😐 In the meantime, since base64 is purely regex-based, you could copy and modify our regex and use it with our regex action as another workaround.
Hi, @Perdolique. I'm Dosu, and I'm helping the Valibot team manage their backlog and am marking this issue as stale.
Issue Summary
- You requested optional padding support in the
base64validator to handle valid unpadded Base64 strings like JWT payloads. - The maintainer acknowledged the usefulness but expressed concerns about breaking other functions that rely on padding.
- A temporary workaround involving modifying the regex-based validator was suggested.
- You reacted positively to the workaround, but no further updates or resolution have been provided.
Next Steps
- Please let me know if this issue is still relevant with the latest version of Valibot by commenting here to keep the discussion open.
- Otherwise, this issue will be automatically closed in 30 days.
Thanks for your understanding and contribution!
I believe it is. Having an option like ignorePadding: true doesn't seem like a breaking change.
(dead GitHub 😔)
@fabian-hiller The user Perdolique has confirmed the issue is still relevant and suggests that adding an ignorePadding: true option for the base64 validator wouldn't be a breaking change. Could you please assist with this?