refactor: use validate util in client

This commit is contained in:
Oliver Eyton-Williams
2019-11-11 20:05:05 +01:00
committed by mrugesh
parent e77408827a
commit e1236a0c15

View File

@ -9,7 +9,6 @@ import {
Alert, Alert,
FormGroup FormGroup
} from '@freecodecamp/react-bootstrap'; } from '@freecodecamp/react-bootstrap';
import isAscii from 'validator/lib/isAscii';
import { import {
validateUsername, validateUsername,
@ -18,6 +17,7 @@ import {
} from '../../redux/settings'; } from '../../redux/settings';
import BlockSaveButton from '../helpers/form/BlockSaveButton'; import BlockSaveButton from '../helpers/form/BlockSaveButton';
import FullWidthRow from '../helpers/FullWidthRow'; import FullWidthRow from '../helpers/FullWidthRow';
import { validate } from '../../../../utils/validate';
const propTypes = { const propTypes = {
isValidUsername: PropTypes.bool, isValidUsername: PropTypes.bool,
@ -44,14 +44,6 @@ const mapDispatchToProps = dispatch =>
dispatch dispatch
); );
const invalidCharsRE = /[/\s?:@=&"'<>#%{}|\\^~[\]`,.;!*()$]/;
const invlaidCharError = {
valid: false,
error: 'Username contains invalid characters.'
};
const valididationSuccess = { valid: true, error: null };
const usernameTooShort = { valid: false, error: 'Username is too short' };
const hex = '[0-9a-f]'; const hex = '[0-9a-f]';
const tempUserRegex = new RegExp(`^fcc${hex}{8}-(${hex}{4}-){3}${hex}{12}$`); const tempUserRegex = new RegExp(`^fcc${hex}{8}-(${hex}{4}-){3}${hex}{12}$`);
@ -119,24 +111,14 @@ class UsernameSettings extends Component {
} }
validateFormInput(formValue) { validateFormInput(formValue) {
if (formValue.length < 3) { return validate(formValue);
return usernameTooShort;
}
if (!isAscii(formValue)) {
return invlaidCharError;
}
if (invalidCharsRE.test(formValue)) {
return invlaidCharError;
}
return valididationSuccess;
} }
renderAlerts(validating, error, isValidUsername) { renderAlerts(validating, error, isValidUsername) {
if (!validating && error) { if (!validating && error) {
return ( return (
<FullWidthRow> <FullWidthRow>
<Alert bsStyle='danger'>{error}</Alert> <Alert bsStyle='danger'>{'Username ' + error}</Alert>
</FullWidthRow> </FullWidthRow>
); );
} }