Strong Dynamically Typed Object Modeling for JavaScript
MIT License
Published by sylvainpolletvillard over 3 years ago
const Address = Model({ city: String, street: { name: String, number: Number }})
const a = new Address({ city: "Paris", street: { name: "Champs-Elysees", number: 12 }}, Model.CHECK_ONCE)
This feature has been asked to deal with specific performance issues, in situations where disabling ObjectModel completely is complicated or not desirable. This is not supposed to be a common usecase.
Published by sylvainpolletvillard over 4 years ago
Due to several issues raised after CJS build has been removed in v4, a CJS version of ObjectModel will be automatically generated thanks to https://www.npmjs.com/package/cjyes by @developit
Published by sylvainpolletvillard over 4 years ago
Published by sylvainpolletvillard over 5 years ago
defaults
method. With the rework of defaults assignment, the distinction between defaults
and defaultTo
is no longer needed, so you can replace all defaults()
calls with defaultTo()
instead.validate
method, in favor of a second optional argument for the test
method. Explaination: https://github.com/sylvainpolletvillard/ObjectModel/issues/96
params
argument to Model and ObjectModel constructors ; can be replaced with Object.assign
examples/sealed.js
). This reduces the size of the library for users that don't use sealed models.numberOfArgs
assertion that was bundled with all function models. Explaination: https://github.com/sylvainpolletvillard/ObjectModel/issues/108
Read the docs here
ObjectModel used to be distributed in both ESM (EcmaScript Module) and UMD (Universal Module Definition) format. ESM provide several advantages over UMD:
In 2019, modern browsers now support natively ESM, and older browsers can still be supported through bundling tools like Webpack, Parcel or Rollup.
Therefore, starting from v4, ObjectModel will be exclusively distributed in ES module format. You should import what you need from the library like this:
import { Model, ObjectModel, ArrayModel } from 'objectmodel'
Some may consider this move to be too early, but there is technically no more argument in favor of UMD today, and legacy codebases can still use bundler tools or manually insert an UMD wrapper if needed. If you just want the ObjectModel API exposed as global variables like before, although I won't personally recommend it, this can be easily done with:
<script type="module">
import * as globals from "./dist/object-model.js"
Object.assign(window, globals)
</script>
Please consider switching to ES Modules for all your JavaScript projects if not done already.
In previous builds of ObjectModel, all the JS code transformations required to support older browsers were included in the distributed files, so that you could directly use the library and get maximum browser support. However, this came at a price: modern browsers had to suffer size and performance penalties due to useless transformations only required for older browsers support.
Therefore, starting from v4, ObjectModel is distributed in the latest version of the ECMAScript standard, with no consideration for code transforms used to improve the browser support. It will be your responsibility to configure a transpiler on your project for this purpose. The most popular option, and my personal recommendation, would be babel and @babel/preset-env.
This decision is motivated by these arguments:
As always, if you are unhappy with these changes, feel free to stick with an older version of ObjectModel which will continue to receive bugfixes.
Since this library is starting to get some traction, I try to make its evolution more participative. New features and breaking changes in v4 have all been introduced as proposals in github issues, open to public discussion and feedback for a few months. I also look at open source projects using ObjectModel to see how it is being used and how could I improve the lib for them. As a reminder, there is a Gitter channel if you want to ask me questions or discuss about ObjectModel in a way that does not justify opening a issue on Github.
Published by sylvainpolletvillard about 6 years ago
Published by sylvainpolletvillard about 6 years ago
Array.prototype.fill
and Array.prototype.copyWithin
to ArrayModel mutator methodsArray.prototype.fill
methodPublished by sylvainpolletvillard over 6 years ago
Published by sylvainpolletvillard over 6 years ago
Model.Name
for debugging purposes with custom devtool formattersPublished by sylvainpolletvillard over 6 years ago
<script type="module">
and direct use of ES6 modules in supported browsersPublished by sylvainpolletvillard almost 7 years ago
Published by sylvainpolletvillard over 7 years ago
Model(definition, { sealed: true })
Published by sylvainpolletvillard over 7 years ago
sealed
mode for object models to prevent undeclared propertiesimport { ObjectModel } from "objectmodel"
to enable tree shakingdelete
, defineProperty
or Object.keys
, are now checked against the model definition. Consequently, new type errors could be found after upgrading.Model.conventionForPrivate
and Model.conventionForConstant
have been moved to Model.prototype
so that you can change these conventions specifically by modelThis is almost a full rewrite of the library that I have been working on for over a year. ObjectModel is now written in ES6, has solid unit testing with over 400 assertions, and overall has much better mechanics to validate and cast your models. Enjoy !
Published by sylvainpolletvillard almost 8 years ago
path
information and have the same propertiesPublished by sylvainpolletvillard about 8 years ago
NaN
and Invalid Date
are now considered invalid for types Number
and Date
model.defaultTo
Published by sylvainpolletvillard about 8 years ago
Published by sylvainpolletvillard about 8 years ago
When constructing a composed object model, properties are automatically casted to model instances according to the definition. Now, this also works for union types. If several models are suitable within a union type, there is an ambiguity and the object will be left untouched with a warning console message.
See https://github.com/sylvainpolletvillard/ObjectModel/issues/24 for details
Published by sylvainpolletvillard over 8 years ago
Published by sylvainpolletvillard over 8 years ago