mobx-class-transformer-validator
TypeScript icon, indicating that this package has built-in type declarations

0.0.1 • Public • Published

mobx-class-transformer-validator

A simple plugin for class-transformer and class-validator which combines them in a nice and programmer-friendly API.

Installation

Module installation

npm install mobx-class-transformer-validator --save

(or the short way):

npm i -S mobx-class-transformer-validator

Peer dependencies

This package is only a simple plugin/wrapper, so you have to install the required modules too because it can't work without them. See detailed installation instruction for the modules installation:

Usage

The usage of this module is very simple.

import { IsEmail } from 'class-validator';
import { transformAndValidate } from 'mobx-class-transformer-validator';
 
class UserDto {
  @IsString()
  fullname: string;
}
 
class UserModel {
  @observable firstName: string;
  @observable lastName: string;
  @computed get fullname() {
    return `${this.firstName} ${this.lastName}`;
  }
}
 
const user = new UserModel();
 
// transform the JSON to class instance and validate it correctness
transformAndValidate(UserDto, UserModel)
  .then((userDto: UserDto) => {
    // now you can access all your class prototype method
    console.log(`Hello ${userDto.fullname}`); // prints "Hello World!" on console
  })
  .catch((err) => {
    // here you can handle error on transformation (invalid JSON)
    // or validation error (e.g. invalid email property)
    console.error(err);
  });

Package Sidebar

Install

npm i mobx-class-transformer-validator

Weekly Downloads

1

Version

0.0.1

License

MIT

Unpacked Size

6.67 kB

Total Files

10

Last publish

Collaborators

  • quarrant