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

@nest-lab/typeschema

v1.0.0

Published

[Typeschema][typeschema] is a general purpose validator that integrates with _several_ other validation packages. This library brings that integration into [NestJS][nest] as well! Just as Nest by default can integrate with class-validator, this package pr

Downloads

231

Readme

@nest-lab/typeschema

Typeschema is a general purpose validator that integrates with several other validation packages. This library brings that integration into NestJS as well! Just as Nest by default can integrate with class-validator, this package provides a ValidationPipe to bring a similar experience to @decs/typeschema.

Crafting DTOs

As all of these validation libraries typeschema integrates with use schemas instead of classes, we need to do a little work creating the DTO classes for Typescript's metadata reflection to enable us to have the same experience that already exists. Fortunately, this is pretty straightforward with how typeschema works! Simple extend the TypeschemaDto mixin by passing it your proper schema and viola! the DTO has been made.

import { TypeschemaDto } from '@nest-lab/typeschema';
import { z } from 'zod';

const helloWorldSchema = z.object({
  message: z.string(),
});

export class HelloWorldDto extends TypeschemaDto(helloWorldSchema) {}

What this does under the hood is creates a class that has a static schema property, along with a few others, that are then used by the ValidationPipe so that the incoming data can be properly validated.

Consuming the Data from the ValidationPipe

Something to make sure to take note of, this ValidationPipe does not return the exact same data as was passed in. In other words, unlike Nest's default ValidationPipe, this one does change the structure of the data after it has been parsed. It does this to provide a sane, type safe API. For every TypeschemaDto class, to access the data after it has been validated by the pipe, simply access the .data property.

@Controller()
export class AppController {
  @Post('ajv')
  ajvTest(@Body() body: AjvDto) {
    return body.data;
  }
}

Binding the ValidationPipe

Just like the existing validation pipe, you can bind this pipe using APP_PIPE or app.useGlobalPipes(new ValidationPipe()) for globally binding it, or simple @UsePipes(ValidationPipe) to bind it to a controller or method, or @Body(ValidationPipe) to bind it to a single parameter.

Options for the pipe

Currently the only option is an exceptionFactory that takes in an array of ValidationIssue from the @decs/typeschema package and returns an Error to be thrown. By default, this array is passed directly to BadRequestException from @nestjs/common.

If you want to pass the options you can via new ValidationPipe, or if you prefer to let Nest inject the options you can add them via the TypeschemaOptions injection token using a custom provider.

Integration with OpenAPI

I know this is going to be a big question, and eventually I want to have the integration automatically there. However, right now, because of the underlying libraries, there's no immediate introspection of each library, which means that we can't, in a type safe manner, create the OpenAPI spec for each DTO.

However, all is not lost, as it can still integrate with @nestjs/swagger so long as you add a static OPENAPI_METADATA property to the DTO. For the format of this, please check @nestjs/swagger's integration tests.