项目作者: madcat23

项目描述 :
Functional JavaScript validation library
高级语言: JavaScript
项目地址: git://github.com/madcat23/simple-object-validation.git
创建时间: 2017-01-28T15:23:24Z
项目社区:https://github.com/madcat23/simple-object-validation

开源协议:MIT License

下载


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

  1. npm install --save simple-object-validation

Examples

Simple registration form

  1. import { assemble, chain, isRequired, isInteger, isGreaterThanOrEqual } from 'simple-object-validation'
  2. const isValidCustomer = assemble({
  3. name: isRequired('Name'),
  4. age: chain([
  5. isRequired,
  6. isInteger,
  7. isGreaterThanOrEqual(18)
  8. ])('Age'),
  9. })
  10. isValidCustomer({
  11. age: 17,
  12. })
  13. // { name: 'Name is required.', age: 'Age must be greater than or equal 18.' }
  14. isValidCustomer({
  15. name: 'Mathew',
  16. age: 18,
  17. })
  18. // {}

I18n example

  1. import { isRequired, isGreaterThanOrEqual } from 'simple-object-validation'
  2. import i18next from 'i18next'
  3. const nameTransformer = name => i18next.t(name)
  4. const i18n_required = isRequired({
  5. messageCreator: (param, name, value) => i18next.t('{{name}} is required.', { name }), nameTransformer
  6. })
  7. const i18n_greaterThanOrEqual = isGreaterThanOrEqual({
  8. messageCreator: (param, name, value) => i18next.t('{{name}} must be greater than or equal to {{param}}.', { name, param }), nameTransformer
  9. })
  10. export {
  11. i18n_required as isRequired,
  12. i18n_greaterThanOrEqual as isGreaterThanOrEqual
  13. }
  14. import { isRequired, isGreaterThanOrEqual } from './i18n-validation'
  15. isRequired('Zip code')('') // 'Feld Postleitzahl ist ein Pflichtfeld.'
  16. isGreaterThanOrEqual(18)('Age')('17') // 'Feld Alter muss größer oder gleich 18 sein.'

Integrate with Redux Form

  1. import { reduxForm } from 'redux-form'
  2. import isValidCustomer from './validators/is-valid-customer' // see code above
  3. class CustomerRegistrationComponent extends Component {
  4. /* ... contains redux form fields for customer, e. g. name, age and email */
  5. }
  6. export default reduxForm({
  7. form: 'customerRegistrationForm',
  8. validate: isValidCustomer,
  9. })(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