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 | 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 12x 12x 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 'lodash/pick'; import omit from 'lodash/omit'; import assign from 'lodash/assign'; import keys from 'lodash/keys'; import equals from 'ramda/src/equals'; import isEmpty from 'ramda/src/isEmpty'; import partialRight from 'ramda/src/partialRight'; 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); }); } validateData(data: Object, name: string) { const { errors: stateErrors } = this.state; const { validator } = this; validator.validate(data); const errors = validator.getErrors(); const error = pick(errors, name); const partialOmit = fields => partialRight(omit, [fields]); const partialAssign = obj => partialRight(assign, [obj]); const getNextErrors = pipe( when(() => !isEmpty(error), partialAssign(error)), when(() => stateErrors[name] && isEmpty(error), partialOmit(name)), when( () => stateErrors[HAS_ERRORS] && !errors, partialOmit(HAS_ERRORS) ), when( () => !stateErrors[HAS_ERRORS] && errors, partialAssign({ HAS_ERRORS: 1 }) ) ); this.setState({ errors: { ...getNextErrors(stateErrors) } }); } 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; } } |