simple-object-validation

Library for JavaScript Object validation

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
simple-object-validation
1.1.27 years ago7 years agoMinified + gzip package size for simple-object-validation in KB

Readme

simple-object-validation
simple object validation
simple-object-validation is a lightweight validation library that enables a functional way of validating javascript objects. It is made to work well with Redux Form's validation API.

Code over configuration

The idea is to use only functions and no constraint configuration or something like that. Typically in a more sophisticated web application user input validation can easily become very complex and specific. So there will be the need of writing custom validation code. You should always be able to easily plug your custom code into the standard library code without having to configure and register stuff intransparently somewhere else.

API Docs

Installation

``` npm install --save simple-object-validation ```
Examples

Simple registration form

```javascript import { assemble, chain, isRequired, isInteger, isGreaterThanOrEqual } from 'simple-object-validation' const isValidCustomer = assemble({ name: isRequired('Name'), age: chain(
isRequired,
isInteger,
isGreaterThanOrEqual(18)
)('Age'), }) isValidCustomer({ age: 17, }) // { name: 'Name is required.', age: 'Age must be greater than or equal 18.' } isValidCustomer({ name: 'Mathew', age: 18, }) // {} ```

I18n example

```javascript import { isRequired, isGreaterThanOrEqual } from 'simple-object-validation' import i18next from 'i18next' const nameTransformer = name => i18next.t(name) const i18nrequired = isRequired({ messageCreator: (param, name, value) => i18next.t('{{name}} is required.', { name }), nameTransformer }) const i18ngreaterThanOrEqual = isGreaterThanOrEqual({ messageCreator: (param, name, value) => i18next.t('{{name}} must be greater than or equal to {{param}}.', { name, param }), nameTransformer }) export { i18nrequired as isRequired, i18ngreaterThanOrEqual as isGreaterThanOrEqual } import { isRequired, isGreaterThanOrEqual } from './i18n-validation' isRequired('Zip code')('') // 'Feld Postleitzahl ist ein Pflichtfeld.' isGreaterThanOrEqual(18)('Age')('17') // 'Feld Alter muss größer oder gleich 18 sein.' ```

Integrate with Redux Form

```javascript import { reduxForm } from 'redux-form' import isValidCustomer from './validators/is-valid-customer' // see code above class CustomerRegistrationComponent extends Component { / ... contains redux form fields for customer, e. g. name, age and email / } export default reduxForm({ form: 'customerRegistrationForm', validate: isValidCustomer, })(CustomerRegistrationComponent) ``` See detailed information on how to use Redux Form's validation API at http://redux-form.com > Examples

Coming soon

  • immutable.js support
  • More examples

Thanks

This library was heavily inspired by jfairbank/revalidate