Skip to content
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

Missing parse function #65

Open
1chb opened this issue Feb 12, 2025 · 1 comment
Open

Missing parse function #65

1chb opened this issue Feb 12, 2025 · 1 comment

Comments

@1chb
Copy link

1chb commented Feb 12, 2025

Shouldn't there be a parse function? Given a base64 encoded string from the untyped world out there, do I really need to both check if its encoding is correct and then assert that it is correct? I looked for a function of type a -> Either e (Base64 k a) and a -> Maybe (Base64 k a) but couldn't find anything.

@emilypi
Copy link
Owner

emilypi commented Feb 12, 2025

The problem with base64 is that you can have a string that consists only of valid characters from a particular base64 alphabet, and have it still be structurally incorrect base64, because the bytes are messed up in one way or another. The only real way to know that a string is base64 is to actually decode it and check to see if the value is correct, and produce errors along the way. decodeBase64Untyped and the like are roughly that process, so to parse is to decode and validate whether you have an error. I provide a quick check in the form of isBase64, which produces a Bool, which isn't quite what you want, but the decode step is exactly the shape of what you want:

decodeBase64Untyped :: ByteString -> Either Text ByteString
.

Otherwise, if you want something that asserts this yourself, i mean, it doesn't quite meet my Fairbarn threshold for writing a new function, since it's special in the sense that it's basically \b -> if isBase64 b then assertBase64 b else "your error" or simply \b -> assertBase64 b <$ decodeBase64Untyped b.

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

No branches or pull requests

2 participants