Skip to content
This repository has been archived by the owner on Apr 15, 2019. It is now read-only.

Show a warning if passphrase contains extra whitespace #815

Merged
merged 1 commit into from
Oct 6, 2017
Merged
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
28 changes: 27 additions & 1 deletion src/components/passphraseInput/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,18 @@ class PassphraseInput extends React.Component {

handleValueChange(value) {
let error;
let warning;

if (!value) {
error = 'Required';
} else if (!isValidPassphrase(value)) {
error = this.getPassphraseValidationError(value);
} else if (this.hasExtraWhitespace(value)) {
const warningMessage = this.getPasshraseWhitespaceWarning(value);
warning = warningMessage ? `Warning: ${warningMessage}` : null;
}

this.setState({ warning });
this.props.onChange(value, error);
}

Expand All @@ -47,6 +54,25 @@ class PassphraseInput extends React.Component {
return 'Passphrase is not valid';
}

// eslint-disable-next-line class-methods-use-this
hasExtraWhitespace(passphrase) {
const normalizedValue = passphrase.replace(/ +/g, ' ').trim();
return normalizedValue !== passphrase;
}

// eslint-disable-next-line class-methods-use-this
getPasshraseWhitespaceWarning(passphrase) {
if (passphrase.replace(/^\s+/, '') !== passphrase) {
return 'Passphrase contains unnecessary whitespace at the beginning';
} else if (passphrase.replace(/\s+$/, '') !== passphrase) {
return 'Passphrase contains unnecessary whitespace at the end';
} else if (passphrase.replace(/\s+/g, ' ') !== passphrase) {
return 'Passphrase contains extra whitespace between words';
}

return null;
}

toggleInputType() {
this.setState({ inputType: this.state.inputType === 'password' ? 'text' : 'password' });
}
Expand All @@ -56,7 +82,7 @@ class PassphraseInput extends React.Component {
<div className={styles.wrapper}>
<Input label={this.props.label} required={true}
className={this.props.className}
error={this.props.error}
error={this.props.error || this.state.warning}
value={this.props.value || ''}
type={this.state.inputType}
theme={this.props.theme}
Expand Down