Skip to content

validate string by regexp or function, return error message if validation fails.

Notifications You must be signed in to change notification settings

qizf7/validate-str

Repository files navigation

validate string

validate string by regexp or function, return error message list.

usage

  validate([{
    name: 'id1',
    value: '123456789012345',
    validators: [
      {
        msg: 'please input',
        method: 'require'
      },
      {
        msg: 'identity error',
        method: 'identity'
      }
    ]
  },
  {
    name: 'id2',
    value: '123456789012345',
    validators: [
      {
        msg: 'please input',
        method: 'require'
      },
      {
        msg: 'identity error',
        method: 'identity'
      }
    ]
  }], {mode: 'single'})

params

validate(vlEntries, options)

  • vlEntries An array of validation
  • options options

vlEntries

see above example

vlEntries[].name

validation name, used by result list

vlEntries[].value

string to be validated

vlEntries[].validators

validator list

vlEntries[].validators[].msg

the message to be returned when validation fail

vlEntries[].validators[].method

the validate method, can be function, regexp, string

internal support method

supported regexp inner, you can use these regexp names as method directly.

options

options.mode

single

only return the first error object when validation fails.

  {
    name: 'id1',
    errors: ['errMsg', 'errMsg']
  }

multiple

return error object list.

  [
    {
      name: 'id1',
      errors: ['errMsg', 'errMsg']
    }, {
      name: 'id2',
      errors: ['errMsg', 'errMsg']
    }
  ]

About

validate string by regexp or function, return error message if validation fails.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published