-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Description
Context
- node version: 6.9.2
- joi version: 10.4.1
- environment node
- used with standalone:
- any other relevant information:
What are you trying to achieve or the steps to reproduce ?
Determine if a URL parameter contains valid base64 encoded content.
const schema = Joi.string().base64()Example values:
MTYyMDcxMDozOjE
The problem is that the validation fails on a base64 string that is valid but does not have padding; the issue was dismissed in #1081
I don't agree with that interpretation; it's not invalid, it just doesn't have optional padding.
https://en.wikipedia.org/wiki/Base64#Output_Padding
In theory, the padding character is not needed for decoding, since the number of missing bytes can be calculated from the number of Base64 digits. In some implementations, the padding character is mandatory, while for others it is not used.
The input is not normalized and comes from a variety of sources that I do not have direct control over and I cannot force changes, nor can I just arbitrarily ignore unpadded content.
Attempting to pad the content prior to validation is hacky.
// Joi requires padding.
const paddingLength = working.length % 4;
const workingPadded = paddingLength ? working + '='.repeat(4 - paddingLength) : working;
A more graceful solution would be to make padding optional.
options: - an optional object with the following optional keys:paddingRequired: iftrue, requires that the string include=padding. Defaults totrue.
Which result you had ?
message: '"value" must be a valid base64 string',
What did you expect ?
No errors.