forked from coralproject/talk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce final form, i18n helper, validators and refactor AddEmailAd…
…dress (coralproject#1636) * Introduce final form, validators and refactor AddEmailAddress * First i18n script * Sort locales * Add delete and copy action * Copy over available validator translations * Add comments * Export validations in plugin-api * Linting * yarn.lock * Sort locales * Drop unused translations * Add translations for validators * Add action drop-unused * Add comments * Add note about limitation * Fix desc
- Loading branch information
Showing
31 changed files
with
4,910 additions
and
4,582 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import t from 'coral-framework/services/i18n'; | ||
|
||
/** | ||
* createNotificationService returns a notification services based on pym. | ||
* @param {func} condition callback that checks that the given argument is valid. | ||
* @param {error} string error that is displayed when validation fails. | ||
* @return {func} validator function | ||
*/ | ||
export function createValidator(condition, error) { | ||
return (v, values) => (condition(v, values) ? undefined : error); | ||
} | ||
|
||
/** | ||
* composeValidators chains validators and runs them in sequence until | ||
* one validator fails and returns an error message. | ||
* @param {func[]} validators array of validator functions. | ||
* @return {func} validator fuction | ||
*/ | ||
export function composeValidators(...validators) { | ||
return value => | ||
validators.reduce( | ||
(error, validator) => error || validator(value), | ||
undefined | ||
); | ||
} | ||
|
||
/** | ||
* required checks that the value is truthy. | ||
* @return {func} validator fuction | ||
*/ | ||
export const required = createValidator(v => !!v, t('validators.required')); | ||
|
||
/** | ||
* Confirm will check that this field has the same value as another. | ||
* @param {string} key key of other form field to compare with. | ||
* @param {error} string error that is displayed when validation fails. | ||
* @return {func} validator fuction | ||
*/ | ||
export const confirm = (key, error) => | ||
createValidator((v, data) => v === data[key], error); | ||
|
||
/** | ||
* Verify email. | ||
* @return {func} validator fuction | ||
*/ | ||
export const verifyEmail = createValidator( | ||
email => /^.+@.+\..+$/.test(email), | ||
t('validators.verify_email') | ||
); | ||
|
||
/** | ||
* Confirm email. | ||
* @param {string} key key of other form field to compare with. | ||
* @return {func} validator fuction | ||
*/ | ||
export const confirmEmail = key => confirm(key, t('validators.confirm_email')); | ||
|
||
/** | ||
* Verify password. | ||
* @return {func} validator fuction | ||
*/ | ||
export const verifyPassword = createValidator( | ||
pass => /^(?=.{8,}).*$/.test(pass), | ||
t('validators.verify_password') | ||
); | ||
|
||
/** | ||
* Confirm password. | ||
* @param {string} key key of other form field to compare with. | ||
* @return {func} validator fuction | ||
*/ | ||
export const confirmPassword = key => | ||
confirm(key, t('validators.confirm_password')); | ||
|
||
/** | ||
* Verify username. | ||
* @return {func} validator fuction | ||
*/ | ||
export const verifyUsername = createValidator( | ||
username => /^[a-zA-Z0-9_]+$/.test(username), | ||
t('validators.verify_username') | ||
); | ||
|
||
/** | ||
* Verify organization name. | ||
* @return {func} validator fuction | ||
*/ | ||
export const verifyOrganizationName = createValidator( | ||
org => /^[a-zA-Z0-9_ ]+$/.test(org), | ||
t('validators.verify_organization_name') | ||
); |
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Oops, something went wrong.