2.0.1 • Published 5 years ago

structure v2.0.1

Weekly downloads
1,049
License
MIT
Repository
github
Last release
5 years ago

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

npm Build Status Coverage Status Code Climate JS.ORG

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

Documentation

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.

const { attributes } = require('structure');

const User = attributes({
  name: String,
  age: {
    type: Number,
    default: 18,
  },
  birthday: Date,
})(
  class User {
    greet() {
      return `Hello ${this.name}`;
    }
  }
);

/* The attributes "wraps" the Class, still providing access to its methods: */

const user = new User({
  name: 'John Foo',
});

user.name; // 'John Foo'
user.greet(); // 'Hello John Foo'

Contributing

LICENSE

2.0.1

5 years ago

2.0.0

5 years ago

2.0.0-alpha.4

5 years ago

2.0.0-alpha.3

5 years ago

2.0.0-alpha.0

5 years ago

2.0.0-alpha.1

5 years ago

2.0.0-alpha.2

5 years ago

1.8.0

6 years ago

1.7.0

6 years ago

1.6.0

6 years ago

1.5.0

6 years ago

1.4.0

6 years ago

1.3.2

6 years ago

1.3.1

6 years ago

1.3.0

7 years ago

1.2.2

7 years ago

1.2.1

8 years ago

1.2.0

8 years ago

1.1.0

8 years ago

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago

0.0.1

13 years ago