All files / app validation.js

100% Statements 44/44
91.67% Branches 11/12
100% Functions 14/14
100% Lines 44/44
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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174                                      2x                                                       2x                                       16x   4x 4x 4x     30x   15x                     6x 6x 6x       2x 2x 2x 2x 1x   2x 1x   2x 2x         7x 7x 7x 7x 1x         6x 6x 6x               6x       6x 6x 6x 6x   6x               6x 6x 6x     10x 10x   6x 6x 6x 4x                             16x      
// @flow
 
import { Component } from 'react';
import LIVR from 'livr';
import pick from 'ramda/src/pick';
import omit from 'ramda/src/omit';
import merge from 'ramda/src/merge';
import __ from 'ramda/src/__';
import keys from 'ramda/src/keys';
import compose from 'ramda/src/compose';
import prop from 'ramda/src/prop';
import and from 'ramda/src/and';
import not from 'ramda/src/not';
import equals from 'ramda/src/equals';
import isEmpty from 'ramda/src/isEmpty';
import when from 'ramda/src/when';
import pipe from 'ramda/src/pipe';
import ContextTypes from './types/context-types';
 
const HAS_ERRORS = 'HAS_ERRORS';
 
type AliasedRule = {
    name: string,
    rules: Object,
    error: string
};
 
type Props = {
    data: Object,
    schema: Object,
    rules: Object,
    aliasedRules: Array<AliasedRule>,
    className: string,
    style: Object,
    errorCodes: Object,
    children?: any
};
 
type State = {
    errors: Object
};
 
type DataChunk = {
    name: string,
    value: any
};
 
LIVR.Validator.defaultAutoTrim(true);
 
export default class Validation extends Component {
    static childContextTypes = ContextTypes;
 
    static defaultProps = {
        rules: {},
        aliasedRules: [],
        className: '',
        style: {},
        errorCodes: {},
        data: {},
        schema: {}
    };
 
    state: State = {
        errors: {}
    };
 
    getChildContext() {
        return {
            setData: ({ name, value }: DataChunk) => {
                const { data } = this;
                data[name] = value;
                this.validateData(data, name);
            },
 
            getError: (name: string) => this.state.errors[name],
 
            getErrors: () => this.state.errors,
 
            className: this.props.className,
 
            style: this.props.style,
 
            errorCodes: this.props.errorCodes
        };
    }
 
    componentDidMount() {
        const { schema } = this.props;
        this.createValidator(schema);
        this.initialValidate();
    }
 
    componentWillReceiveProps({ schema: nextSchema, data: nextData }: Props) {
        const { schema, data } = this.props;
        const equalsSchema = equals(schema, nextSchema);
        const equalsData = equals(data, nextData);
        if (!equalsSchema) {
            this.createValidator(nextSchema);
        }
        if (!equalsData) {
            this.data = nextData;
        }
        Eif (!equalsSchema || !equalsData) {
            this.validateData(this.data, '');
        }
    }
 
    createValidator(schema: Object) {
        const { rules, aliasedRules } = this.props;
        this.validator = new LIVR.Validator(schema);
        this.validator.registerRules(rules);
        aliasedRules.forEach((rule: AliasedRule) => {
            this.validator.registerAliasedRule(rule);
        });
    }
 
    getNextErrors = (name: string, error: Object, errors: Object) => {
        const { errors: stateErrors } = this.state;
        const nextErrorsPipe = pipe(
            when(() => !isEmpty(error), merge(__, error)),
            when(compose(and(isEmpty(error)), prop(name)), omit([name])),
            when(compose(and(!errors), prop(HAS_ERRORS)), omit([HAS_ERRORS])),
            when(
                compose(and(errors), not, prop(HAS_ERRORS)),
                merge(__, { [HAS_ERRORS]: 1 })
            )
        );
        return nextErrorsPipe(stateErrors);
    };
 
    validateData(data: Object, name: string) {
        const { validator, getNextErrors } = this;
        validator.validate(data);
        const errors = validator.getErrors();
        const error = pick([name], errors || {});
 
        this.setState({
            errors: {
                ...getNextErrors(name, error, errors)
            }
        });
    }
 
    initialValidate() {
        const { validator } = this;
        const { data, schema } = this.props;
        const validationData = keys(
            schema
        ).reduce((acc: Object, key: string) => {
            acc[key] = data[key];
            return acc;
        }, {});
        validator.validate(validationData);
        const errors = validator.getErrors();
        if (errors) {
            this.setState({
                errors: {
                    [HAS_ERRORS]: 1
                }
            });
        }
    }
 
    validator: Object;
 
    props: Props;
 
    data = this.props.data;
 
    render() {
        return this.props.children;
    }
}