Détail du package

archetype

boosterfuels170.1kApache 2.00.13.1

readme

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:

changelog

0.11.3 (2020-05-18)

Bug Fixes

  • import: Remove unused import of index.js in src/unmarshal/index.js (71eac99)

0.11.1 (2020-02-27)

Bug Fixes

  • throw non-empty object $default error if passing in a date or other non-POJO (76e7bfa)

0.11.0 (2020-02-27)

Features

  • make new Type() clone by default, but allow passing in an option to disable cloning for perf (3638b57)

0.10.2 (2020-02-25)

Bug Fixes

  • clone empty arrays / objects if set as $default, throw if not an empty object (312aa14)

0.10.1 (2020-02-25)

Bug Fixes

  • clone empty arrays / objects if set as $default, throw if not an empty object (312aa14)

0.10.0 (2019-08-09)

Performance Improvements

  • remove cloneDeep() to reduce memory usage for huge objects (117c083)

0.9.1 (2019-06-10)

Bug Fixes

0.9.0 (2019-02-12)

Bug Fixes

  • ignore special properties (7b99d33)
  • to: disallow casting POJOs to numbers (5548fc2)

Features

  • make compile() return a full ES6 class for improved inheritance (d3e873e), closes #9

0.8.8 (2019-01-30)

Bug Fixes

  • remove leftover reference to lodash (34c6519)

0.8.7 (2019-01-29)

Bug Fixes

  • clean up unnecessary _.each() usage (ab32c15)

0.8.6 (2019-01-28)

0.8.4 (2019-01-27)

Bug Fixes

  • defaults: handle nested defaults correctly (c02b89b), closes #16
  • to: dont convert null -> undefined when casting strings (92faef2)

0.8.3 (2018-03-21)

Features

  • support inPlace updates for recursive schemas (8f61d32), closes #11

0.8.2 (2017-12-03)

Bug Fixes

  • unmarshal: don't use $transform on array if nested in child element (78bc272), closes #13

0.8.1 (2017-10-19)

Bug Fixes

  • handle passing array of paths to Type.omit() (0cfc96f)

0.8.0 (2017-09-21)

Features

  • export CastError type (1c3a971), closes #7
  • helpers: add matchType (7f5e1e8)
  • unmarshal: add rudimentary support for $transform (8bdf8db)
  • unmarshal: report errors from $transform (a8c9e3d)
  • unmarshal: support $transform in arrays (2d11cf5)

0.6.1 (2017-03-17)

Bug Fixes

  • unmarshal: handle $type: Array with non-arrays (f65c063)