0.0.1 • Published 3 years ago
sddd-metatype v0.0.1
Create objects
const { defineType } = require('metatype')
// Type description
const schema = { name: 'string', age: 'number' }
// Define type
const factory = defineType('Human', schema)
// Create object
const human = factory.create({ name: 'John', age: 23 })
// Will throw
// const human = factory.create({ name: 'John', age: '23' })
Validate objects
const { defineType } = require('metatype')
// Type description
const schema = { name: 'string', age: 'number' }
// Define type
const factory = defineType('Human', schema)
// Validate struct (will return true)
factory.validate({ name: 'John', age: 23 })
// Will throw (because age is a string)
// factory.validate({ name: 'John', age: '23' })
Type reference
const { defineType } = require('metatype')
// Define types
const animalFactory = defineType('Animal', { name: 'string', age: 'number' })
const humanFactory = defineType('Human', { name: 'string', age: 'number', pet: 'Animal' })
// Create objects
const pet = animalFactory.create({ name: 'Lex', age: 3 })
const human = humanFactory.create({ name: 'John', age: 23, pet })
// Or
{
const human = humanFactory.create({ name: 'John', age: 23, pet: { name: 'Lex', age: 3 }})
}
// Validate (will return true)
humanFactory.validate({ name: 'John', age: 23, pet: { name: 'Lex', age: 3 }})
// Will throw (because age is a string)
// humanFactory.validate({ name: 'John', age: 23, pet: { name: 'Lex', age: '3' }})
Native objects reference
const { defineType } = require('metatype')
// Define type
const factory = defineType('MyType', { id: 'number', items: Array })
// Create object
const obj = factory.create({ id: 1, items: [1, 2, 3]})
0.0.1
3 years ago