@tunnel-cast/common
v1.0.5
Published
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.
Downloads
8
Maintainers
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.
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
- Clone the project repo.
- Move to project folder.
- Run
npm i
- Run
npm run test
See Documentation
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: '[email protected]' }
});
console.log(JSON.stringify({ value, errors }, undefined, 2))
// output :
{
"value": {
"apiVersion": 9,
"imageTensor": [
[1, 2], [2, 3]
],
"user": {
"email": "[email protected]"
}
}
}