Skip to content

Latest commit

 

History

History
99 lines (75 loc) · 1.95 KB

field-rules.md

File metadata and controls

99 lines (75 loc) · 1.95 KB

Field Rules

Field rules define basic validation for your fields. There are default validation rules that work out of the box and there are validation rules that you can define yourself.

Available Rules

name description
accept Should be true. Default message format: These terms need to be accepted
min Should have a minimum amount of characters. Default message format: Enter a minimum of ${value} characters
max Should have a maximum amount of charaters. Default message format: Enter a maximum of ${value} characters
phone Should be a valid phone number. Default message format: Enter a valid phone number
email Should be a valid emailaddress. Default message format: Enter a valid emailaddress

Accept

id: name
name: Name
type: text
rules:
  - type: min
    value: 5

Min

id: name
name: Name
type: text
rules:
  - type: min
    value: 5

Max

id: name
name: Name
type: text
rules:
  - type: max
    value: 75

Phone

id: name
name: Name
type: text
rules:
  - type: phone

Email

id: name
name: Name
type: text
rules:
  - type: email

Custom Rules

A custom rule can be added as followed:

You define add it to the rules list:

id: name
name: Name
type: text
rules:
  - type: minDrivingAge

You add a validator function to the hooks object.

const moment = require('moment')

const hooks = {
  'ruleValidator/minDrivingAge': {
    validate: (input) => moment(input).isSameOrBefore(
        moment().subtract(18, 'years')
    ),
    message: () => `The driver should be at least 18 years old.`
  }
}