Learn about our RFC process, Open RFC meetings & more.Join in the discussion! »

@lemoncode/fonk-previous-date-validator

1.2.0 • Public • Published

fonk-previous-date-validator

CircleCI NPM Version bundle-size

This is a fonk microlibrary that brings validation capabilities to:

  • Validate if a field of a form is previous to a certain date.

How to install it:

npm install @lemoncode/fonk-previous-date-validator --save

How to add it to an existing form validation schema:

We have the following form model:

const myFormValues = {
  product: 'shoes',
  purchaseDate: new Date('2019-02-10'),
}

The validator must be configured with the following required arguments:

export interface CustomArgs {
  dateDate;
  parseStringToDateFn?: (valuestring) => Date;
  inclusive?: boolean;
}

These are the default arguments:

let defaultCustomArgs: CustomArgs = {
  date: null,
  parseStringToDateFn: null,
  inclusive: false,
};

We can add a previousDate validation to the myFormValues

import { previousDate } from '@lemoncode/fonk-previous-date-validator';
 
const validationSchema = {
  field: {
    purchaseDate: [
      {
        validator: previousDate.validator,
        customArgs: { date: new Date('2019-03-10') },
      },
    ],
  },
};

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 { previousDate } from '@lemoncode/fonk-previous-date-validator';
 
previousDate.setErrorMessage('El campo debe de ser numérico');
  • Locally just override the error message for this validationSchema:
import { previousDate } from '@lemoncode/fonk-previous-date-validator';
 
const validationSchema = {
  field: {
    purchaseDate: [
      {
        validator: previousDate.validator,
        message: 'Error message only updated for the validation schema',
        customArgs: { date: new Date('2019-03-10') },
      },
    ],
  },
};

This validator compare Date values. If your model use dates as string format, you can provide the parseStringToDateFn method.

import { previousDate } from '@lemoncode/fonk-previous-date-validator';
 
const validationSchema = {
  field: {
    purchaseDate: [
      {
        validator: previousDate.validator,
        customArgs: {
          date: new Date('2019-03-10T00:00:00'),
          parseStringToDateFn: value => new Date(value),
        },
      },
    ],
  },
};

Or if you are using some third party library like moment, date-fns, etc:

import { previousDate } from '@lemoncode/fonk-previous-date-validator';
+ import parse from 'date-fns/parse'
 
const validationSchema = {
  field: {
    purchaseDate: [
      {
        validator: previousDate.validator,
        customArgs: {
          date: new Date('2019-03-10T00:00:00'),
-         parseStringToDateFn: value => new Date(value),
+         parseStringToDateFn: value => parse(value, 'yyyy-MM-dd HH:mm:ss', new Date()),
        },
      },
    ],
  },
};

You can specify the custom arguments in two ways:

  • Locally just customize the arguments for this validationSchema:
import { previousDate } from '@lemoncode/fonk-previous-date-validator';
 
const validationSchema = {
  field: {
    purchaseDate: [
      {
        validator: previousDate.validator,
        customArgs: {
          date: new Date('2019-03-10'),
          parseStringToDateFn: value => new Date(value),
        },
      },
    ],
  },
};
  • Globally, replace the default custom arguments in all validationSchemas (e.g. enable strict types):
import { previousDate } from '@lemoncode/fonk-previous-date-validator';
 
previousDate.setCustomArgs({ parseStringToDateFn: (value) => new Date(value) ) });
 
// OR
 
previousDate.setCustomArgs({ date: new Date() });
 
// OR
 
previousDate.setCustomArgs({ date: new Date(), parseStringToDateFn: (value) => new Date(value) ) });
 

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

Install

npm i @lemoncode/fonk-previous-date-validator

DownloadsWeekly Downloads

22

Version

1.2.0

License

MIT

Unpacked Size

18.5 kB

Total Files

8

Last publish

Collaborators

  • avatar