From 96189e260881ee46f1d42270e47ee27bf873ce17 Mon Sep 17 00:00:00 2001 From: Mike Cornwell Date: Thu, 23 Sep 2021 20:58:29 -0400 Subject: [PATCH 1/2] Fixed not allowing special properties in the instance. --- package.json | 4 ++-- src/models.js | 9 ++++++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index ef744fa..40666e4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "functional-models", - "version": "1.0.11", + "version": "1.0.12", "description": "A library for creating JavaScript function based models.", "main": "index.js", "scripts": { @@ -18,7 +18,7 @@ "functional" ], "publishConfig": { - "registry": "https://registry.npmjs.org" + "registry": "https://registry.npmjs.org/package" }, "author": "Mike Cornwell", "license": "GPLV3", diff --git a/src/models.js b/src/models.js index 7f8adca..20e2cbc 100644 --- a/src/models.js +++ b/src/models.js @@ -43,6 +43,12 @@ const Model = ( ) const create = (instanceValues = {}) => { + const specialInstanceProperties1 = MODEL_DEF_KEYS.reduce((acc, key) => { + if (key in instanceValues) { + return {...acc, [key]: instanceValues[key]} + } + return acc + }, {}) const loadedInternals = instanceProperties.reduce( (acc, [key, property]) => { const propertyGetter = property.createGetter(instanceValues[key]) @@ -75,7 +81,8 @@ const Model = ( {}, loadedInternals, specialProperties, - frameworkProperties + frameworkProperties, + specialInstanceProperties1, ) if (instanceCreatedCallback) { instanceCreatedCallback(instance) From 6f062043cce061229ee231aedfdd005032bb2bc0 Mon Sep 17 00:00:00 2001 From: Mike Cornwell Date: Thu, 23 Sep 2021 21:04:36 -0400 Subject: [PATCH 2/2] Updates. --- package.json | 4 ++-- test/src/models.test.js | 10 ++++++++++ test/src/properties.test.js | 2 +- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 40666e4..21ff28e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "functional-models", - "version": "1.0.12", + "version": "1.0.13", "description": "A library for creating JavaScript function based models.", "main": "index.js", "scripts": { @@ -18,7 +18,7 @@ "functional" ], "publishConfig": { - "registry": "https://registry.npmjs.org/package" + "registry": "https://registry.npmjs.org" }, "author": "Mike Cornwell", "license": "GPLV3", diff --git a/test/src/models.test.js b/test/src/models.test.js index f78e164..ec58487 100644 --- a/test/src/models.test.js +++ b/test/src/models.test.js @@ -39,6 +39,16 @@ describe('/src/models.js', () => { const actual = instance.meta.getModel().getProperties().myProperty assert.isOk(actual) }) + it('should combine the meta within the instance values', () => { + const input = { + myProperty: Property({ required: true }), + } + const model = Model('name', input) + const instance = model.create({ myProperty: 'value', meta: {random: () => 'random'}}) + const actual = instance.meta.random() + const expected = 'random' + assert.equal(actual, expected) + }) it('should flow through the additional special functions within the keyValues', () => { const input = { myProperty: Property({ required: true }), diff --git a/test/src/properties.test.js b/test/src/properties.test.js index 1d39d8d..dfbce26 100644 --- a/test/src/properties.test.js +++ b/test/src/properties.test.js @@ -508,7 +508,7 @@ describe('/src/properties.js', () => { const input = ['obj-id'] const fetcher = sinon .stub() - .callsFake((modelName, instanceValues) => instanceValues) + .callsFake((modelName, id) => ({id})) await ReferenceProperty(TestModel1, { fetcher, }).createGetter(...input)()