New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

sw-class-schema

Package Overview
Dependencies
Maintainers
2
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sw-class-schema

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

  • 1.1.12
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
2
Weekly downloads
 
Created
Source

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

FAQs

Package last updated on 30 Jan 2019

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc