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

Feature/validate input text #9

Merged
merged 3 commits into from Nov 22, 2019
Merged

Feature/validate input text #9

merged 3 commits into from Nov 22, 2019

Conversation

RossUK88
Copy link

Hey,
I came across a usecase where I was looping over some user content, and when they hadn't entered anything into a field I was getting an error (null string), while I had a try catch over this I wasn't get any feedback saying that it was invalid, so It was never caught.

While I do need to validate or not pass to the translate api myself I thought it'd be useful to have a Not Valid Arguement exception thrown for this.

This shouldn't break any code anyone has as it would be erroring out anyway.
Added in tests for the funationality too.

Extracted it to a method so if there are anymore things that should be validated agaisnt, they can be put in there with their exceptions.

Ross Steele added 2 commits November 22, 2019 11:14
Create validateInput method that currently checks any inputs for null and throw an invalid argument exception.
Check all methods that can error out by putting in null strings
Create new tests for validating against null string
@@ -149,4 +159,17 @@ public function sanitizeLanguageCode(string $languageCode)
get the list of valid and supported language codes by running GoogleTranslate::languages()"
);
}

protected function validateInput($input): void
Copy link
Member

Choose a reason for hiding this comment

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

we should use the nullable type to typehint $input here ?string

Copy link
Author

Choose a reason for hiding this comment

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

As you can pass an array through to it and validate it that way (rather than using 2 different methods) it'll break when using anything with batch

Copy link
Member

Choose a reason for hiding this comment

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

makes sense

protected function validateInput($input): void
{
if(is_array($input) && in_array(null, $input)) {
throw new \InvalidArgumentException('Input string cannot be null');
Copy link
Member

Choose a reason for hiding this comment

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

can you import it at the top and remove \ on both lines

Copy link
Author

Choose a reason for hiding this comment

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

Pushed amendments

@introwit introwit merged commit cdaf2a4 into JoggApp:master Nov 22, 2019
@introwit
Copy link
Member

@RossUK88 although this is not something the package should be concerned of I merged it anyways because you had put in so much efforts and it was a non-breaking one. Tagged a new patch release. Please in the future, open an issue to discuss if something is out of the scope of this package 🙂 Thank you for the efforts though ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants