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

sw-class-schema

v1.1.12

Published

Useful helper to construct and deconstruct objects over http requests. This module re-exports validators from [Swanest' class-validator fork](https://github.com/swanest/class-validator) and sanitzers from [class-sanitizer](https://github.com/pleerock/clas

Downloads

20

Readme

sw-class-schema

Useful helper to construct and deconstruct objects over http requests. This module re-exports validators from Swanest' class-validator fork and sanitzers from class-sanitizer

List of usable types

Check out class-validator doc.

Install

npm i sw-class-schema --save

Use-case

Suppose you have an User that is posting some Posts


import {
    Schema,
    Min,
    Max,
    Contains,
    ValidateNested,
    IsDefined,
    IsDate,
    Length,
    ValidateIf,
    IsEmail,
    IsFQDN, Strict, IsDatable, ToDate
} from 'sw-class-schema';


class User extends Schema { 
            
    @Strict(false) //Additional parameters in JSON object won't throw an error
    
    @Min(12) @Max(12)
    age: number;
    
    @Contains("patrick")
    name: string;
    
    @ValidateIf(obj => obj.email != void 0) @IsEmail() //This is an optional field
    email: string;

    constructor() {
        super("age", "name", "email"); //Here all properties that need to be constructed/deconstructed
    }
            
}


class Post extends Schema {

    @Strict(true) //Strict mode
    
    @IsDefined() @ValidateNested() //Never forget to put @IsDefined() if you use @ValidateNested()
    user: User;
    
    @Length(5, 20)
    title: string;
    
    @Contains("hello") @Length(10, 200)
    text: string;
    
    @IsDatable() //This means either an ISO string date or a Date object
    @ToDate() //When an object is stringified, Date objects become ISO strings, so to reconstruct, we use ToDate formatter 
    date: Date;
    
    @IsDefined() @ValidateNested({each: true}) // Special case for arrays, see super as well
    users: Array<User>;
    
    constructor() { //Never forget it !
        super({user: User}, "title", "text", "date", {user: [User]});
    }
    
 }

toSchema()

This sync method returns an object and recursively schematize inner objects.

let post = new Post();
post.title = "welcome";
post.user = new User();
post.toSchema();

fromSchema()

This async static method returns a promise of instance.


let post = await Post.fromSchema<Post>({title:"hello"}); //This will throw an error