diff --git a/packages/component-faraday-ui/src/PasswordConfirmation.js b/packages/component-faraday-ui/src/PasswordConfirmation.js index 44eef64e6ac220a169f90476ff0738bfe0068bc4..9bd7fcbd7e568cdd39353bf32831f2952dcd39bb 100644 --- a/packages/component-faraday-ui/src/PasswordConfirmation.js +++ b/packages/component-faraday-ui/src/PasswordConfirmation.js @@ -14,7 +14,7 @@ import { atLeastOneUppercase, atLeastOneLowerrcase, atLeastOnePunctuation, -} from '../../components-faraday/src/components/utils' +} from './Utils.js' const PasswordField = input => <TextField {...input} type="password" /> diff --git a/packages/component-faraday-ui/src/Utils.js b/packages/component-faraday-ui/src/Utils.js new file mode 100644 index 0000000000000000000000000000000000000000..704a4daf3c3eb1a9fcd00da496ae017676bd73b1 --- /dev/null +++ b/packages/component-faraday-ui/src/Utils.js @@ -0,0 +1,76 @@ +export const minLength = (value, min) => !!(value && value.length >= min) + +export const atLeastOneUppercase = value => { + const uppercaseRegex = new RegExp(/([A-Z])+/) + return uppercaseRegex.test(value) +} +export const atLeastOneLowerrcase = value => { + const lowercaseRegex = new RegExp(/([a-z])+/) + return lowercaseRegex.test(value) +} +export const atLeastOneDigit = value => { + const digitRegex = new RegExp(/([0-9])+/) + return digitRegex.test(value) +} +export const atLeastOnePunctuation = value => { + const punctuationRegex = new RegExp(/([,'!@#$%^&*=(){}[\]<>?/\\|.:;_-])+/) + return punctuationRegex.test(value) +} + +export const passwordValidator = values => { + const errors = {} + const { password, confirmPassword } = values + + if ( + !( + minLength(password, 6) && + atLeastOneUppercase(password) && + atLeastOneLowerrcase(password) && + atLeastOnePunctuation(password) && + atLeastOneDigit(password) + ) + ) { + errors.password = 'Password criteria not met' + } + if (!password) { + errors.password = 'Required' + } + if (!confirmPassword) { + errors.confirmPassword = 'Required' + } else if (confirmPassword !== password) { + errors.confirmPassword = "Passwords don't match." + } + + return errors +} + +export const changePasswordValidator = values => { + const { + currentPassword = '', + confirmNewPassword = '', + password = '', + } = values + const errors = {} + if ( + !( + minLength(password, 6) && + atLeastOneUppercase(password) && + atLeastOneLowerrcase(password) && + atLeastOnePunctuation(password) && + atLeastOneDigit(password) + ) + ) { + errors.password = 'Password criteria not met' + } + if (!currentPassword) { + errors.currentPassword = 'Required' + } + + if (!password) { + errors.password = 'Required' + } else if (password !== confirmNewPassword) { + errors.confirmNewPassword = "Passwords don't match." + } + + return errors +} diff --git a/packages/components-faraday/src/components/utils.js b/packages/components-faraday/src/components/utils.js index 5ec731314652e97fb643bb0c35a8f34e1f249b1c..6ca28eae7dcfa5cea119e08190b63ba4c3b9b452 100644 --- a/packages/components-faraday/src/components/utils.js +++ b/packages/components-faraday/src/components/utils.js @@ -101,82 +101,6 @@ export const redirectToError = redirectFn => err => { errorText || 'Something went wrong. Please try again.', ) } -export const minLength = (value, min) => !!(value && value.length >= min) -export const atLeastOneUppercase = value => { - const uppercaseRegex = new RegExp(/([A-Z])+/) - return uppercaseRegex.test(value) -} -export const atLeastOneLowerrcase = value => { - const lowercaseRegex = new RegExp(/([a-z])+/) - return lowercaseRegex.test(value) -} -export const atLeastOneDigit = value => { - const digitRegex = new RegExp(/([0-9])+/) - return digitRegex.test(value) -} -export const atLeastOnePunctuation = value => { - const punctuationRegex = new RegExp(/([,'!@#$%^&*=(){}[\]<>?/\\|.:;_-])+/) - return punctuationRegex.test(value) -} - -export const passwordValidator = values => { - const errors = {} - const { password, confirmPassword } = values - - if ( - !( - minLength(password, 6) && - atLeastOneUppercase(password) && - atLeastOneLowerrcase(password) && - atLeastOnePunctuation(password) && - atLeastOneDigit(password) - ) - ) { - errors.password = 'Password criteria not met' - } - if (!password) { - errors.password = 'Required' - } - if (!confirmPassword) { - errors.confirmPassword = 'Required' - } else if (confirmPassword !== password) { - errors.confirmPassword = "Passwords don't match." - } - - return errors -} - -export const changePasswordValidator = values => { - const { - currentPassword = '', - confirmNewPassword = '', - password = '', - } = values - const errors = {} - if ( - !( - minLength(password, 6) && - atLeastOneUppercase(password) && - atLeastOneLowerrcase(password) && - atLeastOnePunctuation(password) && - atLeastOneDigit(password) - ) - ) { - errors.password = 'Password criteria not met' - } - if (!currentPassword) { - errors.currentPassword = 'Required' - } - - if (!password) { - errors.password = 'Required' - } else if (password !== confirmNewPassword) { - errors.confirmNewPassword = "Passwords don't match." - } - - return errors -} - export const parseSearchParams = url => { const params = new URLSearchParams(url) const parsedObject = {}