From 360b89950731d3d8cfcfdea357ad0415fe87678f Mon Sep 17 00:00:00 2001 From: Michael Cornwell Date: Fri, 17 Sep 2021 13:21:19 -0400 Subject: [PATCH 1/2] Added support for choices in arrays. --- features/arrayFields.feature | 14 ++++++++++++++ features/stepDefinitions/steps.js | 12 ++++++++++++ src/validation.js | 12 ++++++++++-- test/src/fields.test.js | 27 +++++++++++++++++++++++++++ 4 files changed, 63 insertions(+), 2 deletions(-) diff --git a/features/arrayFields.feature b/features/arrayFields.feature index b8dac91..8e28deb 100644 --- a/features/arrayFields.feature +++ b/features/arrayFields.feature @@ -36,3 +36,17 @@ Feature: Array Fields Then the getArrayField field is called on the model Then functions.validate is called Then an array of 0 errors is shown + + Scenario: A model that uses the arrayField with the choice validator should pass validation with no errors. + Given ArrayModel4 model is used + When ArrayModelData5 data is inserted + Then the getArrayField field is called on the model + Then functions.validate is called + Then an array of 0 errors is shown + + Scenario: A model that uses the arrayField with the choice validator should fail validation when one value is outside the choices + Given ArrayModel4 model is used + When ArrayModelData6 data is inserted + Then the getArrayField field is called on the model + Then functions.validate is called + Then an array of 1 errors is shown diff --git a/features/stepDefinitions/steps.js b/features/stepDefinitions/steps.js index b95d4b6..ae14a08 100644 --- a/features/stepDefinitions/steps.js +++ b/features/stepDefinitions/steps.js @@ -21,6 +21,12 @@ const MODEL_DEFINITIONS = { ArrayModel3: createModel({ arrayField: arrayField({}), }), + ArrayModel4: createModel({ + arrayField: arrayField({ + choices: [4, 5, 6], + validators: [validation.arrayType(validation.TYPE_PRIMATIVES.integer)], + }), + }), } const MODEL_INPUT_VALUES = { @@ -46,6 +52,12 @@ const MODEL_INPUT_VALUES = { ArrayModelData4: { arrayField: ['a-string', 1, {}, true], }, + ArrayModelData5: { + arrayField: [4, 5, 5, 5, 6], + }, + ArrayModelData6: { + arrayField: [4, 5, 5, 5, 6, 1], + }, } const EXPECTED_FIELDS = { diff --git a/src/validation.js b/src/validation.js index ad2a87a..778dd62 100644 --- a/src/validation.js +++ b/src/validation.js @@ -69,8 +69,15 @@ const meetsRegex = } const choices = choiceArray => value => { - if (choiceArray.includes(value) === false) { - return 'Not a valid choice' + if (Array.isArray(value)) { + const bad = value.find(v => !choiceArray.includes(v)) + if (bad) { + return `${bad} is not a valid choice` + } + } else { + if (choiceArray.includes(value) === false) { + return `${value} is not a valid choice` + } } return undefined } @@ -153,6 +160,7 @@ const CONFIG_TO_VALIDATE_METHOD = { isNumber: _boolChoice(isNumber), isString: _boolChoice(isString), isArray: _boolChoice(isArray), + choices, } const createFieldValidator = config => { diff --git a/test/src/fields.test.js b/test/src/fields.test.js index 9849e8d..ba8190c 100644 --- a/test/src/fields.test.js +++ b/test/src/fields.test.js @@ -6,6 +6,7 @@ const { referenceField, arrayField, } = require('../../src/fields') +const { TYPE_PRIMATIVES, arrayType } = require('../../src/validation') const { createModel } = require('../../src/models') describe('/src/fields.js', () => { @@ -49,6 +50,32 @@ describe('/src/fields.js', () => { const expected = [] assert.deepEqual(actual, expected) }) + it('should error an array passed in when it doesnt have the right types', async () => { + const theField = arrayField({ + validators: [arrayType(TYPE_PRIMATIVES.integer)], + }) + const getter = theField.createGetter([1, 'string', 3]) + const validator = theField.getValidator(getter) + const actual = await validator() + const expected = 1 + assert.deepEqual(actual.length, expected) + }) + it('should validate an array with [4,4,5,5,6,6] when choices are [4,5,6]', async () => { + const theField = arrayField({ choices: [4, 5, 6] }) + const getter = theField.createGetter([4, 4, 5, 5, 6, 6]) + const validator = theField.getValidator(getter) + const actual = await validator() + const expected = [] + assert.deepEqual(actual, expected) + }) + it('should return errors when an array with [4,4,3,5,5,6,6] when choices are [4,5,6]', async () => { + const theField = arrayField({ choices: [4, 5, 6] }) + const getter = theField.createGetter([4, 4, 3, 5, 5, 6, 6]) + const validator = theField.getValidator(getter) + const actual = await validator() + const expected = 1 + assert.equal(actual.length, expected) + }) }) }) describe('#field()', () => { From a9a881a9503baaabc18e4d0a034a209ee174222e Mon Sep 17 00:00:00 2001 From: Michael Cornwell Date: Fri, 17 Sep 2021 13:21:32 -0400 Subject: [PATCH 2/2] Bump --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 7c5a092..ba62421 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "functional-models", - "version": "1.0.6", + "version": "1.0.7", "description": "A library for creating JavaScript function based models.", "main": "index.js", "scripts": {