GithubHelp home page GithubHelp logo

ikatun / formik-class-validator Goto Github PK

View Code? Open in Web Editor NEW
8.0 1.0 1.0 12 KB

Use class validator's decorators syntax to write formik's validation schema

License: Apache License 2.0

TypeScript 100.00%

formik-class-validator's Introduction

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;
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.