luren-schema
TypeScript icon, indicating that this package has built-in type declarations

0.2.8 • Public • Published

luren-schema

npm version Dependencies Status Build Status Coverage Status

Luren-Schema is a typescript module for Luren, it uses metadata to store type info and validate data.It use decorators to inject metadata, so decorator must be enabled .

Built-in data types:

string, boolean, number, integer, date, array,object

@Schema({title: 'person', additionalProperties: true})
class Person {
      @Prop({ required: true, type: 'string' })
      public name: string
      @Prop({ required: true })
      public password: string
      constructor(name: string, passowrd: string) {
        this.name = name
        this.passowrd = passowrd
      }
      public greeting() {
        console.log(`Hi, I'm ${this.name}`)
      }
    }
const person = new Person('My Name', 'my_passoword')
const schema = getJsSchema(Person)
 
// validation
//result: [true, undefined]
const [valid, errorMsg] = JsTypes.validate(person, schema)
//serialize to json
// result: {name: 'My Name'}
const data = JsTypes.serialize(person, schema)
//deserialize from json
//result: Person {name: 'John', password: 'encrypted_passwd'}
const p = JsTypes.deserialize({name: 'John', password: 'encrypted_passwd'}, schema)
// result: Hi, I'm John
p.greeting()

Readme

Keywords

none

Package Sidebar

Install

npm i luren-schema

Weekly Downloads

3

Version

0.2.8

License

MIT

Unpacked Size

135 kB

Total Files

56

Last publish

Collaborators

  • vincent.wen