🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Sign inDemoInstall
Socket

dto-classes

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

dto-classes

Classes for data transfer objects.

0.0.6
npm
Version published
Weekly downloads
16
14.29%
Maintainers
1
Weekly downloads
 
Created
Source

Introduction

DTO Classes is a TypeScript library for parsing, validating and serializing, designed primarily for data transfer objects in HTTP JSON APIs.

It gives you the following, a bundle I've found missing in the TypeScript/Node ecosystem:

  • Class-based schemas that parse/validate and serialize internal objects to JSON
  • Fields attached to schemas as class properties
  • Static types available by default without an additional infer call
  • Custom validation by adding class methods to a schema class
  • Constraints defined in a single options interface, not by chaining methods
  • Async by default to play nice with ORMs within schemas

Example:

import { Format } from "dto-classes/decorators";
import { DTObject } from "dto-classes/dt-object";
import { ArrayField } from "dto-classes/array-field";
import { StringField } from "dto-classes/string-field";
import { DateTimeField } from "dto-classes/date-time-field";


class UserDto extends DTObject {
    firstName = StringField.bind()
    lastName = StringField.bind()
    nickName = StringField.bind({ required: false })
    birthday = DateTimeField.bind()
    active = BooleanField.bind({ default: true })
    hobbies = ArrayField.bind({ items: StringField.bind() })
    favoriteColor = StringField.bind({ allowNull: true })
}

const userDto = await UserDto.parseNew({
    firstName: "Michael",
    lastName: "Scott",
    birthday: '1962-08-16',
    hobbies: ["Comedy", "Paper"],
    favoriteColor: "Red"
});

VS Code:

Alt Text

Installation

TypeScript 4.5+ is required.

Run npm install dto-classes.

Basic Usage

Parsing & Validating

Formatting

Objects

Field Types

Error Handling

Customization

Validation

Formatting

Fields

Less Common Scenarios

Recursive Objects

Standalone Fields

NestJS

FAQs

Package last updated on 31 May 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts