-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Added Exact Value/s Condition for String Length & Tests required with README.md Update #2019
Conversation
Codecov ReportPatch coverage:
Additional details and impacted files@@ Coverage Diff @@
## master #2019 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 104 104
Lines 2308 2311 +3
Branches 578 579 +1
=========================================
+ Hits 2308 2311 +3
☔ View full report in Codecov by Sentry. |
Hello, I have added the required feature. If the string length falls in the range and if exact number/array/object is mentioned, then exact conditions are checked. If not provided, then true is returned is len>=min & len<=max and false is returned is not in range [min, max]. All the checks have also been passed :) |
src/lib/isLength.js
Outdated
} else { // backwards compatibility: isLength(str, min [, max]) | ||
min = arguments[1] || 0; | ||
max = arguments[2]; | ||
exact = arguments[3]; |
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.
I don't think we need backwards compatibility for a new feature
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.
That also removes the need for some of the tests btw
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.
Ohh I thought if at all a User wants to check if it's exact to a bunch of values rather than a single value, so I configured the exact value to be an array or an object or a number. Should I remove the backwards compatibility feature ??
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.
It's just this line that you can remove (and the tests associated to this line). This else statement is only here for the people that use the old syntax for isLength. And since this is a new feature there is no need to implement it in both the old and new syntax.
That you configured the exact value to be an array or object or number is fine.
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.
Okkay!! I made the required changes & also updated the Tests for the same... Hence, now this new Feature exact is implemented only in the new Syntax of isLength.
src/lib/isLength.js
Outdated
@@ -5,15 +5,42 @@ export default function isLength(str, options) { | |||
assertString(str); | |||
let min; | |||
let max; | |||
let exact; | |||
let result; | |||
let isPerfect = false; |
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.
Maybe refactor this to isValid
?
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.
Yess!! Renamed isPerfect
to isValid
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.
Thanks for the updates! I have some additional comments
And I'll tag @rubiin here as well for the more official review and approval for having exact
be either a number, a array or an object of numbers since I'm not a maintainer of this library
README.md
Outdated
@@ -140,7 +140,7 @@ Validator | Description | |||
**isJSON(str [, options])** | check if the string is valid JSON (note: uses JSON.parse).<br/><br/>`options` is an object which defaults to `{ allow_primitives: false }`. If `allow_primitives` is true, the primitives 'true', 'false' and 'null' are accepted as valid JSON values. | |||
**isJWT(str)** | check if the string is valid JWT token. | |||
**isLatLong(str [, options])** | check if the string is a valid latitude-longitude coordinate in the format `lat,long` or `lat, long`.<br/><br/>`options` is an object that defaults to `{ checkDMS: false }`. Pass `checkDMS` as `true` to validate DMS(degrees, minutes, and seconds) latitude-longitude format. | |||
**isLength(str [, options])** | check if the string's length falls in a range.<br/><br/>`options` is an object which defaults to `{min:0, max: undefined}`. Note: this function takes into account surrogate pairs. | |||
**isLength(str [, options])** | check if the string's length falls in a range and equal to exactValue if provided.<br/><br/>`options` is an object which defaults to `{min:0, max: undefined, exact: undefined}`. Note: this function takes into account surrogate pairs. |
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.
Maybe add here that exact
can be a single number, an array or an object of several numbers?
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.
Done!! Updated the README.md
.
test/validators.js
Outdated
@@ -4629,6 +4629,12 @@ describe('Validators', () => { | |||
valid: [''], | |||
invalid: ['a', 'ab'], | |||
}); | |||
test({ |
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.
I think we can remove this new test since it does not test the new functionality
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.
Yess!! Removed that test.
test/validators.js
Outdated
valid: ['helloguy', 'shopping', 'validator', 'length'], | ||
invalid: ['abcde', 'abcdefg', 'abcdefghij'], | ||
}); | ||
test({ |
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 can remove this test since it tests the same as the test of { exact: 2 }
.
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.
Or adjust this test to test the combination of a min
and exact
values.
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.
Actually, the later one with exact: { first: 6, second: 8, third: 9 }
checks for the condition involving the exact
being an object. Hence, I have adjusted the previous one with {exact: 2}
to test the combination of a min
and exact
values.
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.
I was wrong about this test, the difference is that this is using a string of '9' instead of the number 9. That makes this a different test to { exact: 2 }
. So the combination of min
and exact
should be an additional test
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.
So I have included the combination of a min
and exact
values in a test with args: [{ min: 1, exact: 2 }]
.
test/validators.js
Outdated
}); | ||
test({ | ||
validator: 'isLength', | ||
args: [{ min: 5, max: 10, exact: { first: 6, second: 8, third: 9 } }], |
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.
What would happen if one of the exact
numbers is lower than min
or higher than max
? Should we add tests for that behaviour as well?
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.
Since the exact
condition only runs when the Length of the String falls inside [min, max]
range, if at all exact
integer or one of the exact
numbers is out of range [min, max]
, the equality check for that case will return false
and isValid
won't be set to true
.
Also, I have included the test condition for that behavior in the already present test with args: [{ min: 5, max: 10, exact: [2, 6, 8, 9] }]
.
src/lib/isLength.js
Outdated
isValid = true; | ||
} | ||
} | ||
} else { |
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.
This else statement is not needed since you already define isValid
as false at the beginning.
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.
Yess!! Removed the else statement.
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.
LGTM, just a few comments :).
I was just thinking: A future improvement could be to improve the options API to have consistent fields. This is because, with Something like: {
range: {
min: 0,
max: undefined
}, // optional
discreteLengths: undefined // optional
} With the above proposal, it also becomes clear, that there is another permutation of the rules set:
So basically, we would have to discuss if this API should allow checking against discrete lengths w/o using the range rule/option. Currently, to check Just some food for thoughts 😄. |
Co-authored-by: Falk Schieber <12937991+pixelbucket-dev@users.noreply.github.com>
Yeahh that's a really good idea. Making it segregated into key-value pairs would be easier to use and understand. |
It would be a breaking change! So I'd suggest keep the existing API ( |
@rubiin any reason why you approved this while the GitHub Actions have not passed? |
@bevatsal1122 could you please fix the formatting issues that break the CI? View the “Files Changed” tab so see details about the linting issues. N.B. we have to discuss better linter integration. Errors like this should never arrive in the PR, but rather fixed at commit time or push time by running commit hooks. |
@bevatsal1122 could you please assign this issue to me |
closing in favor of #2474 |
feat(isLength): Added Conditions for checking if String's length is equal to exact if exact provided
If the string length falls in the range and if exact number/array/object is provided, then exact conditions are checked. If not provided, then true is returned is len>=min & len<=max and false is returned is not in the range [min, max]. README.md File is also updated for the same.
Checklist