Skip to content
This repository was archived by the owner on Jan 18, 2025. It is now read-only.
Ernest edited this page May 2, 2020 · 2 revisions

Default Config

The following is all configs(and its default values).

FormValidation.config({
  validateOnBound: false,
  validateOnUpdated: true
})

Global Config

You can use second parameter to set Schema-scoped config.

FormValidation.createSchema({}, {
  config: {
    validateOnBound: false,
    validateOnUpdated: true
  }
})

Local Config

You can even override global config for any partial schema.

FormValidation.createSchema({
  $config: {
    validateOnBound: false,
    validateOnUpdated: true
  }
})

If

Definition

$if: If

type Param = { value?: any, key?: string, path?: Array<string>, target?: any, params?: Object }
type If = ({ value, key, path, target, params }: Param) => boolean

Example

For example, this feature would be useful for dynamic validation.

const form1 = {
  ip: [
    '8.8.8.8'
  ]
}

const form2 = {
  ip: '8.8.8.8'
}

const schema = FormValidation.createSchema({
  ip: [
    {
      // if
      $if: ({ value }) => Array.isArray(value),
      $rule: minLength(1),
      $iter: {
        $rule: ipAddress
      }
    },
    {
      // else if
      $if: ({ value }) => typeof value === 'string',
      $rule: ipAddress
    },
    {
      // else
      $rule: alwaysError
    }
  ]
})

Clone this wiki locally