Giter VIP home page Giter VIP logo

typed-dto's Introduction

TypedDTO

Strong validated Data Transfer Objects for typescript.

NPM: typed-dto

Installation: npm install typed-dto

Example:

Schema:

./dto/article.dto.ts

import {BaseDTO, Schema, Property} from "typed-dto";

@Schema({strict: true})
class ArticleDTO extends BaseDTO
{
    @Property({ type: "string", regexp: /^[0-9a-zA-Z]{5,256}$/s })
    public title: string;
    
    @Property({ type: "string", min: 5, max: 256*256 })
    public content: string;
    
    @Property({ type: "date" })
    public publishedAt: Date;
}
NestJS Usage:

articles.controller.ts

import { Controller, Post, Body, HttpException } from '@nestjs/common';
import {ArticleDTO} from "./dto/article.dto";

@Controller("articles")
export class ArticlesController
{
    @Post("/create")
    create(@Body() body): string
    {
        const article = ArticleDTO.create(body);
        if(article) // check if not null
            return "OK";
        throw new HttpException("Invalid body.", 400);
    }
}
Express Usage:

app.ts

import * as express from "express";
import * as bodyParser from "body-parser";
import {ArticleDTO} from "./dto/article.dto";

const app = express();
app.use( bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true })); 

app.post('/articles/create', function(req, res)
{
    const article = ArticleDTO.create(req.body);
    
    if(article) // check if not null
    {
        res.writeHead(200);
        res.end("OK");
    }
    else
    {
        res.writeHead(400);
        res.end("Invalid body.");
    }
});

app.listen(3000);

typed-dto's People

Contributors

arteha avatar

Stargazers

 avatar

Watchers

 avatar

typed-dto's Issues

Improve Errors

  • Add "map" to ValidationException and use it to generate way to invalid property.
    Example: map: "property>list>0>foo>bar"
  • Add userfriendly error messages.
    Example: @Property({type: "string", error: e: ValidationException => "Sorry, but your value is invalid."})
  • Make MultipleValidationExceptions with public readonly list: Array<ValidationException> = []; and use it in ValidArray and Schema instead of single ValidationException .

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.