From 26aa7531f48fa31c0fa9ef3fb05640c708f81359 Mon Sep 17 00:00:00 2001 From: Mike Cornwell Date: Fri, 24 Sep 2021 20:32:13 -0400 Subject: [PATCH 1/3] Needed for date bug. --- src/validation.js | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/validation.js b/src/validation.js index 79b6b2a..8d0d3a6 100644 --- a/src/validation.js +++ b/src/validation.js @@ -77,6 +77,16 @@ const choices = choiceArray => value => { return undefined } +const isDate = value => { + if (!value) { + return 'Date value is empty' + } + if (!value.toISOString) { + return 'Value is not a string' + } + return undefined +} + const isRequired = value => { if (value === true || value === false) { return undefined @@ -84,7 +94,13 @@ const isRequired = value => { if (isNumber(value) === undefined) { return undefined } - return isEmpty(value) ? 'A value is required' : undefined + const empty = isEmpty(value) + if (empty) { + if (isDate(value)) { + return 'A value is required' + } + } + return undefined } const maxNumber = max => value => { @@ -186,9 +202,15 @@ const createPropertyValidator = config => { }), ...(config.validators ? config.validators : []), ].filter(x => x) + const isRequiredValue = config.required + ? true + : validators.includes(isRequired) const validator = validators.length > 0 ? aggregateValidator(validators) : emptyValidator const _propertyValidator = async value => { + if (!value && !isRequiredValue) { + return [] + } const errors = await validator(value) return [...new Set(flatMap(errors))] } From 8c0e2f8dc1e56ea47c19514512a3bf44d9cdd18b Mon Sep 17 00:00:00 2001 From: Mike Cornwell Date: Fri, 24 Sep 2021 20:32:29 -0400 Subject: [PATCH 2/3] BUmp --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 21ff28e..0007fce 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "functional-models", - "version": "1.0.13", + "version": "1.0.14", "description": "A library for creating JavaScript function based models.", "main": "index.js", "scripts": { From 8f1fb501480fc00029f8de24e90c59e8d65d771e Mon Sep 17 00:00:00 2001 From: Michael Cornwell Date: Fri, 24 Sep 2021 20:45:15 -0400 Subject: [PATCH 3/3] Finishing up. --- src/models.js | 4 ++-- src/validation.js | 3 ++- test/src/models.test.js | 5 ++++- test/src/properties.test.js | 4 +--- test/src/validation.test.js | 23 +++++++++++++++++++++++ 5 files changed, 32 insertions(+), 7 deletions(-) diff --git a/src/models.js b/src/models.js index 20e2cbc..a0cfb6d 100644 --- a/src/models.js +++ b/src/models.js @@ -45,7 +45,7 @@ const Model = ( const create = (instanceValues = {}) => { const specialInstanceProperties1 = MODEL_DEF_KEYS.reduce((acc, key) => { if (key in instanceValues) { - return {...acc, [key]: instanceValues[key]} + return { ...acc, [key]: instanceValues[key] } } return acc }, {}) @@ -82,7 +82,7 @@ const Model = ( loadedInternals, specialProperties, frameworkProperties, - specialInstanceProperties1, + specialInstanceProperties1 ) if (instanceCreatedCallback) { instanceCreatedCallback(instance) diff --git a/src/validation.js b/src/validation.js index 8d0d3a6..42b8710 100644 --- a/src/validation.js +++ b/src/validation.js @@ -82,7 +82,7 @@ const isDate = value => { return 'Date value is empty' } if (!value.toISOString) { - return 'Value is not a string' + return 'Value is not a date' } return undefined } @@ -245,6 +245,7 @@ module.exports = { isString, isInteger, isType, + isDate, isArray, isRequired, maxNumber, diff --git a/test/src/models.test.js b/test/src/models.test.js index ec58487..75f5de6 100644 --- a/test/src/models.test.js +++ b/test/src/models.test.js @@ -44,7 +44,10 @@ describe('/src/models.js', () => { myProperty: Property({ required: true }), } const model = Model('name', input) - const instance = model.create({ myProperty: 'value', meta: {random: () => 'random'}}) + const instance = model.create({ + myProperty: 'value', + meta: { random: () => 'random' }, + }) const actual = instance.meta.random() const expected = 'random' assert.equal(actual, expected) diff --git a/test/src/properties.test.js b/test/src/properties.test.js index dfbce26..d2878da 100644 --- a/test/src/properties.test.js +++ b/test/src/properties.test.js @@ -506,9 +506,7 @@ describe('/src/properties.js', () => { }) it('should provide the passed in model and the instance values when switch-a-roo fetcher is used', async () => { const input = ['obj-id'] - const fetcher = sinon - .stub() - .callsFake((modelName, id) => ({id})) + const fetcher = sinon.stub().callsFake((modelName, id) => ({ id })) await ReferenceProperty(TestModel1, { fetcher, }).createGetter(...input)() diff --git a/test/src/validation.test.js b/test/src/validation.test.js index 896ab09..7e30183 100644 --- a/test/src/validation.test.js +++ b/test/src/validation.test.js @@ -8,6 +8,7 @@ const { isInteger, isString, isArray, + isDate, arrayType, isRequired, maxNumber, @@ -33,6 +34,24 @@ const TestModel2 = Model('TestModel2', { }) describe('/src/validation.js', () => { + describe('#isDate()', () => { + it('should return an error if value is null', () => { + const actual = isDate(null) + assert.isOk(actual) + }) + it('should return an error if value is undefined', () => { + const actual = isDate(undefined) + assert.isOk(actual) + }) + it('should return an error object does not have toISOString', () => { + const actual = isDate({}) + assert.isOk(actual) + }) + it('should return undefined if a date', () => { + const actual = isDate(new Date()) + assert.isUndefined(actual) + }) + }) describe('#referenceTypeMatch()', () => { it('should allow a function for a model', () => { const myModel = TestModel1.create() @@ -85,6 +104,10 @@ describe('/src/validation.js', () => { const actual = isRequired(1) assert.isUndefined(actual) }) + it('should return undefined when a date is passed', () => { + const actual = isRequired(new Date()) + assert.isUndefined(actual) + }) it('should return undefined when 0 is passed', () => { const actual = isRequired(0) assert.isUndefined(actual)