/schemaor

https://zhuanlan.zhihu.com/p/54859685

Primary LanguageJavaScript

Warning:

DO NOT USE THIS IN PRODUCTION, NOT FULLY TEST YET

Install

npm install schemaor

or

yarn add schemaor

Usage

  • Basic: Define schema without constraint:
import {Schema} from 'schemaor'
const Person = Schema({
  name: '',
  age: ''
})

// Instantiate:
const person = Person({
  name: 'Lee',
  age: 22
})
  • Field data type constraint:
import {Schema, Types} from 'schemaor'
const Person = Schema({
  name: Types().string(),
  age: Types().number()
})
  • Other kind of constraint: default(), required(), valueof()
import {Schema, Types} from 'schemaor'
const Person = Schema({
  name: Types().string().default('').required(),
  age: Types().number().default(22).required(),
  title: Types().valueof('coder', 'programmer')
})
  • Nested schema
import {Schema, Types} from 'schemaor'
const Person = Schema({
  job: Schema({
    title: '',
    company: ''
  })
})