brunotot / react-decorate-form

πŸš€ Elegant form validation solution in React with TypeScript using Decorators πŸš€

Home Page:https://www.npmjs.com/package/react-decorate-form

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

πŸš€ TypeScript Decorator Validation - React Implementation πŸš€

⭐ Class entity validations made easy with the help of @Decorators

⭐ Allows strict type checking when applying decorator validators

⭐ Works perfectly with existing TypeScript-first applications

Downloads per month NPM Version Contributors Maintained Awesome badge

Table of Contents

Installation

  1. Install library dependency
npm install react-decorate-form
  1. Allow experimental decorators configuration in your tsconfig.json.
    This removes IDE errors which could pop-up
{
  "compilerOptions": {
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    /* ... */
  }
}
  1. Add babel configuration to your tsconfig.json.
    This allows for type-safety checking
{
  plugins: [
    "babel-plugin-transform-typescript-metadata",
    ["@babel/plugin-proposal-decorators", { legacy: true }],
    ["@babel/plugin-proposal-class-properties", { loose: true }],
  ],
  presets: ["@babel/preset-typescript"],
}

Contribute

  1. Open bash terminal
  2. Change directory to your desired position
  3. Prepare the environment using this command
bash <(curl -s https://raw.githubusercontent.com/brunotot/react-decorate-form/master/contribute/setup.sh)

Expanded shell code:

#!/bin/bash
mkdir typescript-decorator-validation-dev
cd typescript-decorator-validation-dev
git clone https://github.com/brunotot/react-decorate-form.git
cd react-decorate-form
npm install
cd ../
git clone https://github.com/brunotot/react-decorate-form.git demo
cd demo
git checkout testing
npm install
cd ../
if command -v code; then
    code .
fi
  1. Commit and push changes to a local branch
  2. Open pull request

Documentation

See extended documentation on typescript-decorator-validation

Examples

A basic TypeScript form can look something like

import { validators } from 'react-decorate-form';

export type UserFormFields = {
  confirmPassword: string;
  firstName: string;
  lastName: string;
  password: string;
  url: string;
  age: number;
};

export default class UserForm implements UserFormFields {
  @validators.string.Size({ min: 5 })
  @validators.string.NotEmpty()
  firstName!: string;

  @validators.string.NotEmpty()
  lastName!: string;

  @validators.string.NotEmpty()
  @validators.string.Password()
  password!: string;

  confirmPassword!: string;

  @validators.string.URL()
  url!: string;

  @validators.number.Range({ min: 18, max: 100 })
  age!: number;

  @validators.boolean.AssertTrue('Passwords must match')
  get passwordsMatch(): boolean {
    return this.password === this.confirmPassword;
  }
}

And with some styling we can display the form which can look something like:

example form

About

πŸš€ Elegant form validation solution in React with TypeScript using Decorators πŸš€

https://www.npmjs.com/package/react-decorate-form

License:MIT License


Languages

Language:TypeScript 90.5%Language:Shell 9.5%