1.3.0 • Published 3 years ago

objectypes v1.3.0

Weekly downloads
636
License
MIT
Repository
github
Last release
3 years ago

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[]
}
1.3.0

3 years ago

1.2.3

4 years ago

1.2.2

4 years ago

1.2.0

4 years ago

1.2.1

4 years ago

1.1.8

4 years ago

1.1.7

4 years ago

1.1.6

4 years ago

1.1.5

4 years ago

1.1.4

4 years ago

1.1.3

4 years ago

1.1.2

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.0

4 years ago

0.2.2

4 years ago

0.2.1

4 years ago

0.2.0

4 years ago

0.1.2

4 years ago

0.1.0

4 years ago

0.1.1

4 years ago

0.0.5

4 years ago

0.0.6

4 years ago

0.0.4

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago