sylvainpolletvillard / ObjectModel

Strong Dynamically Typed Object Modeling for JavaScript
http://objectmodel.js.org
MIT License
467 stars 30 forks source link

Way to define a custom model constructor? #122

Closed ArturBaybulatov closed 4 years ago

ArturBaybulatov commented 4 years ago

Trying to define a custom, plain object model constructor. But get an error:

import _ from 'lodash';
import {ObjectModel} from 'objectmodel';

const PlainObjectModel = model => ObjectModel(model).assert(_.isPlainObject);

PlainObjectModel({foo: Number})({foo: 123});
// -> TypeError: assertion "isPlainObject" returned false for value {foo: 123}

Upd: Just found out, that I can do it like this:

const PlainObjectModel = model => BasicModel(model).assert(_.isPlainObject);
sylvainpolletvillard commented 4 years ago

Yes, you found out by yourself. Using a custom factory function should do the trick in 99% cases