Skip to content

Commit

Permalink
Make "inRange" and "hasLength" validator parameters optional
Browse files Browse the repository at this point in the history
  • Loading branch information
tobias-walle committed Jul 30, 2017
1 parent 2d7dda8 commit 7292a29
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/configuration/default-value-validator-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ export const defaultValueValidatorConfig: ValueValidatorConfig<{}> = {
notValid: 'Enter a valid number'
},
hasLength: {
shorter: ({params: {min}}) => `Use at least ${min} ${pluralize(min, 'character')}`,
longer: ({params: {max}}) => `Use a maximum of ${max} ${pluralize(max, 'character')}`
shorter: ({params: {min}}) => min != null ? `Use at least ${min} ${pluralize(min, 'character')}` : '',
longer: ({params: {max}}) => max != null ? `Use a maximum of ${max} ${pluralize(max, 'character')}` : '',
},
matchRegExp: {
notValid: ({target: {name}}) => `Enter a valid ${name}`
Expand Down
33 changes: 31 additions & 2 deletions src/configuration/resolvers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe('Resolvers', () => {
underMin: 'underMin',
overMax: 'overMax',
};
const inRange = VALIDATION_RESOLVERS.inRange({errorMessages})({
let inRange = VALIDATION_RESOLVERS.inRange({errorMessages})({
min: -10,
max: 10,
});
Expand All @@ -64,6 +64,21 @@ describe('Resolvers', () => {
expect(inRange(10.000012)).toBe(errorMessages.overMax);
expect(inRange(Infinity)).toBe(errorMessages.overMax);
});

it('should work with only min', () => {
inRange = VALIDATION_RESOLVERS.inRange({errorMessages})({
min: -10,
});
expect(inRange(-11)).toBe(errorMessages.underMin);
expect(inRange(1000000000)).toBeNull();
});
it('should work with only max', () => {
inRange = VALIDATION_RESOLVERS.inRange({errorMessages})({
max: -10,
});
expect(inRange(-11)).toBeNull();
expect(inRange(1000000000)).toBe(errorMessages.overMax);
});
});

describe('isEmail', () => {
Expand Down Expand Up @@ -124,7 +139,7 @@ describe('Resolvers', () => {
shorter: 'shorter',
longer: 'longer',
};
const hasLength = VALIDATION_RESOLVERS.hasLength({errorMessages})({
let hasLength = VALIDATION_RESOLVERS.hasLength({errorMessages})({
min: 5,
max: 10
});
Expand All @@ -136,6 +151,20 @@ describe('Resolvers', () => {
expect(hasLength('1234')).toBe('shorter');
expect(hasLength('12345689101')).toBe('longer');
});
it('should work with only min', () => {
hasLength = VALIDATION_RESOLVERS.hasLength({errorMessages})({
min: 5
});
expect(hasLength('123')).toBe('shorter');
expect(hasLength('1234565')).toBeNull();
});
it('should work with only max', () => {
hasLength = VALIDATION_RESOLVERS.hasLength({errorMessages})({
max: 5
});
expect(hasLength('123')).toBeNull();
expect(hasLength('1234565')).toBe('longer');
});
});

describe('matchRegExp', () => {
Expand Down
8 changes: 4 additions & 4 deletions src/configuration/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,19 @@ export const VALIDATION_RESOLVERS: ValidationResolvers<any> = {
},
inRange: ({errorMessages}) => ({min, max}) => (value) => {
if (value == null) return null;
if (value < min) {
if (min != null && value < min) {
return errorMessages.underMin;
} else if (value > max) {
} else if (max != null && value > max) {
return errorMessages.overMax;
}
return null;
},
hasLength: ({errorMessages}) => ({min, max}) => (value) => {
if (value == null) return null;
const length = value.length;
if (length < min) {
if (min != null && length < min) {
return errorMessages.shorter;
} else if (length > max) {
} else if (max != null && length > max) {
return errorMessages.longer;
}
return null;
Expand Down
8 changes: 4 additions & 4 deletions src/configuration/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@ export interface Validators {
inRange: {
inputType: number
params: {
min: number,
max: number,
min?: number,
max?: number,
},
cases: 'underMin' | 'overMax'
};
hasLength: {
inputType: string
params: {
min: number,
max: number,
min?: number,
max?: number,
},
cases: 'shorter' | 'longer'
};
Expand Down

0 comments on commit 7292a29

Please sign in to comment.