Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions features/arrayFields.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 12 additions & 0 deletions features/stepDefinitions/steps.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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 = {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
12 changes: 10 additions & 2 deletions src/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -153,6 +160,7 @@ const CONFIG_TO_VALIDATE_METHOD = {
isNumber: _boolChoice(isNumber),
isString: _boolChoice(isString),
isArray: _boolChoice(isArray),
choices,
}

const createFieldValidator = config => {
Expand Down
27 changes: 27 additions & 0 deletions test/src/fields.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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()', () => {
Expand Down