Share your code. npm Orgs help your team discover, share, and reuse code. Create a free org »

    archetypepublic

    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 {
      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.

    Connect

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

    Keywords

    none

    install

    npm i archetype

    Downloadslast 7 days

    143

    version

    0.8.3

    license

    Apache 2.0

    repository

    github.com

    last publish

    collaborators

    • avatar