-
Notifications
You must be signed in to change notification settings - Fork 10
Issue#50 validation rules #52
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
Conversation
Exported validationFunctions in index.ts
Renamed constants in rules. Removed customParams in email. Updated samples.
lib/lcformvalidation.d.ts
Outdated
| function pattern(value: any, vm: any, customParams: PatternParams): FieldValidationResult; | ||
| } | ||
|
|
||
|
|
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.
Remove extra space
lib/src/rules/index.ts
Outdated
| minLength, | ||
| maxLength, | ||
| email, | ||
| pattern |
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.
Add trailing comma
| } | ||
|
|
||
| export function parseLengthParams(customParams: LengthParams, errorMessage: string) { | ||
| const length = customParams.length === null ? NaN : Number(customParams.length); |
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 happends if customParams.length is 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.
Why do you have to parse customParams.length to number if LengthParams.lenght field is a number?
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 is declared as a number in TypeScript but when it is transpiled there is no type checking in runtime.
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.
If it is undefined it is casted with Number() and getting NaN. I asked for null and did not included undefined because when you do a Number(null) you get 0 instead of NaN and if you do Number(undefined) you get NaN.
lib/src/rules/length.ts
Outdated
| return length; | ||
| } | ||
|
|
||
| interface LengthValidatorFunction { |
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.
Where are you using this interface?
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'll remove it
lib/src/rules/length.ts
Outdated
| } | ||
|
|
||
| // Don't validate non string values | ||
| return true; |
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.
Why don't use ternary?
lib/src/rules/maxLength.ts
Outdated
| } | ||
|
|
||
| function isStringLengthValid(value: string, length: number) { | ||
| return isNaN(length) ? false : value.length <= length; |
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 necessary check that length isNaN?
What about check value is != null or 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.
To improve readability is better introduce a carriage return after ? and :
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 necessary check that length isNaN?
Yes. Length must be a number. - What about check value is != null or undefined?
isNaNdoes an implicitNumber(<value>). - To improve readability is better introduce a carriage return after
?and:.
Do you mean insert (CR)/LF afterreturnstatement like this?:
function isStringLengthValid(value: string, length: number) {
return isNaN(length) ? false : value.length <= length;
}That has no sense to me.
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.
No I said something like:
function isStringLengthValid(value: string, length: number) {
return isNaN(length) ?
false :
value.length <= length;
}
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.
Btw to improve readability could be write this code like:
function isStringLengthValid(value: string, length: number) {
return !isNaN(length) &&
value.length <= length;
}
lib/src/rules/maxLength.ts
Outdated
| const validationResult = new FieldValidationResult(); | ||
|
|
||
| validationResult.succeeded = isValid; | ||
| validationResult.key = 'MAX_LENGTH'; |
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.
Typo: validationResult.type = 'MAX_LENGTH';
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.
Yes, I'll correct it and its unit tests.
lib/src/rules/minLength.ts
Outdated
| const validationResult = new FieldValidationResult(); | ||
| validationResult.errorMessage = isValid ? '' : `The value provided must have at least ${length} characters.`; | ||
| validationResult.succeeded = isValid; | ||
| validationResult.key = 'MIN_LENGTH'; |
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.
Typo: validationResult.type = 'MIN_LENGTH';
| } | ||
|
|
||
| function isStringLengthValid(value: string, length: number) { | ||
| return value.length >= length; |
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 have to check is value != null or undefined before access length property
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 method is called before isLengthValid that already checks if value is a string.
| pattern: string | RegExp; | ||
| } | ||
|
|
||
| const BAD_PARAMETER = 'FieldValidationError: pattern option for pattern validation is mandatory. Example: { pattern: /\d+/ }.'; |
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.
Add carriage return afer this const like other rules
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.
Ok! 👍
lib/src/rules/pattern.ts
Outdated
| } | ||
|
|
||
| function parsePattern({ pattern }: PatternParams): RegExp { | ||
| if (pattern === undefined || pattern === 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.
Is this condition equals to:
if (!pattern) {
....
}
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's not true for "", false or 0 values. I tried to avoid RegExp like /null/ I'll change that to:
if (typeof pattern === 'boolean' || pattern === null) {
throw new Error(BAD_PARAMETER);
}
return getRegExp(pattern);to avoid /true/ and /false/ too (with a comment).
lib/src/rules/pattern.ts
Outdated
|
|
||
| function getRegExp(pattern) { | ||
| if (typeof pattern === 'string') { | ||
| return new RegExp(`^${pattern}$`); |
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.
Why do you have to force to ^...$? User could pass a pattern like:
{
pattern: 'abc$'
}
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 add an flag in customParams to enable this feature or disable it. We need discussion about this behavior.
lib/src/rules/pattern.ts
Outdated
| if (pattern instanceof RegExp) { | ||
| return pattern; | ||
| } | ||
| return new RegExp(pattern); |
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.
Which is this case?
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.
User is allowed to enter a RegExp or string to use the pattern validator.
lib/src/rules/required.ts
Outdated
| const validationResult = new FieldValidationResult(); | ||
| const isValid = isValidField(value, Boolean(customParams.trim)); | ||
| if (!isValid) { | ||
| validationResult.errorMessage = 'Please fill in this mandatory field.'; |
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.
Why don't use ternary as other rules?
validationResult.errorMessage = isValid? '' : 'Please fill in this mandatory field.';
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'll change it.
lib/src/rules/required.ts
Outdated
|
|
||
| function isValidField(value, trim: boolean): boolean { | ||
| if (typeof value === 'string') { | ||
| return isStringValid(value, trim); |
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.
Please avoid multiples returns. You can write this code like:
function isValidField(value, trim: boolean): boolean {
return isInitialized(value) &&
isString(value) &&
isStringValid(value, trim);
}
lib/src/rules/length.ts
Outdated
| length: number, | ||
| validatorFn: (value: string, length: number) => boolean | ||
| ): boolean { | ||
| return typeof value === 'string' ? validatorFn(value, length) : true; |
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.
return typeof value === 'string' ?
validatorFn(value, length) :
true;
lib/src/rules/spec/email.spec.ts
Outdated
|
|
||
| it('should return false if value is undefined', () => { | ||
| // Arrange | ||
| const value = 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.
Typo: const value = undefined?
Added typings on returned values by some functions.
There was implemented next rules:
truegiventruevalue (for checkboxes) and non-empty string (trimed by default, can be changed with{ customParams: { trim: false } }. Returnsfalsefor falsy values.trueif given value is string and its length is lesser or equals that givenlengthoption in{ customParams: { length: <number> } }.nullandundefinedreturntrue(intended behaviour to treat them as uninitialized values). Returns false if mentioned conditions are not met.trueif given value is string and its length is greater or equals that givenlengthoption in{ customParams: { length: <number> } }.nullandundefinedreturntrue(intended behaviour to treat them as uninitialized values). Returns false if mentioned conditions are not met.trueif value is astringand matches the RegExp. Otherwise, it returnsfalse.trueif given value matches with the described pattern in{ customParams: { pattern: <RegExp | string> } }. If given pattern is astringit will be treated as a full RegExp (NEED REVISION HERE).