archetype
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 = ;const moment = ; // `Person` is now a constructorconst Person = name: 'string' bandId: $type: ObjectId $required: true createdAt: $type: moment $ ; const test = name: 'test' bandId: '507f191e810c19729de860ea'; testbandId; // Now a mongodb ObjectIdtestcreatedAt; // moment representing now
If casting fails, archetype throws a nice clean exception:
try ; catcherror errorerrors'bandId'message; // Mongodb ObjectId error
Archetypes are composable, inspectable, and extendable via extends
.