Skip to content

Commit

Permalink
add CHECK_ONCE mode on object models constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
sylvainpolletvillard committed Jun 13, 2021
1 parent fb16d65 commit 238dcaa
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 10 deletions.
4 changes: 3 additions & 1 deletion src/object-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ export const
_check = Symbol(),
_checked = Symbol(), // used to skip validation at instanciation for perf
_original = Symbol(), // used to bypass proxy
CHECK_ONCE = Symbol(),

initModel = (def, constructor, parent, init, getTraps, useNew) => {
const model = function (val = model.default, mode) {
if (useNew && !is(model, this)) return new model(val)
if (init) val = init(val, model, this)

if (mode === _checked || check(model, val))
return getTraps ? proxify(val, getTraps(model)) : val
return getTraps && mode !== CHECK_ONCE ? proxify(val, getTraps(model)) : val
}

if (parent) extend(model, parent)
Expand Down Expand Up @@ -387,6 +388,7 @@ Object.assign(Model.prototype, {
}
})

Model.CHECK_ONCE = CHECK_ONCE

export function BasicModel(def) {
return initModel(def, BasicModel)
Expand Down
1 change: 0 additions & 1 deletion test/array-model.spec.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ QUnit.test("constructor && proto", function (assert) {
assert.ok(Arr.definition === Number, "test Array model prop definition");
assert.ok(typeof Arr.assertions === "object", "test Array model prop assertions");


assert.ok(ArrayModel(undefined) instanceof ArrayModel, "ArrayModel can receive undefined as argument");
});

Expand Down
20 changes: 20 additions & 0 deletions test/object-model.spec.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -1305,4 +1305,24 @@ QUnit.test("Null-safe object traversal", function (assert) {
assert.strictEqual(config.local.time.format, undefined, "null-safe object traversal getter")
config.local.time.format = "12h";
assert.strictEqual(config.local.time.format, "12h", "null-safe object traversal setter")
})

QUnit.test("Check once mode", function(assert){
const Address = Model({ city: String, street: { name: String, number: Number }})
assert.throws(function () {
Address({ city: "Paris", street: { name: null, number: 12 }}, Model.CHECK_ONCE)
}, /TypeError.*expecting street.name to be String/, "check once mode still checks at instanciation")
const a = new Address({ city: "Paris", street: { name: "Champs-Elysees", number: 12 }}, Model.CHECK_ONCE)
a.street.name = null;
assert.strictEqual(a.street.name, null, "check once mode does not check future mutations")

class Person extends Model({ name: String, age: Number }) {
constructor({ name, age }) {
super({ name, age }, Model.CHECK_ONCE)
}
}

const john = new Person({ name: "John", age: 32 })
john.age = "twelve";
assert.strictEqual(john.age, "twelve", "check once mode does not check future mutations for extended class-based models")
})
16 changes: 8 additions & 8 deletions test/umd.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
<script src="lib/qunit.js"></script>
<script src="https://umd.cdn.pika.dev/objectmodel/v4"></script>
<script>const { Model, BasicModel, ObjectModel, ArrayModel, FunctionModel, MapModel, SetModel, Any } = objectmodel</script>
<script src="model.spec.js"></script>
<script src="basic-model.spec.js"></script>
<script src="any-model.spec.js"></script>
<script src="object-model.spec.js"></script>
<script src="array-model.spec.js"></script>
<script src="function-model.spec.js"></script>
<script src="map-model.spec.js"></script>
<script src="set-model.spec.js"></script>
<script src="model.spec.cjs"></script>
<script src="basic-model.spec.cjs"></script>
<script src="any-model.spec.cjs"></script>
<script src="object-model.spec.cjs"></script>
<script src="array-model.spec.cjs"></script>
<script src="function-model.spec.cjs"></script>
<script src="map-model.spec.cjs"></script>
<script src="set-model.spec.cjs"></script>
</head>

<body>
Expand Down
1 change: 1 addition & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export interface Model {
export interface ModelConstructor {
(definition: any): ObjectModel;
new(definition: any): ObjectModel;
CHECK_ONCE: symbol;
}

export interface BasicModel extends Model {
Expand Down

0 comments on commit 238dcaa

Please sign in to comment.