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.
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.
npm install --save simple-object-validation
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,
})
// {}
import { isRequired, isGreaterThanOrEqual } from 'simple-object-validation'
import i18next from 'i18next'
const nameTransformer = name => i18next.t(name)
const i18n_required = isRequired({
messageCreator: (param, name, value) => i18next.t('{{name}} is required.', { name }), nameTransformer
})
const i18n_greaterThanOrEqual = isGreaterThanOrEqual({
messageCreator: (param, name, value) => i18next.t('{{name}} must be greater than or equal to {{param}}.', { name, param }), nameTransformer
})
export {
i18n_required as isRequired,
i18n_greaterThanOrEqual 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.'
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
- immutable.js support
- More examples
This library was heavily inspired by jfairbank/revalidate