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

@buddh4/mapped-types

v1.2.1

Published

Mapped types helper classes for class-validator library

Downloads

7

Readme

Description

This library provides mapped types for creating dynamic model classes and class-validator validation schemas with the help of following classes:

  • PartialType: Sets all validation schema fields of a given type to @IsOptional
  • IntersectionType: Combines two classes and their validation schema
  • OmitType: Omits certain fields and validations from an existing class
  • PickType: Picks certain fields with validation schema from an existing class

This library is heavily based on the implementation of the @nestjs/swagger module but contains some modifications as:

  • Removed all nestjs dependencies and nestjs/swagger related metadata code
  • Use of real inheritance in PartialType and IntersectionType, as opposed to property only inheritance
  • Pass constructor arguments to inherited constructor

Note: In browser environments the inheritance of @Exclude and @Expose decorators for Pick-, Omit-, and the second class provided to IntersectionType does not work due to the following limitation of the class-transformer library issue.

Usage

The provided type helper classes can be used to create subclasses with altered validation schemas, without the need of redefining all properties of a class.

PartialType

With the PartialType helper, we can create a subclass which automatically adds an @IsOptional decorator to all properties of the base class.

import { IsString } from 'class-validator';
import { PartialType } from '@buddh4/mapped-types';

class CreateArticleDto {
  @IsString()
  title: string;

  @IsString()
  text;
}

class UpdateArticelDto extends PartialType(CreateDto) {}

Notes:

  • Only properties which do have a validation rule in the base class will be attached with an @IsOptional rule.
  • The created subclass will inherit all functions, this behavior differs from the original nestjs/swagger implementation.

OmitType

With the OmitType you can create a subclass which omits all selected fields and their validations.

import { IsString } from 'class-validator';
import { OmitType } from '@buddh4/mapped-types';

class UserDto {
  @IsString()
  username: string;

  @IsString()
  password;
}

class UserInfoDto extends OmitType(UserDto, ['password']) {}

Notes:

  • The OmitType does not inherit any class functions, only properties.

PickType

The PickType behaves similar to the OmitType but instead of excluding certain fields it only includes the selected fields.

import { IsString } from 'class-validator';
import { PickType } from '@buddh4/mapped-types';

class UserDto {
  @IsString()
  username: string;

  @IsString()
  password;
}

class UserInfoDto extends PickType(UserDto, ['username']) {}

Notes:

  • The PickType does not inherit any class functions, only properties.

IntersectionType

The IntersectionType can be used to merge the properties and their validation schema into a single subclass.

import { IsString } from 'class-validator';
import { IntersectionType } from '@buddh4/mapped-types';

class UserDto {
  @IsString()
  username: string;
}

class UserInfoDto {
  @Length(15)
  username: string;
}

class UserFormDto extends InterSectionType(UserDto, UserInfoDto) {}

Note:

  • The IntersectionType will only inherit functions and the constructor from the first provided class but not the second one. So only the fields and validation schema of the second class will be merged into the resulting class.
  • If one class of an IntersectionType sets an @Exclude on a field, the other class is not able to overwrite it with an @Expose.

Additional Notes

Class level Exclude and Expose are ignored

You need to manually set @Expose or @Exclude on the newly created types if required.