Structure
Search…
Introduction

A simple schema/attributes library built on top of modern JavaScript

Structure provides a simple interface which allows you to add attributes to your ES6 classes based on a schema, with validations and type coercion.

Use cases

You can use Structure for a lot of different cases, including:
  • Domain entities and value objects
  • Model business rules
  • Validation and coercion of request data
  • Map pure objects and JSON to your application classes
  • Add attributes to classes that you can't change the class hierarchy
What Structure is not:
  • It's not a database abstraction
  • It's not a Model of a MVC framework
  • It's not an attempt to simulate classic inheritance in JavaScript

Example usage

For each attribute on your schema, a getter and a setter will be created into the given class. It'll also auto-assign those attributes passed to the constructor.
1
const { attributes } = require('structure');
2
3
const User = attributes({
4
name: String,
5
age: {
6
type: Number,
7
default: 18,
8
},
9
birthday: Date,
10
})(
11
class User {
12
greet() {
13
return `Hello ${this.name}`;
14
}
15
}
16
);
17
18
/* The attributes "wraps" the Class, still providing access to its methods: */
19
20
const user = new User({
21
name: 'John Foo',
22
});
23
24
user.name; // 'John Foo'
25
user.greet(); // 'Hello John Foo'
Copied!

LICENSE

Last modified 2yr ago