@tunnel-cast/common

Object-To-Model library, lets the user construct a strict model definition using ES6 class and Typescript Decorators api, the model class will wrap and describe the parsing, validation and transformation processes.

Usage no npm install needed!

<script type="module">
  import tunnelCastCommon from 'https://cdn.skypack.dev/@tunnel-cast/common';
</script>

README

Tunnel-Cast/Common

Object-To-Model library, lets the user construct a strict model definition using ES6 class and Typescript Decorators api, the model class will wrap and describe the parsing, validation and transformation processes.

casting process


Highlights

  • Embedding an input processing logic & validation in the model class definitions using decorators.
  • Supporting common types and your own Models as (nested) class attributes in your model class definition.
  • Providing an easy API to customize each stage in the process, parsing, validating and transforming.
  • Supporting Model class extending (eg. class AdminModel extends UserModel ...)

Install

npm install @tunnel-cast/common

Note:
This package required the peerDependencies :
@tunnel-cast/core


Test

  1. Clone the project repo.
  2. Move to project folder.
  3. Run npm i
  4. Run npm run test

See Documentation

  • The general flow of the cast process

  • Decorators

  • Models

  • Methods

  • Errors


Example

import { String, Boolean, Number, Model, Required, JsonParse } from '@tunnel-cast/common'
import { cast } from '@tunnel-cast/common/cast'

class User {
    @String({ required: false })
    username: string;

    @String()
    email: string;

    @Boolean({ required: false })
    notificationOn: number;
}

class ServerResponse {
    @Required(true)
    @Number({ 
        min: 3,
        parsing: [(val) => Number(val)]
    })
    apiVersion: number;

    @JsonParse
    @field.Array()
    imageTensor: Array<Array<number>>;

    @Model()
    user: User
}

const { value, errors } = cast(ServerResponse, { 
    apiVersion: '9', 
    imageTensor: "[[1,2],[2,3]]", 
    user: { email: 'user@examle.com' } 
}); 


console.log(JSON.stringify({ value, errors }, undefined, 2))
// output :
{
    "value": {
        "apiVersion": 9,
        "imageTensor": [
            [1, 2], [2, 3]
        ],
        "user": {
            "email": "user@examle.com"
        }
    }
}