Skip to content
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

Adds a range validator #333

Merged
merged 2 commits into from
Jan 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions packages/metal-state/src/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ const Config = {
*/
func: setPrimitiveValidators('func'),

/**
* Creates `State` configuration object with a `rangeOf` validator.
* @param {!Number} min The minimum value allowed.
* @param {!Number} max The maximum value allowed.
* @return {ConfigWithValidator} `State` configuration object.
*/
inRange(min, max) {
return this.validator(validators.inRange(min, max));
},

/**
* Function that creates `State` object with an `instanceOf` validator.
* @return {ConfigWithValidator} `State` configuration object.
Expand Down
24 changes: 24 additions & 0 deletions packages/metal-state/src/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,30 @@ const validators = {
});
},

/**
* Creates a validator that checks for a value within a range.
* @param {!Number} min The minimum value allowed.
* @param {!Number} max The maximum value allowed.
* @return {!function()}
*/
inRange: function(min, max) {
const minResult = validators.number(min);
const maxResult = validators.number(max);
if (isInvalid(minResult)) {
return minResult;
}
if (isInvalid(maxResult)) {
return maxResult;
}
return maybe(value => {
const valueResult = validators.number(value);
if (isInvalid(valueResult)) {
return valueResult;
}
return value >= min && value <= max;
});
},

/**
* Creates a validator that checks if a value is an instance of a given class.
* @param {!function()} expectedClass Class to check value against.
Expand Down
8 changes: 8 additions & 0 deletions packages/metal-state/test/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,4 +334,12 @@ describe('Config', function() {
assert.isUndefined(config.config.required);
assert.isUndefined(config2.config.required);
});

it('should validate ranges', function() {
const config = Config.inRange(0, 100);
assert.ok(core.isObject(config));
assert.ok(config.config.validator(10));
assert.isFalse(config.config.validator(101));
assert.ok(config.config.validator('3') instanceof Error);
});
});
18 changes: 18 additions & 0 deletions packages/metal-state/test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,4 +296,22 @@ describe('validators', function() {

assert.equal(resultError, ERROR_MESSAGE);
});

it('should validate a range', function() {
const range1 = validators.inRange(0, 100);
assert.isTrue(range1(1));
assert.isFalse(range1(-1));

const range2 = validators.inRange('foo', null);
assert.ok(range2 instanceof Error);

const range3 = validators.inRange(1, 'bar');
assert.ok(range3 instanceof Error);

const range4 = validators.inRange(1, 10);
assert.isFalse(range4(-1));
assert.isFalse(range4(11));
assert.ok(range4('foo') instanceof Error);
assert.ok(range4({}) instanceof Error);
});
});