0.13.0 • Published 3 years ago

archetype v0.13.0

Weekly downloads
10,567
License
Apache 2.0
Repository
github
Last release
3 years ago

archetype

Archetype is a library for casting and validating objects. It has exceptional support for deeply nested objects, type composition, custom types, and geoJSON.

CircleCI

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 {
  new Person({
    name: 'test',
    bandId: 'ImNotAValidObjectId'
  });
} catch(error) {
  error.errors['bandId'].message; // Mongodb ObjectId error
}

Archetypes are composable, inspectable, and extendable via extends.

Connect

Follow archetype on Twitter for updates, including our gists of the week. Here's some older gists of the week:

0.13.0

3 years ago

0.12.0

4 years ago

0.11.3

4 years ago

0.11.2

4 years ago

0.11.0

4 years ago

0.11.1

4 years ago

0.10.2

4 years ago

0.10.0

5 years ago

0.9.1

5 years ago

0.9.0

5 years ago

0.8.8

5 years ago

0.8.7

5 years ago

0.8.6

5 years ago

0.8.5

5 years ago

0.8.4

5 years ago

0.8.3

6 years ago

0.8.2

6 years ago

0.8.1

7 years ago

0.8.0

7 years ago

0.7.0

7 years ago

0.0.4

13 years ago