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

feat: Add new rule sumArray #51

Merged
merged 1 commit into from
Sep 26, 2016
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
54 changes: 54 additions & 0 deletions src/rules/__tests__/sumArray_tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import test from 'ava';

import sumArray from '../sumArray';

test('rules.sumArray should return "sumArray.exact" for invalid exact', t => {
t.is(sumArray('field', [0, 1, 2], { exact: 4 }), 'sumArray.exact');
t.is(sumArray(
'field',
[{ value: 0 }, { value: 1 }, { value: 2 }],
{ exact: 4, field: 'value' }
), 'sumArray.exact');
});

test('rules.sumArray should return "sumArray.min" for invalid min', t => {
t.is(sumArray('field', [0], { min: 2, max: 4 }), 'sumArray.min');
t.is(sumArray(
'field',
[{ value: 0 }, { value: 1 }],
{ min: 2, max: 4, field: 'value' }
), 'sumArray.min');
});

test('rules.sumArray should return "sumArray.max" for invalid max', t => {
t.is(sumArray('field', [0, 1, 4], { min: 2, max: 4 }), 'sumArray.max');
t.is(sumArray(
'field',
[{ value: 0 }, { value: 1 }, { value: 4 }],
{ min: 2, max: 4, field: 'value' }
), 'sumArray.max');
});

test('rules.sumArray should return null for unapplicable data type', t => {
t.is(sumArray('field', 'value', { exact: 100 }), 'sumArray');
t.is(sumArray('field', 123, { exact: 100 }), 'sumArray');
t.is(sumArray('field', {}, { exact: 100 }), 'sumArray');
});

test('rules.sumArray should return null for valid exact', t => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the same test as on line 5

t.is(sumArray('field', [0, 1, 2], { exact: 3 }), null);
t.is(sumArray(
'field',
[{ value: 0 }, { value: 1 }, { value: 2 }],
{ exact: 3, field: 'value' }
), null);
});

test('rules.sumArray should return null for valid min/max', t => {
t.is(sumArray('field', [0, 1, 2], { min: 2, max: 4 }), null);
t.is(sumArray(
'field',
[{ value: 0 }, { value: 1 }, { value: 2 }],
{ min: 2, max: 4, field: 'value' }
), null);
});
1 change: 1 addition & 0 deletions src/rules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export { default as length } from './length';
export { default as numeric } from './numeric';
export { default as regex } from './regex';
export { default as required } from './required';
export { default as sumArray } from './sumArray';
50 changes: 50 additions & 0 deletions src/rules/sumArray.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@

import { get, isArray, isNil, isObject, reduce } from 'lodash';
import { isFloat } from 'validator';

function evaluateMin(value, min) {
if (isNil(min) || min === '') {
return false;
}
return !isFloat(value.toString(), { min: parseFloat(min) });
}

function evaluateMax(value, max) {
if (isNil(max) || max === '') {
return false;
}
return !isFloat(value.toString(), { max: parseFloat(max) });
}

export default function sumArray(field, value, options) {
if (!isArray(value)) {
return 'sumArray';
}

const number = reduce(
value,
(lastValue, item) => lastValue + parseFloat(isObject(item) ? item[options.field] : item),
0
);

if (!isFloat(number.toString())) {
return 'sumArray';
}

const exact = get(options, 'exact');
if (!isNil(exact) && parseFloat(number) !== parseFloat(exact)) {
return 'sumArray.exact';
}

const min = get(options, 'min');
if (evaluateMin(number, min)) {
return 'sumArray.min';
}

const max = get(options, 'max');
if (evaluateMax(number, max)) {
return 'sumArray.max';
}

return null;
}