Skip to content

Commit

Permalink
Merge pull request #772 from poanetwork/validate-decimals-stepTwo-#769
Browse files Browse the repository at this point in the history
(Fix) Validate decimals in step two
  • Loading branch information
vbaranov authored Apr 10, 2018
2 parents 1c41784 + acfc648 commit 31c3a22
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 38 deletions.
8 changes: 6 additions & 2 deletions src/components/Common/TokenDecimals.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import React from 'react'
import { validateDecimals } from '../../utils/validations'
import { composeValidators, isLessOrEqualThan, isNonNegative, isRequired } from '../../utils/validations'
import { TEXT_FIELDS } from '../../utils/constants'
import { Field } from 'react-final-form'
import { InputField2 } from './InputField2'
import { acceptPositiveIntegerOnly } from '../../utils/utils'

export const TokenDecimals = ({ disabled, errorStyle }) => (
<Field
validate={validateDecimals}
validate={composeValidators(
isRequired(),
isNonNegative(),
isLessOrEqualThan("Should not be greater than 18")(18)
)}
component={InputField2}
parse={acceptPositiveIntegerOnly}
side="left"
Expand Down
2 changes: 1 addition & 1 deletion src/components/stepTwo/StepTwoForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const errorStyle = {
fontWeight: 'bold',
fontSize: '12px',
width: '100%',
height: '10px',
height: '20px',
}

export const StepTwoForm = ({
Expand Down
30 changes: 27 additions & 3 deletions src/components/stepTwo/__snapshots__/StepTwoForm.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ exports[`StepTwoForm Should render the component 1`] = `
"color": "red",
"fontSize": "12px",
"fontWeight": "bold",
"height": "10px",
"height": "20px",
"width": "100%",
}
}
Expand Down Expand Up @@ -84,7 +84,7 @@ exports[`StepTwoForm Should render the component 1`] = `
"color": "red",
"fontSize": "12px",
"fontWeight": "bold",
"height": "10px",
"height": "20px",
"width": "100%",
}
}
Expand Down Expand Up @@ -125,7 +125,31 @@ exports[`StepTwoForm Should render the component 1`] = `
"color": "red",
"fontSize": "12px",
"fontWeight": "bold",
"height": "10px",
"height": "20px",
"width": "100%",
}
}
/>
<p
className="error"
style={
Object {
"color": "red",
"fontSize": "12px",
"fontWeight": "bold",
"height": "20px",
"width": "100%",
}
}
/>
<p
className="error"
style={
Object {
"color": "red",
"fontSize": "12px",
"fontWeight": "bold",
"height": "20px",
"width": "100%",
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/stepTwo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class stepTwo extends Component {
tokenValues: {
name: this.props.tokenStore.name,
ticker: this.props.tokenStore.ticker,
decimals: this.props.tokenStore.decimals,
decimals: this.props.tokenStore.decimals || 0,
}
}
}
Expand Down
6 changes: 0 additions & 6 deletions src/utils/validations.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export const validators = (type, value) => {
return {
name: value && typeof value === 'string' && 1 <= value.length && value.length <= 30,
ticker: /^[a-zA-Z0-9]{1,5}$/.test(value),
decimals: (value === undefined || value === '') || (/^[0-9]+$/.test(value) && 0 <= value && value <= 18),
}[type] || false
}

Expand All @@ -21,11 +20,6 @@ export const validateTicker = (value) => {
return isValid ? undefined : VALIDATION_MESSAGES.TICKER
}

export const validateDecimals = (value) => {
const isValid = validators('decimals', value)
return isValid ? undefined : VALIDATION_MESSAGES.DECIMALS
}

export const isPositive = (errorMsg = VALIDATION_MESSAGES.POSITIVE) => (value) => {
const isValid = value > 0
return isValid ? undefined : errorMsg
Expand Down
25 changes: 0 additions & 25 deletions src/utils/validations.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
isNonNegative,
isPositive,
isRequired,
validateDecimals,
validateTicker,
validateTokenName,
validators
Expand Down Expand Up @@ -63,30 +62,6 @@ describe('validateTicker', () => {
})
})

describe('validateDecimals', () => {
[
{ value: '100', expected: VALIDATION_MESSAGES.DECIMALS },
{ value: '-10', expected: VALIDATION_MESSAGES.DECIMALS },
{ value: '20', expected: VALIDATION_MESSAGES.DECIMALS },
{ value: '1.5', expected: VALIDATION_MESSAGES.DECIMALS },
{ value: '1.', expected: VALIDATION_MESSAGES.DECIMALS },
{ value: '1e1', expected: VALIDATION_MESSAGES.DECIMALS },
{ value: '--', expected: VALIDATION_MESSAGES.DECIMALS },
{ value: undefined, expected: undefined },
{ value: '', expected: undefined },
{ value: '0', expected: undefined },
{ value: '1', expected: undefined },
{ value: '10', expected: undefined },
{ value: '18', expected: undefined },
].forEach(testCase => {
const action = testCase.expected === undefined ? 'pass' : 'fail'

it(`Should ${action} for '${testCase.value}'`, () => {
expect(validateDecimals(testCase.value)).toBe(testCase.expected)
})
})
})

describe('isPositive', () => {
const testCases = [
{ value: '1.01', errorMessage: undefined, expected: undefined },
Expand Down

0 comments on commit 31c3a22

Please sign in to comment.