<commit_before>// @flow
import { Component, createElement } from 'react'
import PropTypes from 'prop-types'
import invariant from 'invariant'
import createConnectedField from './ConnectedField'
import shallowCompare from './util/shallowCompare'
import prefixName from './util/prefixName'
import type {
  ConnectedComponent,
  Structure,
  ReactContext
} from './types.js.flow'
import type { InstanceApi as ConnectedFieldInstanceApi } from './ConnectedField.types.js.flow'
import type { Component as ReactComponent } from 'react'
import type { Props } from './Field.types.js.flow'

const createField = (structure: Structure<*, *>) => {
  const ConnectedField = createConnectedField(structure)

  const { setIn } = structure

  class Field extends Component {
    props: Props
    context: ReactContext

    ref: ConnectedComponent<ConnectedFieldInstanceApi>

    constructor(props: Props, context: ReactContext) {
      super(props, context)
      if (!context._reduxForm) {
        throw new Error(
          'Field must be inside a component decorated with reduxForm()'
        )
      }
    }

    shouldComponentUpdate(nextProps: Props) {
      return shallowCompare(this, nextProps)
    }

    componentWillMount() {
      this.context._reduxForm.register(
        this.name,
        'Field',
        () => this.props.validate,
        () => this.props.warn
      )
    }

    componentWillReceiveProps(nextProps: Props) {
      if (
        this.props.name !== nextProps.name ||
        this.props.validate !== nextProps.validate ||
        this.props.warn !== nextProps.warn
      ) {
        // unregister old name
        this.context._reduxForm.unregister(this.name)
        // register new name
        this.context._reduxForm.register(
          prefixName(this.context, nextProps.name),<commit_msg>Fixed field-level validation bug (#3204)<commit_after>// @flow
import { Component, createElement } from 'react'
import PropTypes from 'prop-types'
import invariant from 'invariant'
import createConnectedField from './ConnectedField'
import shallowCompare from './util/shallowCompare'
import prefixName from './util/prefixName'
import plain from './structure/plain'
import type {
  ConnectedComponent,
  Structure,
  ReactContext
} from './types.js.flow'
import type { InstanceApi as ConnectedFieldInstanceApi } from './ConnectedField.types.js.flow'
import type { Component as ReactComponent } from 'react'
import type { Props } from './Field.types.js.flow'

const createField = (structure: Structure<*, *>) => {
  const ConnectedField = createConnectedField(structure)

  const { setIn } = structure

  class Field extends Component {
    props: Props
    context: ReactContext

    ref: ConnectedComponent<ConnectedFieldInstanceApi>

    constructor(props: Props, context: ReactContext) {
      super(props, context)
      if (!context._reduxForm) {
        throw new Error(
          'Field must be inside a component decorated with reduxForm()'
        )
      }
    }

    shouldComponentUpdate(nextProps: Props) {
      return shallowCompare(this, nextProps)
    }

    componentWillMount() {
      this.context._reduxForm.register(
        this.name,
        'Field',
        () => this.props.validate,
        () => this.props.warn
      )
    }

    componentWillReceiveProps(nextProps: Props) {
      if (
        this.props.name !== nextProps.name ||
        // use deepEqual here because they could be a function or an array of functions
        !plain.deepEqual(this.props.validate, nextProps.validate) ||
        !plain.deepEqual(this.props.warn, nextProps.warn)
      ) {
        // unregister old name
        this.context._reduxForm.unregister(this.name)
        // register new name
        this.context._reduxForm.register(
          prefixName(this.context, nextProps.name),
{'fraction_non_alphanumeric': 0.10491717065474625, 'fraction_numerical': 0.001051801209571391, 'mean_word_length': 3.0211416490486256, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7204771', 'n_tokens_mistral': 1058, 'n_tokens_neox': 1039, 'n_words': 267}