Skip to content
Closed
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
95 changes: 46 additions & 49 deletions services/app/apps/codebattle/assets/js/widgets/formik/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,41 @@ const emailSchema = Yup.string()
.matches(/^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,})$/i, 'Can\'t contain special symbols')
.required('Email required');

const nameSchema = settings => Yup.string()
.strict()
.min(3, 'Should be at least 3 characters')
.max(16, 'Should be 16 characters or less')
.test(
'start-or-end-with-empty-symbols',
'Can\'t start or end with empty symbols',
value => {
if (!value) {
return true;
}
const invalidSymbolIndex = invalidSymbols.findIndex(invalidSymbol => (
value.startsWith(invalidSymbol) || value.endsWith(invalidSymbol)
));
return invalidSymbolIndex === -1;
},
)
.test(
'max',
'Should be 16 characters or less',
(name = '') => (
settings.name === name || name.length <= 16
),
)
.matches(
/^[a-zA-Z]+[a-zA-Z0-9_-\s{1}][a-zA-Z0-9_]+$/i,
'Should contain Latin letters, numbers and underscores. Only begin with latin letter',
)
.trim();

const schemas = {
userSettings: settings => ({
name: Yup.string()
.strict()
.required("Field can't be empty")
.min(3, 'Should be at least 3 characters')
// .max(5, 'Should be 16 character(s) or less')
.test(
'max',
'Should be 16 character(s) or less',
(name = '') => (
settings.name === name || name.length <= 16
),
)
.trim(),
clan: Yup.string()
.strict(),
name: nameSchema(settings)
.required("Field can't be empty"),
clan: Yup.string().strict(),
}),
resetPassword: {
email: emailSchema,
Expand All @@ -45,40 +63,19 @@ const schemas = {
password: Yup.string().required('Password required'),
},
signUp: {
name: Yup
.string()
.test(
'start-or-end-with-empty-symbols',
'Can\'t start or end with empty symbols',
value => {
if (!value) {
return true;
}
const invalidSymbolIndex = invalidSymbols.findIndex(invalidSymbol => (
value.startsWith(invalidSymbol) || value.endsWith(invalidSymbol)
));

return invalidSymbolIndex === -1;
},
)
.min(3, 'Should be from 3 to 16 characters')
.max(16, 'Should be from 3 to 16 characters')
.matches(
/^[a-zA-Z]+[a-zA-Z0-9_-\s{1}][a-zA-Z0-9_]+$/i,
'Should contain Latin letters, numbers and underscores. Only begin with latin letter',
)
.required('Nickname required'),
email: emailSchema,
password: Yup
.string()
.matches(/^\S*$/, 'Can\'t contain empty symbols')
.min(6, 'Should be from 6 to 16 characters')
.max(16, 'Should be from 6 to 16 characters')
.required('Password required'),
passwordConfirmation: Yup
.string()
.required('Confirmation required')
.oneOf([Yup.ref('password')], 'Passwords must match'),
name: nameSchema()
.required('Nickname required'),
email: emailSchema,
password: Yup
.string()
.matches(/^\S*$/, 'Can\'t contain empty symbols')
.min(6, 'Password should be from 6 to 16 characters')
.max(16, 'Password should be from 6 to 16 characters')
.required('Password required'),
passwordConfirmation: Yup
.string()
.required('Confirmation required')
.oneOf([Yup.ref('password')], 'Passwords must match'),
},
};

Expand Down
Loading