archetype-js

0.6.1 • Public • Published

archetype

Build Status

Archetype is a runtime type-casting library. Its purpose is to compose types from existing types in a way that's easy to write and document.

const { ObjectId } = require('mongodb');
const moment = require('moment');
 
// `Person` is now a constructor
const Person = new Archetype({
  name: 'string',
  bandId: {
    $type: ObjectId,
    $required: true
  },
  createdAt: {
    $type: moment,
    $default: () => moment()
  }
}).compile('Person');
 
const test = new Person({
  name: 'test',
  bandId: '507f191e810c19729de860ea'
});
 
test.bandId; // Now a mongodb ObjectId
test.createdAt; // moment representing now

If casting fails, archetype throws a nice clean exception:

try {
  Person({ // <-- calling with `new` is optional
    name: 'test',
    bandId: 'ImNotAValidObjectId'
  });
} catch(error) {
  error.errors['bandId'].message; // Mongodb ObjectId error
}

Archetypes are composable, inspectable, and extendable via extends.

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 0.6.1
    1
    • latest

Version History

Package Sidebar

Install

npm i archetype-js

Weekly Downloads

2

Version

0.6.1

License

Apache 2.0

Last publish

Collaborators

  • vkarpov15