Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Socket
Sign inDemoInstall

formik-class-validator

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

formik-class-validator

Integrate class-validator with formik for easier javascript and typescript form validation


Version published
Maintainers
1
Created
Source

Installation

npm install formik-class-validator

Use decorators-style schema to validate formik forms using class-validator library.

The library re-exports everything from class-validator with the exception of ValidateNested which is modified to take a nested type.

formik-class-validator also exports class FormikValidatorBase which defines createValidator static method used to convert a class-validator class to formik validator.

Usage example, schema definition:

import {
  IsNotEmpty,
  IsString,
  ValidateNested,
  IsDateString,
  FormikValidatorBase,
  ValidateWith
} from 'formik-class-validator';
import { size } from 'lodash';

export class ProductFormModel extends FormikValidatorBase {
  @IsNotEmpty()
  @IsString({ message: 'Food type name is required' })
  name: string = '';

  @IsNotEmpty()
  @IsString({ message: 'Food category is required' })
  @ValidateWith(args => size(args.value) > 2, { message: 'Category must be longer than 2' })
  category: string = '';
  
  @IsNotEmpty()
  @ValidateWith(args => size(args.value) > size(args.object.name), { message: 'Description must be longer than name' })
  description: string = '';
}

Usage example, integration with formik:

import React from 'react';
import { FormikProps, Formik, Field } from 'formik';
import { ProductFormModel } from './product-form-model';

export class ProductForm extends React.Component {
  formik: FormikProps;

  handleSubmit = (values) => {
    console.log('submitting', values);
  }

  render() {
    return (
      <Formik initialValues={new ProductFormModel()} onSubmit={this.handleSubmit} validate={ProductFormModel.createValidator()}>
        {(formik: FormikProps) => {
          console.log('errors', formik.errors);
          return (
            <div>
              <div>
                name <Field name="name" />
              </div>
              <div>
                description <Field name="description" />
              </div>
              <div>
                category <Field name="category" />
              </div>
              <input type="submit" onClick={formik.handleSubmit} />
            </div>
          );
        }
      </Formik>
    )
  }

Usage example, nested schema definition:

import {
  IsNotEmpty,
  IsString,
  ValidateNested,
  IsDateString,
  FormikValidatorBase,
} from 'formik-class-validator';

class AdditionalInfoFormModel {
  @IsNotEmpty()
  @IsString({ message: 'Food type is required' })
  foodType: string = '';

  @IsNotEmpty()
  @IsString({ message: 'Units are required' })
  units: string;

  @IsNotEmpty()
  @IsDateString({ message: 'Expiration date is required' })
  expirationDate: string;
}

export class ProductFormModel extends FormikValidatorBase {
  @ValidateNested(() => AdditionalInfoFormModel)
  additionalInfo = new AdditionalInfoFormModel();

  @IsNotEmpty()
  @IsString({ message: 'Food type name is required' })
  name: string;

  @IsNotEmpty()
  @IsString({ message: 'Food description is required' })
  description: string;

  @IsNotEmpty()
  @IsString({ message: 'Food category is required' })
  category: string;
}

FAQs

Package last updated on 06 Mar 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc