mirror of
https://github.com/wassname/talk.git
synced 2026-06-29 23:43:19 +08:00
41fab3e11c
* 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
92 lines
2.6 KiB
JavaScript
92 lines
2.6 KiB
JavaScript
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')
|
|
);
|