mobx-react-form icon indicating copy to clipboard operation
mobx-react-form copied to clipboard

Customizing SVK ajv error messages with ajv-errors

Open LaloHao opened this issue 7 years ago • 1 comments

Not really an issue, but i wanted to leave a note if anyone is interested in doing it

import MobxReactForm from 'mobx-react-form';
import svk from 'mobx-react-form/lib/validators/SVK';
import ajv from 'ajv';
import ajvErrors from 'ajv-errors';

const schema = {
  type: 'object',
  properties: {
    password: {
      type: 'string',
      minLength: 8,
      pattern: '(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[-+_!@#$%^&*.,?])',
    },
  },
  required: [
    'password',
  ],
  errorMessage: {
    properties: {
      password: 'debe contener al menos una mayuscula, minuscula, y un signo (?=.*[-+_!@#$%^&*.,?]).',
  }
};

const fields = {
  password: {
    label: 'Contraseña',
  },
};

const form = new MobxReactForm({ fields }, {
  plugins: {
    svk: svk({
      schema,
      package: ajv,
      extend: ({ validator }) => ajvErrors(validator),
    }),
  },
});

LaloHao avatar Feb 21 '19 17:02 LaloHao

Great, thank you!

foxhound87 avatar Feb 21 '19 21:02 foxhound87