Skip to content

Commit

Permalink
fix: renamed rangeLength to length
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff Handley committed Dec 19, 2017
1 parent 9313ea0 commit 38e7a57
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 40 deletions.
Expand Up @@ -2,22 +2,22 @@ import validate from './strickland';
import minLength from './minLength';
import maxLength from './maxLength';

export default function range(minLengthValue, maxLengthValue, props) {
export default function length(min, max, props) {
let validatorProps;

if (typeof minLengthValue === 'object') {
if (typeof min === 'object') {
validatorProps = {
...minLengthValue
...min
};
} else if (typeof maxLengthValue === 'object') {
} else if (typeof max === 'object') {
validatorProps = {
minLength: minLengthValue,
...maxLengthValue
minLength: min,
...max
};
} else {
validatorProps = {
minLength: minLengthValue,
maxLength: maxLengthValue,
minLength: min,
maxLength: max,
...props
};
}
Expand Down
2 changes: 1 addition & 1 deletion packages/strickland/src/strickland.js
Expand Up @@ -4,7 +4,7 @@ export {default as maxLength} from './maxLength';
export {default as min} from './min';
export {default as minLength} from './minLength';
export {default as range} from './range';
export {default as rangeLength} from './rangeLength';
export {default as length} from './length';
export {default as required} from './required';
export {default as composite} from './composite';

Expand Down
@@ -1,36 +1,36 @@
import expect from 'expect';
import deepFreeze from 'deep-freeze';
import rangeLength from '../src/rangeLength';
import length from '../src/length';

describe('rangeLength', () => {
describe('length', () => {
describe('throws', () => {
it('when props are not supplied', () => {
expect(() => rangeLength()).toThrow();
expect(() => length()).toThrow();
});

it('when the min length is a string', () => {
expect(() => rangeLength('123', 456)).toThrow();
expect(() => length('123', 456)).toThrow();
});

it('when the max length is a string', () => {
expect(() => rangeLength(123, '456')).toThrow();
expect(() => length(123, '456')).toThrow();
});

it('when props is an object without a min or a max', () => {
expect(() => rangeLength({})).toThrow();
expect(() => length({})).toThrow();
});

it('when props is an object without a min', () => {
expect(() => rangeLength({ maxLength: 456 })).toThrow();
expect(() => length({ maxLength: 456 })).toThrow();
});

it('when props is an object without a max', () => {
expect(() => rangeLength({ minLength: 123 })).toThrow();
expect(() => length({ minLength: 123 })).toThrow();
});
});

describe('with a single props argument', () => {
const validate = rangeLength({minLength: 3, maxLength: 5, message: 'Custom message'});
const validate = length({minLength: 3, maxLength: 5, message: 'Custom message'});
const result = validate('1234');

it('uses the min prop', () => {
Expand All @@ -47,7 +47,7 @@ describe('rangeLength', () => {
});

describe('with the first argument as a number and the second as an object', () => {
const validate = rangeLength(3, {maxLength: 5, message: 'Custom message'});
const validate = length(3, {maxLength: 5, message: 'Custom message'});
const result = validate('1234');

it('sets the min prop', () => {
Expand All @@ -64,7 +64,7 @@ describe('rangeLength', () => {
});

describe('with the first and second arguments as numbers and the third as an object', () => {
const validate = rangeLength(3, 5, {message: 'Custom message'});
const validate = length(3, 5, {message: 'Custom message'});
const result = validate('1234');

it('sets the min prop', () => {
Expand All @@ -81,14 +81,14 @@ describe('rangeLength', () => {
});

it('returns the value on the result', () => {
const validate = rangeLength(3, 5);
const validate = length(3, 5);
const result = validate('1234');

expect(result.value).toBe('1234');
});

describe('returns the parsedValue on the result', () => {
const validate = rangeLength(3, 5);
const validate = length(3, 5);

it('when the value is a string', () => {
const result = validate('1234');
Expand Down Expand Up @@ -127,7 +127,7 @@ describe('rangeLength', () => {
});

describe('returns the length on the result', () => {
const validate = rangeLength(3, 5);
const validate = length(3, 5);

it('when the value is a string', () => {
const result = validate('1234');
Expand Down Expand Up @@ -209,15 +209,15 @@ describe('rangeLength', () => {
});
});

validates('with numeric arguments', rangeLength(3, 5));
validates('with a props argument', rangeLength({minLength: 3, maxLength: 5}));
validates('with a numeric min and props', rangeLength(3, {maxLength: 5}));
validates('with numeric min and max plus props', rangeLength(3, 5, {other: 'other'}));
validates('with numeric arguments', length(3, 5));
validates('with a props argument', length({minLength: 3, maxLength: 5}));
validates('with a numeric min and props', length(3, {maxLength: 5}));
validates('with numeric min and max plus props', length(3, 5, {other: 'other'}));

describe('with props passed into validation', () => {
it('allows the minLength value to be specified at time of validation', () => {
const validatorProps = {minLength: 4, maxLength: 6};
const validate = rangeLength(validatorProps);
const validate = length(validatorProps);
const result = validate('123', {minLength: 2});

expect(result).toMatchObject({
Expand All @@ -228,7 +228,7 @@ describe('rangeLength', () => {

it('allows the maxLength value to be specified at time of validation', () => {
const validatorProps = {minLength: 4, maxLength: 6};
const validate = rangeLength(validatorProps);
const validate = length(validatorProps);
const result = validate('1234567', {maxLength: 8});

expect(result).toMatchObject({
Expand All @@ -243,21 +243,21 @@ describe('rangeLength', () => {
const props = {minLength: 3, maxLength: 5};
deepFreeze(props);

expect(() => rangeLength(props)('1234')).not.toThrow();
expect(() => length(props)('1234')).not.toThrow();
});

it('when a min length and props are used', () => {
const props = {maxLength: 5, message: 'Custom message'};
deepFreeze(props);

expect(() => rangeLength(3, props)('1234')).not.toThrow();
expect(() => length(3, props)('1234')).not.toThrow();
});

it('when min and max values and props are used', () => {
const props = {message: 'Custom message'};
deepFreeze(props);

expect(() => rangeLength(3, 5, props)('1234')).not.toThrow();
expect(() => length(3, 5, props)('1234')).not.toThrow();
});
});
});
13 changes: 5 additions & 8 deletions packages/strickland/test/strickland.spec.js
@@ -1,8 +1,5 @@
import expect from 'expect';
import validate, {isValid} from '../src/strickland';
import required from '../src/required';
import minLength from '../src/minLength';
import rangeLength from '../src/rangeLength';
import validate, {isValid, required, minLength, length} from '../src/strickland';

describe('validate', () => {
describe('throws', () => {
Expand Down Expand Up @@ -305,15 +302,15 @@ describe('validate', () => {
homeAddress: {
street: required(),
city: required(),
state: [required(), rangeLength(2, 2)]
state: [required(), length(2, 2)]
},
workAddress: {
street: {
number: required(),
name: required()
},
city: required(),
state: [required(), rangeLength(2, 2)]
state: [required(), length(2, 2)]
}
};

Expand Down Expand Up @@ -506,15 +503,15 @@ describe('validate', () => {
homeAddress: {
street: required(),
city: required(),
state: [required(), rangeLength(2, 2)]
state: [required(), length(2, 2)]
},
workAddress: {
street: {
number: required(),
name: required()
},
city: required(),
state: [required(), rangeLength(2, 2)]
state: [required(), length(2, 2)]
}
};

Expand Down

0 comments on commit 38e7a57

Please sign in to comment.