npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

typescript-decorator-validation

v3.0.3

Published

Typescript form validation using Decorators

Downloads

26

Readme

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

| Method | Parameters | Returns | Description | |---------------|------------|---------|-------------| |constructor |clazzClass<T>|ValidationHandler<T>|instantiates ValidationHandler class with the given decorated class to validate| | validationData | | ValidationData<T> | returns calculated validation data for given class through its metadata decorators | | hasErrors | state: Object | boolean | returns true if state object has errors | | getErrors | state: Object | ErrorData | returns object error state from the calculated validation metadata for the given state object | | validate | state: Object | StateValidationResult | returns object state validation result from the calculated validation metadata for the given object state | | buildInstance | state: Object | T | returns instantiated class T which is used to construct ValidationHandler<T> |

Goals and TODOs

  • [x] Implement strict type checking
  • [x] 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] React
    • [ ] 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