Skip to content

hmmhmmhm/typescript-json

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Typescript JSON

📦 Initialize JSON data according to type schema

Have you ever had trouble resetting the values you received to the desired form of value when you received any data from the web or server?

The Typescript JSON module helps initialize the JSON data to the desired Schema. It also supports Intellisense and Automatic Completion based on Typescript. (This module is a module that binds scotthovestadt/schema-object to a Typescript.)

Install

npm install typescript-json

Usage

import {
    TypescriptJSON,
    Types
} from 'typescript-json'

// Defines the data schema.
let schema = {

    // If non-compliant data is entered,
    // the value will be replaced with undefined
    id: Types.String({
        default: 'accountId',
        minLength: 6
    }),

    // Or just can simply specify a format.
    pw: Types.String({})
}

// Contains the data to be entered.
let input: any = {}

// Input value of modified
// to schema specification
let data = TypescriptJSON
    <typeof schema>
    (schema, input)

console.log(data.id) // 'accountId'
console.log(data.pw) // undefined


// The value does not change because the
// minimum length specification does not comply.
data.id = 'wrong' // need to be more than 6 length.
console.log(data.id) // still 'accountId'

Example

It can define the default value of the id value, define the maximum length, and so on.

Once the data has been initialized, all components of the data can be accessed accurately through Intelligence and Auto Completion.

License

MIT Licensed.

About

📦 Initialize JSON data according to type schema

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published