3.0.3 • Published 10 months ago

typescript-decorator-validation v3.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

Table of Contents

Installation

  1. Install library dependency
npm install typescript-decorator-validation
  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. Clone the repository main branch
git clone https://github.com/brunotot/typescript-decorator-validation.git
  1. Checkout a new branch
git checkout -b "[issue-number]-[issue-description]"
  1. Commit and push changes
  2. Open pull request

Documentation

ValidationHandler

MethodParametersReturnsDescription
constructorclazz: Class\ValidationHandler\instantiates ValidationHandler class with the given decorated class to validate
validationDataValidationData\returns calculated validation data for given class through its metadata decorators
hasErrorsstate: Objectbooleanreturns true if state object has errors
getErrorsstate: ObjectErrorDatareturns object error state from the calculated validation metadata for the given state object
validatestate: ObjectStateValidationResultreturns object state validation result from the calculated validation metadata for the given object state
buildInstancestate: ObjectTreturns instantiated class T which is used to construct ValidationHandler<T>

Goals and TODOs

  • Implement strict type checking
  • Implement predefined decorator validators
  • Write documentation
  • Implement the logic so the library can be used easily in CI tests
  • Implement tests for predefined decorator validators
  • Write implementation libraries for popular front-end frameworks - x(https://github.com/brunotot/react-decorate-form) - Angular - Svelte - Vue - Solid

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

2.0.2

11 months ago

2.0.5

11 months ago

2.0.4

11 months ago

2.0.7

11 months ago

2.0.6

11 months ago

2.0.9

11 months ago

2.0.8

11 months ago

2.0.1

11 months ago

3.0.3

10 months ago

1.0.29

11 months ago

3.0.2

11 months ago

3.0.1

11 months ago

3.0.0

11 months ago

1.1.9

11 months ago

1.1.8

11 months ago

1.1.6

11 months ago

1.1.5

11 months ago

1.1.3

11 months ago

1.1.2

11 months ago

2.0.15

11 months ago

2.0.16

11 months ago

2.0.13

11 months ago

2.0.14

11 months ago

2.0.11

11 months ago

2.0.12

11 months ago

2.0.10

11 months ago

2.0.17

11 months ago

2.0.18

11 months ago

1.0.28

1 year ago

1.0.27

1 year ago

1.0.26

1 year ago

1.0.25

1 year ago

1.0.24

1 year ago

1.0.23

1 year ago

1.0.22

1 year ago

1.0.20

1 year ago

1.0.18

1 year ago

1.0.16

1 year ago

1.0.15

1 year ago

1.0.14

1 year ago

1.0.13

1 year ago

1.0.12

1 year ago

1.0.11

1 year ago

1.0.10

1 year ago

1.0.9

1 year ago

1.0.8

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago