-
Notifications
You must be signed in to change notification settings - Fork 0
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
Validator#regular expression #16
Conversation
Codecov Report
@@ Coverage Diff @@
## devel #16 +/- ##
=========================================
+ Coverage 93.88% 94.09% +0.2%
=========================================
Files 5 5
Lines 507 525 +18
=========================================
+ Hits 476 494 +18
Misses 31 31 |
README.md
Outdated
@@ -37,7 +38,8 @@ const rules = { | |||
username: 'required|unique:users,username', | |||
email: 'required|email', | |||
age: 'required|numeric|min:18', | |||
gender: 'in:male,female' | |||
gender: 'in:male,female', | |||
phoneNumber: 'regex:[0-9]{3} ?-?[0-9]{3} ?-?[0-9]{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.
Test this with ,
. It's used to separate parameters. If you get more that 1 parameter then you need to join parameteres with ,
.
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.
But there is only 1 parameter
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.
But you can use ,
in regex for example to match date 10th January, 2015
. If you use ,
in regex then you might end up with 2 parameters
@@ -95,3 +95,9 @@ export function validateAlpha(attribute, value) { | |||
export function validateAlphaNum(attribute, value) { | |||
return (typeof value === 'string' || typeof value === 'number') && is.alphaNumeric(value) | |||
} | |||
|
|||
export function validateRegex(attribute, value, parameters) { |
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.
You can follow logic implemented here https://github.com/laravel/framework/blob/5.4/src/Illuminate/Validation/Concerns/ValidatesAttributes.php#L1042
src/messages.js
Outdated
@@ -20,5 +20,6 @@ export default { | |||
integer: 'The :attribute must be an integer.', | |||
accepted: 'The :attribute must be accepted.', | |||
alpha: 'The :attribute may only contain letters.', | |||
alpha_num: 'The :attribute may only contain letters and numbers.' | |||
alpha_num: 'The :attribute may only contain letters and numbers.', | |||
regex: 'The :attribute must match to the format.' |
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.
You can copy messages from here: https://github.com/laravel/laravel/blob/master/resources/lang/en/validation.php
/* | ||
* Regular Expression | ||
* ----------------------------------------------------- */ | ||
describe('#regular_expression', () => { |
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.
Tests looks good. For future referece, you can check https://github.com/laravel/framework/blob/5.4/tests/Validation/ValidationValidatorTest.php#L2264
src/validators.js
Outdated
@@ -95,3 +95,9 @@ export function validateAlpha(attribute, value) { | |||
export function validateAlphaNum(attribute, value) { | |||
return (typeof value === 'string' || typeof value === 'number') && is.alphaNumeric(value) | |||
} | |||
|
|||
export function validateRegex(attribute, value, parameters) { | |||
const regex = parameters[0] ? new RegExp(`${parameters[0]}`) : undefined |
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 rule should require just 1 parameter:
this.requireParameterCount(1, parameters, 'regex')
…ck if value is string or number
Codecov Report
@@ Coverage Diff @@
## devel #16 +/- ##
==========================================
+ Coverage 93.88% 94.23% +0.35%
==========================================
Files 5 5
Lines 507 538 +31
==========================================
+ Hits 476 507 +31
Misses 31 31 |
src/validators.js
Outdated
if (is.not.string(value) && is.not.number(value)) { | ||
return false | ||
} | ||
const regex = parameters[0] ? new RegExp(`${parameters[0]}`) : undefined |
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.requireParameterCount
will throw error if parameters[0] doesn't exit so this check is useless.
const regex = new RegExp(`${parameters[0]}`)
should be enough
Codecov Report
@@ Coverage Diff @@
## devel #16 +/- ##
==========================================
+ Coverage 93.88% 94.23% +0.35%
==========================================
Files 5 5
Lines 507 538 +31
==========================================
+ Hits 476 507 +31
Misses 31 31 |
1 similar comment
Codecov Report
@@ Coverage Diff @@
## devel #16 +/- ##
==========================================
+ Coverage 93.88% 94.23% +0.35%
==========================================
Files 5 5
Lines 507 538 +31
==========================================
+ Hits 476 507 +31
Misses 31 31 |
No description provided.