@lemoncode/fonk-match-field-validator
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

fonk-match-field-validator

CircleCI NPM Version bundle-size

This is a fonk microlibrary that brings validation capabilities to:

  • Validate if a field of a form matchs with another field in same form.

How to add it to an existing form validation schema:

We have the following form model:

const myFormValues = {
  login: 'user1',
  password: 1234,
  confirmPassword: 1234,
};

We can add a matchField validation to the myFormValues

import { matchField } from '@lemoncode/fonk-match-field-validator';

const validationSchema = {
  field: {
    confirmPassword: [
      {
        validator: matchField.validator,
        customArgs: { field: 'password' },
      },
    ],
  },
};

You can customize the error message displayed in two ways:

  • Globally, replace the default error message in all validationSchemas (e.g. porting to spanish):
import { matchField } from '@lemoncode/fonk-match-field-validator';

matchField.setErrorMessage('El campo debe coincidir con {{field}}');
  • Locally just override the error message for this validationSchema:
import { matchField } from '@lemoncode/fonk-match-field-validator';

const validationSchema = {
  field: {
    confirmPassword: [
      {
        validator: matchField.validator,
        customArgs: { field: 'password' },
        message: 'The field must match with {{field}}',
      },
    ],
  },
};
  • Even if you have a nested field:
const myFormValues = {
  user: {
    name: 'user1',
    password: 1234,
    confirmPassword: 1234,
  },
};
  • Overriding the message:
import { matchField } from '@lemoncode/fonk-match-field-validator';

const validationSchema = {
  field: {
    'user.confirmPassword': [
      {
        validator: matchField.validator,
        customArgs: { field: 'user.password' },
        message: 'The field must match with {{field}}',
      },
    ],
  },
};

Please, refer to fonk to know more.

License

MIT

About Basefactor + Lemoncode

We are an innovating team of Javascript experts, passionate about turning your ideas into robust products.

Basefactor, consultancy by Lemoncode provides consultancy and coaching services.

Lemoncode provides training services.

For the LATAM/Spanish audience we are running an Online Front End Master degree, more info: http://lemoncode.net/master-frontend

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 1.0.1
    78
    • latest

Version History

  • Version
    Downloads (Last 7 Days)
    • Published
  • 1.0.1
    78
  • 1.0.0
    0

Package Sidebar

Install

npm i @lemoncode/fonk-match-field-validator

Weekly Downloads

78

Version

1.0.1

License

MIT

Unpacked Size

15.3 kB

Total Files

8

Last publish

Collaborators

  • lemoncode