1.1.0 • Published 3 years ago

@rexlabs-spicerhaart/validator v1.1.0

Weekly downloads
-
License
UNLICENSED
Repository
-
Last release
3 years ago

Validator

Validator is a light wrapper around the validatorjs laravel-style object validation library.

It exports a createValidationRules method that is designed to work with @rexlabs/form and the underlying Validator object to enable the addition of custom validation rules.

Usage

import Validator, { createValidationRules } from '@rexlabs/validator';

Validator.register(
  'rule_name',
  function(val) {
    // return truthy to pass validation
    return val < 5;
  },
  'Message to return if validation fails :attribute'
);

// returns a function that takes an object of values and returns a flat object of
// fields that did not pass validation with the first error encountered
const validatorFunction = createValidationRules({
  key: ['rule_name|another_rule_name', 'attribute name'],
  'nested.key': ['required', 'nested key']
});

// Calling this function returns an object with the shape
// {
//   key: 'Message to return if validation fails attribute name'
//   'nested.key': 'The nested key field is required.'
// }
validatorFunction({
  key: 10,
  nested: { key: '' }
});

Refer to the validatorjs documentation for more details about how to use the Validator object.

Development

Install dependencies

$ yarn

Available Commands

$ yarn test               # runs all units tests
$ yarn test:watch         # runs unit tests when files change
$ yarn build              # bundles the package for production

Legal

Copyright (c) 2018 Rex Software All Rights Reserved.

1.1.0

3 years ago