Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

objectypes

Package Overview
Dependencies
Maintainers
1
Versions
28
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

objectypes

A type-safe library to transform and validate objects

  • 1.3.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.1K
decreased by-20.14%
Maintainers
1
Weekly downloads
 
Created
Source

objectypes

A type-safe library to transform and validate objects.

Find more details about objectypes usage in the API Reference.

Installation

Make sure these compilerOptions flags are in the tsconfig.json file.

"emitDecoratorMetadata": true,
"experimentalDecorators": true

Run npm i --save objectypes to add it to your project.

Quick Start

Suppose you are requesting data from an API and you want to validate and have a type safe object from the JSON the API returns.

A JSON example:

{
  "userData": [
    {
      "name": "John  ",
      "age": 19,
      "likes": 2
    },
    {
      "name": "Maria",
      "age": 30,
      "likes": 0
    },
    {
      "name": "Peter",
      "age": 28,
      "posts": [
        {
          "title": "Comment here some lines from musics you like",
          "createdAt": "2021-10-14T20: 40: 07.609Z",
          "comments": ["Such a lonely day and it's mine"]
        }
      ],
      "likes": 100
    }
  ]
}

The target Typescript type:

import { BuildTransformation, Property } from 'objectypes'

class Post {
  @Property()
  title: string

  @Property()
  createdAt: Date

  @Property({ nullable: true })
  comments?: string[]
}

class PostPublisher {
  @Property({ nullable: true, type: Post })
  posts?: Post[]
}

class User extends PostPublisher {
  @BuildTransformation({ transform: (name: string) => name.trim() })
  @Property()
  name: string

  @Property()
  age: number

  @Property()
  likes: number
}

export class APIResponse {
  @Property({ name: 'userData', type: User })
  users: User[]
}

Keywords

FAQs

Package last updated on 14 May 2024

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