1.0.21 • Published 8 months ago

@elieandraos/clockwork v1.0.21

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

GitHub Workflow Status npm bundle size (scoped) Coveralls CodeFactor npms.io (quality) downloads

  • :technologist: Minimal model-based javascript validation library
  • :bricks: Can be used with any javascript framework
  • :green_heart: Offers 30+ built-in validation rules
  • :children_crossing: Easy to use api, inspired from the Laravel validation syntax
  • :test_tube: High test coverage

Installation

npm install @elieandraos/clockwork --save

Usage

import Clockwork from '@elieandraos/clockwork'
const validator = new Clockwork()

Basic validation

Clockwork validates a given data object against a given rules object

// define the data and rules objects
validator
   .setData({
      name: '',
      email: ''
   })
   .setRules({
        name: 'required',
        email: 'required'
    })

if ( validator.passes() ) {
    // ... do something when input data are valid
}

// alternately, you could do the inverse
if (validator.fails() ) {
    // ... do something when input data are not valid
}

Complex model validation

  • "dot annotations" for nested object properties validation
  • multiple rules are separated with a pipe
  • rules that accept an additional paramter are suffixed with a semi column followed by the parameter
  • rule paramter can be static or from the data object itself
validator
   .setData({
      person: {
         name: null,
         age: null,
         email: null
      },
      domain: 'leadwithprimitive.com'
   })
    .setRules({
        'person.name': 'required | alpha',
        'person.age': 'required | integer | min:18',
        'person.email': 'required | email | ends_with:domain' // here domain will be evaluated as 'leadwithprimitive.com'
    })

Accessing the error bag

Clockwork provides 4 helper methods to access any validation error:

validator
   .setData({ name: null, age: 10 })
   .setRules({ name: 'required', age: 'min:12' })

if(validator.fails()) {
    validator.hasErrors() // checks if there is any error
    validator.hasErrors('name') // checks if there is any error for the 'name' field
    
    validator.getErrors() // returns all the error messages
    validator.getErrors('name') // returns all the error messages of the 'name' field

    validator.getFirstError() // returns the first error message found
    validator.getFirstError('name') // returns the first error message found of the 'name' field
    
    validator.getErrorBag() // returns the error bag object as it is
}

Custom error messages

Custom error message can be defined with setCustomErrorMesssages() method. This method accepts an object of key/value pairs:

  • The key is the data property concatenated with the rule name
  • The value is the custom message
validator
   .setData({ name: null })
   .setRules({ name: 'required' })
   .setCustomErrorMessages({ 'name.required' : 'You must enter your name' })

For rules with parameters, add {param} to into the error message and it will be parsed.

validator
   .setData({ age: null })
   .setRules({ age: 'min:18' })
   .setCustomErrorMessages({ 'age.min' : 'You must be at least {param} years old' }) // returns: You must be at least 18 years old

Custom rules

Custom rules can be created with the extend() method. This method accepts three parameters:

  • The first, is the name of the rule name
  • The second, is the closure (should return a boolean) that should be executed when calling the rule
  • The third (optional), is the rule's error message (default: 'Invalid')
validator
   .setData({ age: null })
   .setRules({ age: 'greater_than:18' })
   .extend( 'greated_than', (value, arg) => {
      return value > arg
   }, 'Age must be greater than {param}')

Built-in rules

RulesDescription
after:dateThe field under validation must be a value after a given date
after_or_equal:dateThe field under validation must be a value after or equal a given date
alphaThe field under validation must be entirely alphabetic characters
alpha_dashThe field under validation may have alpha characters, as well as dashes and underscores
alpha_numericThe field under validation may have alpha-numeric characters
arrayThe field under validation must be an array
before:dateThe field under validation must be a value before a given date
before_or_equal:dateThe field under validation must be a value before or equal a given date
booleanThe field under validation must be able to be cast as a boolean
dateThe field under validation must be a valid javascript date
date_format:stringThe field under validation must match the given format
different:valueThe field under validation must not match the given value
emailThe field under validation must be formatted as an email address
ends_with:stringThe field under validation must end with the given value
is_in:valueThe field under validation must be included in the given value. Accepted values are comma seperated string or array
integerThe field under validation must be an integer
jsonThe field under validation must be a valid JSON object
leap_yearThe field under validation must be a leap year date
max:valueThe field under validation must be less than or equal to a maximum value. Accepted values are string, numerics and array
matches_regex:patternThe field under validation must not match the given regular expression
multiple_of:numberThe field under validation must a be multiple of the given number
min:valueThe field under validation must be greater than or equal to a minimum value. Accepted values are string, numerics and array
not_in:valueThe field under validation must not be included in the given value. Accepted values are comma seperated string or array
numbericThe field under validation must be a numeric (integer or decimal)
requiredThe field under validation must be present in the input data and not empty
same:valueThe field under validation must match the given value
size:valueThe field under validation must have a length matching the given value. Accepted value are string and array
sometimesThe field will only ve validated if present
starts_with:stringThe field under validation must end with the given value
stringThe field under validation must be a string
todayThe field under validation must be a date equal to today
tomorrowThe field under validation must be a date equal to tomorrow
urlThe field under validation must be a valid url
uuidThe field under validation must be a valid RFC 4122 (version 1, 3, 4, or 5) universally unique identifier (UUID)
yesterdayThe field under validation must be a date equal to yesterday

Frameworks wrapper

1.0.21

8 months ago

1.0.20

8 months ago

1.0.19

2 years ago

1.0.18

2 years ago

1.0.17

2 years ago

1.0.16

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.11

2 years ago

1.0.10

2 years ago

1.0.15

2 years ago

1.0.14

2 years ago

1.0.13

2 years ago

1.0.12

2 years ago

1.0.0

2 years ago

1.1.0

3 years ago

1.0.1

3 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago