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
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.13",
"version": "1.0.14",
"description": "A library for creating JavaScript function based models.",
"main": "index.js",
"scripts": {
Expand Down
4 changes: 2 additions & 2 deletions src/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}, {})
Expand Down Expand Up @@ -82,7 +82,7 @@ const Model = (
loadedInternals,
specialProperties,
frameworkProperties,
specialInstanceProperties1,
specialInstanceProperties1
)
if (instanceCreatedCallback) {
instanceCreatedCallback(instance)
Expand Down
25 changes: 24 additions & 1 deletion src/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,30 @@ const choices = choiceArray => value => {
return undefined
}

const isDate = value => {
if (!value) {
return 'Date value is empty'
}
if (!value.toISOString) {
return 'Value is not a date'
}
return undefined
}

const isRequired = value => {
if (value === true || value === false) {
return undefined
}
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 => {
Expand Down Expand Up @@ -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))]
}
Expand Down Expand Up @@ -223,6 +245,7 @@ module.exports = {
isString,
isInteger,
isType,
isDate,
isArray,
isRequired,
maxNumber,
Expand Down
5 changes: 4 additions & 1 deletion test/src/models.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 1 addition & 3 deletions test/src/properties.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)()
Expand Down
23 changes: 23 additions & 0 deletions test/src/validation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const {
isInteger,
isString,
isArray,
isDate,
arrayType,
isRequired,
maxNumber,
Expand All @@ -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()
Expand Down Expand Up @@ -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)
Expand Down