Skip to content

Commit

Permalink
Add specific validation for min/max whitelist values
Browse files Browse the repository at this point in the history
  • Loading branch information
fernandomg committed Apr 9, 2018
1 parent 7ace876 commit e93165c
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/utils/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export const VALIDATION_MESSAGES = {
REQUIRED: 'This field is required',
DECIMAL_PLACES: 'Decimals should not exceed the amount of decimals specified',
LESS_OR_EQUAL: 'Should be less or equal than the specified value',
GREATER_OR_EQUAL: 'Should be greater than the specified value',
GREATER_OR_EQUAL: 'Should be greater or equal than the specified value',
INTEGER: 'Should be integer',
DATE_IN_FUTURE: 'Should be set in the future',
DATE_IS_PREVIOUS: 'Should be previous than specified time',
Expand Down
22 changes: 22 additions & 0 deletions src/utils/validations.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,28 @@ export const validateDecimals = (value) => {
return isValid ? undefined : VALIDATION_MESSAGES.DECIMALS
}

export const validateWhitelistMin = ({ min, max, decimals }) => {
const listOfErrors = composeValidators(
isRequired(),
isNonNegative(),
isDecimalPlacesNotGreaterThan(`Decimals should not exceed ${decimals} places`)(decimals),
isLessOrEqualThan('Should be less or equal than max')(max)
)(min)

return listOfErrors ? listOfErrors.shift() : ''
}

export const validateWhitelistMax = ({ min, max, decimals }) => {
const listOfErrors = composeValidators(
isRequired(),
isNonNegative(),
isDecimalPlacesNotGreaterThan(`Decimals should not exceed ${decimals} places`)(decimals),
isGreaterOrEqualThan('Should be greater or equal than min')(min)
)(max)

return listOfErrors ? listOfErrors.shift() : ''
}

export const isPositive = (errorMsg = VALIDATION_MESSAGES.POSITIVE) => (value) => {
const isValid = value > 0
return isValid ? undefined : errorMsg
Expand Down
55 changes: 55 additions & 0 deletions src/utils/validations.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import {
validateDecimals,
validateTicker,
validateTokenName,
validateWhitelistMax,
validateWhitelistMin,
validators
} from './validations'
import { VALIDATION_MESSAGES } from './constants'
Expand Down Expand Up @@ -87,6 +89,58 @@ describe('validateDecimals', () => {
})
})

describe('validateWhitelistMin', () => {
const testCases = [
{ value: { min: '0', max: '1', decimals: '0' }, expected: '' },
{ value: { min: '0', max: '10', decimals: '0' }, expected: '' },
{ value: { min: '5', max: '11', decimals: '5' }, expected: '' },
{ value: { min: '10', max: '10', decimals: '0' }, expected: '' },
{ value: { min: '10', max: '10', decimals: '7' }, expected: '' },
{ value: { min: '5', max: '11.123456', decimals: '3' }, expected: '' },
{ value: { min: '5.123', max: '11.123456', decimals: '3' }, expected: '' },
{ value: { min: '5.123456', max: '11.123456', decimals: '3' }, expected: 'Decimals should not exceed 3 places' },
{ value: { min: '5.123456', max: '11.123', decimals: '3' }, expected: 'Decimals should not exceed 3 places' },
{ value: { min: '25.123456', max: '11.123', decimals: '3' }, expected: 'Decimals should not exceed 3 places' },
{ value: { min: '25.123', max: '11.123', decimals: '3' }, expected: 'Should be less or equal than max' },
{ value: { min: '5', max: '3', decimals: '0' }, expected: 'Should be less or equal than max' },
]

testCases.forEach(testCase => {
const action = testCase.expected ? 'fail' : 'pass'
const { min, max, decimals } = testCase.value

it(`Should ${action} for { min: '${min}', max: '${max}', decimals: '${decimals}' }`, () => {
expect(validateWhitelistMin({ ...testCase.value })).toBe(testCase.expected)
})
})
})

describe('validateWhitelistMax', () => {
const testCases = [
{ value: { min: '0', max: '1', decimals: '0' }, expected: '' },
{ value: { min: '0', max: '10', decimals: '0' }, expected: '' },
{ value: { min: '5', max: '11', decimals: '5' }, expected: '' },
{ value: { min: '10', max: '10', decimals: '0' }, expected: '' },
{ value: { min: '10', max: '10', decimals: '7' }, expected: '' },
{ value: { min: '5.123456', max: '11', decimals: '3' }, expected: '' },
{ value: { min: '5.123345', max: '11.123', decimals: '3' }, expected: '' },
{ value: { min: '5.123456', max: '11.123456', decimals: '3' }, expected: 'Decimals should not exceed 3 places' },
{ value: { min: '5.123', max: '11.123456', decimals: '3' }, expected: 'Decimals should not exceed 3 places' },
{ value: { min: '25.123', max: '11.123456', decimals: '3' }, expected: 'Decimals should not exceed 3 places' },
{ value: { min: '25.123', max: '11.123', decimals: '3' }, expected: 'Should be greater or equal than min' },
{ value: { min: '5', max: '3', decimals: '0' }, expected: 'Should be greater or equal than min' },
]

testCases.forEach(testCase => {
const action = testCase.expected ? 'fail' : 'pass'
const { min, max, decimals } = testCase.value

it(`Should ${action} for { min: '${min}', max: '${max}', decimals: '${decimals}' }`, () => {
expect(validateWhitelistMax({ ...testCase.value })).toBe(testCase.expected)
})
})
})

describe('isPositive', () => {
const testCases = [
{ value: '1.01', errorMessage: undefined, expected: undefined },
Expand Down Expand Up @@ -541,3 +595,4 @@ describe('composeValidators', () => {
expect(listOfErrors).toBeUndefined()
})
})

0 comments on commit e93165c

Please sign in to comment.