All files / src index.js

100% Statements 12/12
100% Branches 6/6
100% Functions 8/8
100% Lines 10/10
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50                                        6x     11x       6x     10x       1x 10x     6x 6x   6x         4x      
import R from 'ramda';
 
export { emailSchema, isEmail } from './email';
export { isOfficialAddress, isValidAddress, isAbsoluteUrl, websiteSchema } from './website';
export { newPasswordSchema } from './password';
export { isPhoneNumber, phoneNumberSchema } from './phone';
export { commentSchema } from './comment';
export { firstNameSchema } from './firstName';
export { lastNameSchema } from './lastName';
export {
    isRequired,
    isNumber,
    hasLengthBetween,
    isEqualOrShorterThan,
    isStringWithoutNumbers,
    hasAtLeastOneLetter,
    setupCheck,
    createNameSchema
} from './common';
 
const reduceToFieldsOnlyInSchema = (schema, values) => R.pipe(
    R.keys,
    R.reduce((acc, val) => {
        return schema[val] !== undefined ? { ...acc, [val]: values[val] } : acc;
    }, {})
);
 
const setEmptyValues = (schema, values) => R.pipe(
    R.keys,
    R.reduce((acc, val) => {
        return acc[val] === undefined ? { ...acc, [val]: '' } : acc;
    }, { ...values })
)(schema);
 
const trimConditionally = (val) => {
    return R.type(val) === 'String' ? R.trim(val) : val;
};
 
export default (schema) => (values, { intl }) => {
    const initialValues = setEmptyValues(schema, values);
 
    return R.pipe(
        reduceToFieldsOnlyInSchema(schema, initialValues),
        R.map(trimConditionally),
        R.evolve(schema),
        R.reject(R.isNil),
        R.map((message) => intl.formatMessage(message))
    )(initialValues);
};