ObjectModel

Strong Dynamically Typed Object Modeling for JavaScript

MIT License

Downloads
15K
Stars
467
Committers
9
ObjectModel - v4.4.0 Latest Release

Published by sylvainpolletvillard almost 2 years ago

  • Improved a lot the TypeScript definitions for all models - looking for feedback/reports
  • Removed FunctionModel#extend: this method has never been properly documented and was not intuitive to use
ObjectModel - v4.3.0

Published by sylvainpolletvillard over 3 years ago

  • Performance improvement: every autocasted model now bypass its second validation step
  • Added a CHECK_ONCE mode as a second argument to object models constructors, that only validates the data once at model instanciation:
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.

ObjectModel - v4.2.0

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

ObjectModel - v4.1

Published by sylvainpolletvillard over 4 years ago

  • remove getPrototype proxy trap on object models, which should improve interoperability with other libraries
  • upgrade dependencies
  • minor perf improvements
ObjectModel - v4.0

Published by sylvainpolletvillard over 5 years ago

Breaking changes in v4

  • Node 6-7 support dropped. Long term support has officially ended on 2018-04-30.
  • UMD build dropped in favor of ESM builds exclusively. See explainations below.
  • Browser support is the same than v3, but you will have to bring your own transpiler. See explainations below.
  • REWORK defaults assignment: defaults for object models are no longer assigned to the model prototype, but applied only once at model instanciation, and now work for nested properties default values. This change aims to make default values behaviour more intuitive because using the prototype was too confusing for many users (I received many questions about it). To get the original behaviour, manually assign the default values to the model prototype.
  • API cleanup: removed ObjectModel 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.
  • API cleanup: removed Model validate method, in favor of a second optional argument for the test method. Explaination: https://github.com/sylvainpolletvillard/ObjectModel/issues/96
  • API cleanup: removed the params argument to Model and ObjectModel constructors ; can be replaced with Object.assign
  • MOVED sealed object models to their own external models (see examples/sealed.js). This reduces the size of the library for users that don't use sealed models.
  • REMOVED the numberOfArgs assertion that was bundled with all function models. Explaination: https://github.com/sylvainpolletvillard/ObjectModel/issues/108

New features

  • Any
  • Any.remaining

Read the docs here

Moving from UMD to ESM

ObjectModel used to be distributed in both ESM (EcmaScript Module) and UMD (Universal Module Definition) format. ESM provide several advantages over UMD:

  • you can import only the features you need, and get a smaller library size penalty
  • modules run in strict mode by default, which covers more potential errors
  • globals are dangerous because they can be overridden by other libs, user scripts or browser extensions
  • AMD and other asynchronous module formats are basically dead now that dynamic import() is standard

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.

Bring your own transpiler

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:

  • it seems unfair to impose the same size and perf penalties on all projects, even those targeting a smaller set of modern browsers.
  • this fits logically with the decision to only distribute ESM builds, since you will have to configure a build step to support older browsers
  • transpilers in 2019 are a very common part of every web dev's toolkit, and included by default in most modern JS frameworks

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.

Roadmap and public proposals

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.

ObjectModel - v3.7

Published by sylvainpolletvillard about 6 years ago

  • Huge performance improvements for model instanciation and autocast (~4x faster)
  • Autocast no longer works for Basic Models. This is a very specific usecase and you should not be impacted
  • Improvements to TypeScript definition file
ObjectModel - v3.6

Published by sylvainpolletvillard about 6 years ago

  • share model code between all list models: ArrayModel, MapModel and SetModel
  • autocast ArrayModel items at model instanciation
  • check if Map and Set models constructor argument is iterable
  • added Array.prototype.fill and Array.prototype.copyWithin to ArrayModel mutator methods
  • add autocast to Array.prototype.fill method
ObjectModel - v3.5

Published by sylvainpolletvillard over 6 years ago

  • Object models now require passed values in constructor to be objects or inherit from objects. Primitives that could be casted to objects that meet the definition are no longer accepted. Closes #74
ObjectModel - v3.4

Published by sylvainpolletvillard over 6 years ago

  • Add Model.Name for debugging purposes with custom devtool formatters
ObjectModel - v3.3

Published by sylvainpolletvillard over 6 years ago

  • added support for <script type="module"> and direct use of ES6 modules in supported browsers
  • internal methods are now exposed as Symbols
  • improved library TS definition file
  • reduced the number of internal dependencies
ObjectModel - v3.2

Published by sylvainpolletvillard almost 7 years ago

  • ObjectModel instances enumerated keys behaviour has been changed to not include optional properties that are defined in the model definition but not assigned on the instance. This is required to resolve some issues with some external libraries integration.
ObjectModel - v3.1

Published by sylvainpolletvillard over 7 years ago

  • sealed models now prevent all undeclared properties at initial or future assignments, on direct and nested properties
  • add a params argument to object models constructor, i.e. Model(definition, { sealed: true })
  • add a dedicated section in the docs for sealed models
  • bugfix: assignments with null-safe traversal
ObjectModel - v3.0

Published by sylvainpolletvillard over 7 years ago

New features in v3

  • ES6 Proxies are used everywhere instead of ES5 property getters
  • New models: Map models and Set models
  • A custom devtool formatter in Chrome
  • sealed mode for object models to prevent undeclared properties
  • usable as ES Module: import { ObjectModel } from "objectmodel" to enable tree shaking

Breaking changes in v3

  • since v3 relies on ES6 Proxies, it is only usable on modern browsers: Edge 14+ / Firefox 47+ / Chrome 50+ / Node 6+
  • All the possible operations on objects, including special ones like delete, 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 model
  • Private properties are now private for real: you cannot use them outside of the model's own methods

Notes

This 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 !

ObjectModel - v2.6

Published by sylvainpolletvillard almost 8 years ago

  • Basic, Object and Array model constructors now throws an error if no definition is provided
  • All the type errors now includes path information and have the same properties
ObjectModel - v2.5

Published by sylvainpolletvillard almost 8 years ago

  • array models elements are now automatically casted to their suitable model if found
  • function models arguments and return values are now automatically casted to their suitable model if found
  • added support for yarn
ObjectModel - v2.4

Published by sylvainpolletvillard about 8 years ago

  • NaN and Invalid Date are now considered invalid for types Number and Date
  • function models instances called with invalid arguments are no longer executed
  • added model.defaultTo
ObjectModel - v2.3

Published by sylvainpolletvillard about 8 years ago

  • assertions now use try/catch to catch exceptions. When an exception is catched, the assertion fails and the exception is passed to the error collector
  • TypeError messages have been improved, especially for assertion failures. It now indicates the value tested and what the assertion returned.
ObjectModel - v2.2

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

ObjectModel - v2.1

Published by sylvainpolletvillard over 8 years ago

  • assertions are now inherited from Model.prototype, which enables you to add global assertions
  • assertions now require to return true to validate. Truthy values do not pass anymore
  • assertions error messages can now be functions called with the assertion result as argument
ObjectModel - v1.0.0

Published by sylvainpolletvillard over 8 years ago