Skip to content

Commit 2b7ede8

Browse files
committed
Allowed email to be empty.
Added typings on returned values by some functions.
1 parent c7c1e5a commit 2b7ede8

7 files changed

Lines changed: 27 additions & 17 deletions

File tree

lib/src/rules/email.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,22 @@ const EMAIL_PATTERN = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".
55

66
export const email: FieldValidationFunction = (value: string) => {
77
const validationResult = new FieldValidationResult();
8-
const isValid = EMAIL_PATTERN.test(value);
8+
const isValid = isValidEmail(value);
99

1010
validationResult.succeeded = isValid;
1111
validationResult.type = 'EMAIL';
1212
validationResult.errorMessage = isValid ? '' : 'Please enter a valid email address.';
1313
return validationResult;
1414
};
15+
16+
function isValidEmail(value): boolean {
17+
return isEmptyValue(value) ?
18+
true :
19+
EMAIL_PATTERN.test(value);
20+
}
21+
22+
function isEmptyValue(value): boolean {
23+
return value === null ||
24+
value === undefined ||
25+
value === '';
26+
}

lib/src/rules/maxLength.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export function maxLength(value: string, vm, customParams: LengthParams = DEFAUL
1919
return validationResult;
2020
}
2121

22-
function isStringLengthValid(value: string, length: number) {
22+
function isStringLengthValid(value: string, length: number): boolean {
2323
return isNaN(length) ?
2424
false :
2525
value.length <= length;

lib/src/rules/minLength.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ export const minLength: FieldValidationFunction = (value: string, vm, customPara
1919
return validationResult;
2020
}
2121

22-
function isStringLengthValid(value: string, length: number) {
22+
function isStringLengthValid(value: string, length: number): boolean {
2323
return value.length >= length;
2424
}

lib/src/rules/pattern.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function parsePattern({ pattern }: PatternParams): RegExp {
2626
return getRegExp(pattern);
2727
}
2828

29-
function getRegExp(pattern) {
29+
function getRegExp(pattern): RegExp {
3030
return pattern instanceof RegExp ?
3131
pattern :
3232
new RegExp(pattern);

lib/src/rules/required.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,7 @@ export const required: FieldValidationFunction = (value, vm, customParams: Requi
1717
function isValidField(value, trim: boolean): boolean {
1818
return typeof value === 'string' ?
1919
isStringValid(value, trim) :
20-
isNotFalsy(value);
21-
}
22-
23-
function isNotFalsy(value) {
24-
return value !== null &&
25-
value !== undefined &&
26-
value !== false;
20+
value === true;
2721
}
2822

2923
function isStringValid(value: string, trim: boolean): boolean {

lib/src/rules/spec/email.spec.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { FieldValidationResult } from '../../entities';
33

44
describe('[email] validation rule tests =>', () => {
55
describe('When validating a non string value', () => {
6-
it('should return false if value is null', () => {
6+
it('should return true if value is null', () => {
77
// Arrange
88
const value = null;
99
const vm = undefined;
@@ -13,12 +13,12 @@ describe('[email] validation rule tests =>', () => {
1313
const validationResult = email(value, vm, customParams) as FieldValidationResult;
1414

1515
// Assert
16-
expect(validationResult.succeeded).to.be.false;
16+
expect(validationResult.succeeded).to.be.true;
1717
expect(validationResult.type).to.be.equals('EMAIL');
18-
expect(validationResult.errorMessage).to.be.equals('Please enter a valid email address.');
18+
expect(validationResult.errorMessage).to.be.empty;
1919
});
2020

21-
it('should return false if value is undefined', () => {
21+
it('should return true if value is undefined', () => {
2222
// Arrange
2323
const value = undefined;
2424
const vm = undefined;
@@ -28,9 +28,9 @@ describe('[email] validation rule tests =>', () => {
2828
const validationResult = email(value, vm, customParams) as FieldValidationResult;
2929

3030
// Assert
31-
expect(validationResult.succeeded).to.be.false;
31+
expect(validationResult.succeeded).to.be.true;
3232
expect(validationResult.type).to.be.equals('EMAIL');
33-
expect(validationResult.errorMessage).to.be.equals('Please enter a valid email address.');
33+
expect(validationResult.errorMessage).to.be.empty;
3434
});
3535

3636
it('should return false if value is number', () => {

lib/src/rules/spec/maxLength.spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ describe('[maxLength] validation rule tests =>', () => {
1515

1616
// Assert
1717
expect(validationResult.succeeded).to.be.true;
18+
expect(validationResult.type).to.be.equals('MAX_LENGTH');
19+
expect(validationResult.errorMessage).to.be.empty;
1820
});
1921

2022
it('should return true if value is null', () => {
@@ -28,6 +30,8 @@ describe('[maxLength] validation rule tests =>', () => {
2830

2931
// Assert
3032
expect(validationResult.succeeded).to.be.true;
33+
expect(validationResult.type).to.be.equals('MAX_LENGTH');
34+
expect(validationResult.errorMessage).to.be.empty;
3135
});
3236
});
3337

0 commit comments

Comments
 (0)