Skip to content

Commit

Permalink
Add validation to new root profile attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
luisrudge committed May 29, 2019
1 parent 5d2fbc6 commit a48ccbd
Show file tree
Hide file tree
Showing 5 changed files with 240 additions and 226 deletions.
37 changes: 37 additions & 0 deletions src/__tests__/field/field.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import Immutable from 'immutable';
import { setField } from '../../field';

const createModel = field =>
Immutable.fromJS({
field: {
[field]: `old_test_${field}`
}
});

const testField = (field, maxLength) => {
const m = createModel(field);
expect(setField(m, field, '').toJS().field[field].valid).toBe(false);
expect(setField(m, field, 'test_value').toJS().field[field].valid).toBe(true);
if (maxLength) {
expect(setField(m, field, 'a'.repeat(maxLength + 1)).toJS().field[field].valid).toBe(false);
}
};
describe('field/index', () => {
describe('default validation', () => {
it('validates family_name', () => {
testField('family_name', 150);
});
it('validates family_name', () => {
testField('given_name', 150);
});
it('validates name', () => {
testField('name', 300);
});
it('validates nickname', () => {
testField('nickname', 300);
});
it('validates other fields', () => {
testField('test');
});
});
});
7 changes: 4 additions & 3 deletions src/__tests__/field/username.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ describe('field/username', () => {
});
});
describe('usernameLooksLikeEmail()', () => {
it('checks for @', () => {
it('checks for @ and .', () => {
expect(username.usernameLooksLikeEmail('[email protected]')).toBe(true);
expect(username.usernameLooksLikeEmail('tt.com')).toBe(false);
expect(username.usernameLooksLikeEmail('t@tcom')).toBe(false);
});
});
describe('getUsernameValidation()', () => {
Expand Down Expand Up @@ -134,11 +135,11 @@ describe('field/username', () => {
expectToFailWith('aaaaaa');
});
it('validates invalid chars', () => {
const invalidChars = `{}[],;?/\\!@#$%¨&*()¹²³\`~^´ªº§£¢¬<>|"' `.split('');
const invalidChars = `{}[],;?/\\%¨&*()¹²³ªº§£¢¬<>|" `.split('');
invalidChars.forEach(i => expectToFailWith(`aa${i}`));
});
it('accepts letters, numbers, `_`, `-`, `+` and `.`', () => {
const validChars = `_-+.`.split('');
const validChars = `_+-.!#$'^\`~@`.split('');
validChars.forEach(i => expectToSuccedWith(`aa${i}`));
});
});
Expand Down
24 changes: 20 additions & 4 deletions src/field/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,31 @@ import trim from 'trim';
import OptionSelectionPane from './option_selection_pane';
import * as l from '../core/index';

export function setField(m, field, value, validator = str => trim(str).length > 0, ...args) {
const minMax = (value, min, max) => value.length >= min && value.length <= max;

const getDefaultValidator = field => {
switch (field) {
case 'family_name':
return str => minMax(trim(str), 1, 150);
case 'given_name':
return str => minMax(trim(str), 1, 150);
case 'name':
return str => minMax(trim(str), 1, 300);
case 'nickname':
return str => minMax(trim(str), 1, 300);

default:
return str => trim(str).length > 0;
}
};

export function setField(m, field, value, validator = getDefaultValidator(field), ...args) {
const prevValue = m.getIn(['field', field, 'value']);
const prevShowInvalid = m.getIn(['field', field, 'showInvalid'], false);
const validation = validate(validator, value, ...args);

return m.mergeIn(
['field', field],
validation,
Map({
validation.merge({
value: value,
showInvalid: prevShowInvalid && prevValue === value
})
Expand Down
6 changes: 3 additions & 3 deletions src/field/username.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { validateEmail } from './email';
import { databaseConnection } from '../connection/database';
import trim from 'trim';

const DEFAULT_CONNECTION_VALIDATION = { username: { min: 1, max: 15 } };
const regExp = /^[a-zA-Z0-9_+\-.]+$/;
const DEFAULT_CONNECTION_VALIDATION = { username: { min: 1, max: 128 } };
const regExp = /^[a-zA-Z0-9_+\-.!#\$\^`~@']*$/;

function validateUsername(str, validateFormat, settings = DEFAULT_CONNECTION_VALIDATION.username) {
// If the connection does not have validation settings, it should only check if the field is empty.
Expand Down Expand Up @@ -55,5 +55,5 @@ export function setUsername(m, str, usernameStyle = 'username', validateUsername
}

export function usernameLooksLikeEmail(str) {
return str.indexOf('@') > -1;
return str.indexOf('@') > -1 && str.indexOf('.') > -1;
}
Loading

0 comments on commit a48ccbd

Please sign in to comment.