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

Fix #1156 - Adds support for optional base64 padding validation. #1174

Merged
merged 6 commits into from
Jun 2, 2017

Conversation

fluxsauce
Copy link
Contributor

As discussed in #1156.

Added a single boolean option padding; if undefined, defaults to true.

My original version was slightly too lenient and allowed too much padding at the end of the statement. I believe this version should take into account only proper amounts of padding.

@fluxsauce fluxsauce changed the title #1156 - Adds support for optional base64 padding validation. Fix #1156 - Adds support for optional base64 padding validation. May 2, 2017
API.md Outdated

Requires the string value to be a valid base64 string.
* padding - optional parameter defaulting to `true` which will require `=` padding if `true` or make padding optional (`false`).
Copy link
Contributor

Choose a reason for hiding this comment

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

I think that calling this paddingRequired would better represent the functionality of it, I could see a padding option being used for other things.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If it's better to stick with a single parameter rather than an object, sounds good.

API.md Outdated
@@ -1764,12 +1764,23 @@ Requires the string value to be a valid hexadecimal string.
const schema = Joi.string().hex();
```

#### `string.base64()`
#### `string.base64([padding])`
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure what the consensus is but I'd personally prefer to see this as an options object instead that way we can support other options in the future without making a breaking change.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can, but the only other options I can anticipate would be around decoding? Trying to be cognizant of over-engineering.

test/string.js Outdated

const rule = Joi.string().base64(true);
Helper.validate(rule, [
['YW55IGNhcm5hbCBwbGVhc3VyZS4=', true],
Copy link
Contributor

Choose a reason for hiding this comment

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

We should also include a test for 2 bytes of padding (== instead of =). ie. YW55IGNhcm5hbCBwbGVhc3VyZQ==

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍

test/string.js Outdated

const rule = Joi.string().base64(false);
Helper.validate(rule, [
['YW55IGNhcm5hbCBwbGVhc3VyZS4=', true],
Copy link
Contributor

Choose a reason for hiding this comment

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

We should also include a test for 2 bytes of padding (== instead of =). ie. YW55IGNhcm5hbCBwbGVhc3VyZQ==

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Already there :-)

lib/string.js Outdated
if (padding) {
Hoek.assert(typeof padding === 'boolean', 'padding requirement must be boolean');
}
const paddingRequired = (typeof padding === 'undefined') ? true : padding;
Copy link
Contributor

Choose a reason for hiding this comment

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

This won't catch when a null is passed. In general I would usually handle this with something like the following:

padding = padding === false ? padding : padding || true;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍 and will add tests.

test/string.js Outdated
@@ -3180,6 +3180,42 @@ describe('string', () => {
], done);
});

it('validates an base64 string with explicit padding required', (done) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we add some tests around passing in the padding parameter so that we are making sure that it requires a boolean and other inputs are rejected.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍

@DavidTPate DavidTPate added the feature New functionality or improvement label May 2, 2017
@fluxsauce
Copy link
Contributor Author

@DavidTPate Thanks again, I renamed the option but as I didn't get any feedback about additional options for base64 validation, I'm keeping it as boolean for now. I believe all other concerns were addressed.

@Marsup
Copy link
Collaborator

Marsup commented May 12, 2017

Also in favor of an object. I'm not dogmatic about the famous boolean trap but I'd say this specifically is a very good case and would contribute in a bad way to the readability of your schemas.

@DavidTPate
Copy link
Contributor

@fluxsauce Your changes & docs look good, I'd just really like to see the option passed as an object as opposed to a single parameter.

@fluxsauce
Copy link
Contributor Author

I appreciate the feedback and I'll make the changes, I just haven't had the time to sit down and do it properly. Will happen this week.

@fluxsauce
Copy link
Contributor Author

Sunday counts as this week, yes? :-)

Should be set, let me know if you have any feedback!

Copy link
Contributor

@DavidTPate DavidTPate left a comment

Choose a reason for hiding this comment

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

LGTM

@Marsup Marsup self-assigned this Jun 2, 2017
@Marsup Marsup added this to the 10.6.0 milestone Jun 2, 2017
@Marsup Marsup merged commit b9a6110 into hapijs:master Jun 2, 2017
@lock
Copy link

lock bot commented Jan 9, 2020

This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions.

@lock lock bot locked as resolved and limited conversation to collaborators Jan 9, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature New functionality or improvement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants