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
Conversation
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 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
makes sense
src/GoogleTranslate.php
Outdated
protected function validateInput($input): void | ||
{ | ||
if(is_array($input) && in_array(null, $input)) { | ||
throw new \InvalidArgumentException('Input string cannot be null'); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pushed amendments
@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 ❤️ |
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.