Skip to content

gitjs/redux-form-with-ajv

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

83 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Travis npm

redux-form-with-ajv

This package combines redux-form with json-schema. It transforms the generated ajv errors to a way that redux-form can work with. As validation library it uses ajv.

The advanced usage give you more control about ajv itself, so you can pass your own instance of ajv maybe with plugins like ajv-errors, ajv-keywords, ajv-i18n (must be passed by localize option) and so on, as well you have full control about the error via a callback.

Installation

npm install --save redux-form-with-ajv

Basic usage

import validate from 'redux-form-with-ajv';

export default reduxForm({
  form: 'yourForm',
  validate: validate(yourJsonSchema)
})(YourForm);

Advanced usage: options

Use your custom ajv instance via ajv option

You can use ajv option when you need full control of ajv. It is only mandatory to pass two options, allErrors and verbose.

import Ajv from 'ajv';
import validate from 'redux-form-with-ajv';

// Be sure to set the mandatory options!
const ajv = new Ajv({
  allErrors: true,
  verbose: true
});

export default reduxForm({
  form: 'yourForm',
  validate: validate(yourJsonSchema, { ajv })
})(YourForm);

Localize error message via localize option with ajv-i18n

You can use localize option to pass the translations from ajv-i18n.

import localize from 'ajv-i18n';
import validate from 'redux-form-with-ajv';

export default reduxForm({
  form: 'yourForm',
  validate: validate(yourJsonSchema, { localize: localize.sv })
})(YourForm);

Customize error message via errorMessage option

You can use errorMessage option to pass a function that will receive an ajv error object as argument and should return error message.

import validate from 'redux-form-with-ajv';

const errorMessage = error => {
  if (error.keyword === 'required') {
    return 'is required';
  }

  return error.message;
};

export default reduxForm({
  form: 'yourForm',
  validate: validate(yourJsonSchema, { errorMessage })
})(YourForm);

View Sandbox Samples

Simple validation with few Field(s)

Dynamic validation with FieldArrays and custom error messages

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •