functional-models 1.0.12 → 1.0.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/models.js +2 -2
- package/src/validation.js +24 -1
- package/test/src/models.test.js +13 -0
- package/test/src/properties.test.js +1 -3
- package/test/src/validation.test.js +23 -0
package/package.json
CHANGED
package/src/models.js
CHANGED
|
@@ -45,7 +45,7 @@ const Model = (
|
|
|
45
45
|
const create = (instanceValues = {}) => {
|
|
46
46
|
const specialInstanceProperties1 = MODEL_DEF_KEYS.reduce((acc, key) => {
|
|
47
47
|
if (key in instanceValues) {
|
|
48
|
-
return {...acc, [key]: instanceValues[key]}
|
|
48
|
+
return { ...acc, [key]: instanceValues[key] }
|
|
49
49
|
}
|
|
50
50
|
return acc
|
|
51
51
|
}, {})
|
|
@@ -82,7 +82,7 @@ const Model = (
|
|
|
82
82
|
loadedInternals,
|
|
83
83
|
specialProperties,
|
|
84
84
|
frameworkProperties,
|
|
85
|
-
specialInstanceProperties1
|
|
85
|
+
specialInstanceProperties1
|
|
86
86
|
)
|
|
87
87
|
if (instanceCreatedCallback) {
|
|
88
88
|
instanceCreatedCallback(instance)
|
package/src/validation.js
CHANGED
|
@@ -77,6 +77,16 @@ const choices = choiceArray => value => {
|
|
|
77
77
|
return undefined
|
|
78
78
|
}
|
|
79
79
|
|
|
80
|
+
const isDate = value => {
|
|
81
|
+
if (!value) {
|
|
82
|
+
return 'Date value is empty'
|
|
83
|
+
}
|
|
84
|
+
if (!value.toISOString) {
|
|
85
|
+
return 'Value is not a date'
|
|
86
|
+
}
|
|
87
|
+
return undefined
|
|
88
|
+
}
|
|
89
|
+
|
|
80
90
|
const isRequired = value => {
|
|
81
91
|
if (value === true || value === false) {
|
|
82
92
|
return undefined
|
|
@@ -84,7 +94,13 @@ const isRequired = value => {
|
|
|
84
94
|
if (isNumber(value) === undefined) {
|
|
85
95
|
return undefined
|
|
86
96
|
}
|
|
87
|
-
|
|
97
|
+
const empty = isEmpty(value)
|
|
98
|
+
if (empty) {
|
|
99
|
+
if (isDate(value)) {
|
|
100
|
+
return 'A value is required'
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return undefined
|
|
88
104
|
}
|
|
89
105
|
|
|
90
106
|
const maxNumber = max => value => {
|
|
@@ -186,9 +202,15 @@ const createPropertyValidator = config => {
|
|
|
186
202
|
}),
|
|
187
203
|
...(config.validators ? config.validators : []),
|
|
188
204
|
].filter(x => x)
|
|
205
|
+
const isRequiredValue = config.required
|
|
206
|
+
? true
|
|
207
|
+
: validators.includes(isRequired)
|
|
189
208
|
const validator =
|
|
190
209
|
validators.length > 0 ? aggregateValidator(validators) : emptyValidator
|
|
191
210
|
const _propertyValidator = async value => {
|
|
211
|
+
if (!value && !isRequiredValue) {
|
|
212
|
+
return []
|
|
213
|
+
}
|
|
192
214
|
const errors = await validator(value)
|
|
193
215
|
return [...new Set(flatMap(errors))]
|
|
194
216
|
}
|
|
@@ -223,6 +245,7 @@ module.exports = {
|
|
|
223
245
|
isString,
|
|
224
246
|
isInteger,
|
|
225
247
|
isType,
|
|
248
|
+
isDate,
|
|
226
249
|
isArray,
|
|
227
250
|
isRequired,
|
|
228
251
|
maxNumber,
|
package/test/src/models.test.js
CHANGED
|
@@ -39,6 +39,19 @@ describe('/src/models.js', () => {
|
|
|
39
39
|
const actual = instance.meta.getModel().getProperties().myProperty
|
|
40
40
|
assert.isOk(actual)
|
|
41
41
|
})
|
|
42
|
+
it('should combine the meta within the instance values', () => {
|
|
43
|
+
const input = {
|
|
44
|
+
myProperty: Property({ required: true }),
|
|
45
|
+
}
|
|
46
|
+
const model = Model('name', input)
|
|
47
|
+
const instance = model.create({
|
|
48
|
+
myProperty: 'value',
|
|
49
|
+
meta: { random: () => 'random' },
|
|
50
|
+
})
|
|
51
|
+
const actual = instance.meta.random()
|
|
52
|
+
const expected = 'random'
|
|
53
|
+
assert.equal(actual, expected)
|
|
54
|
+
})
|
|
42
55
|
it('should flow through the additional special functions within the keyValues', () => {
|
|
43
56
|
const input = {
|
|
44
57
|
myProperty: Property({ required: true }),
|
|
@@ -506,9 +506,7 @@ describe('/src/properties.js', () => {
|
|
|
506
506
|
})
|
|
507
507
|
it('should provide the passed in model and the instance values when switch-a-roo fetcher is used', async () => {
|
|
508
508
|
const input = ['obj-id']
|
|
509
|
-
const fetcher = sinon
|
|
510
|
-
.stub()
|
|
511
|
-
.callsFake((modelName, instanceValues) => instanceValues)
|
|
509
|
+
const fetcher = sinon.stub().callsFake((modelName, id) => ({ id }))
|
|
512
510
|
await ReferenceProperty(TestModel1, {
|
|
513
511
|
fetcher,
|
|
514
512
|
}).createGetter(...input)()
|
|
@@ -8,6 +8,7 @@ const {
|
|
|
8
8
|
isInteger,
|
|
9
9
|
isString,
|
|
10
10
|
isArray,
|
|
11
|
+
isDate,
|
|
11
12
|
arrayType,
|
|
12
13
|
isRequired,
|
|
13
14
|
maxNumber,
|
|
@@ -33,6 +34,24 @@ const TestModel2 = Model('TestModel2', {
|
|
|
33
34
|
})
|
|
34
35
|
|
|
35
36
|
describe('/src/validation.js', () => {
|
|
37
|
+
describe('#isDate()', () => {
|
|
38
|
+
it('should return an error if value is null', () => {
|
|
39
|
+
const actual = isDate(null)
|
|
40
|
+
assert.isOk(actual)
|
|
41
|
+
})
|
|
42
|
+
it('should return an error if value is undefined', () => {
|
|
43
|
+
const actual = isDate(undefined)
|
|
44
|
+
assert.isOk(actual)
|
|
45
|
+
})
|
|
46
|
+
it('should return an error object does not have toISOString', () => {
|
|
47
|
+
const actual = isDate({})
|
|
48
|
+
assert.isOk(actual)
|
|
49
|
+
})
|
|
50
|
+
it('should return undefined if a date', () => {
|
|
51
|
+
const actual = isDate(new Date())
|
|
52
|
+
assert.isUndefined(actual)
|
|
53
|
+
})
|
|
54
|
+
})
|
|
36
55
|
describe('#referenceTypeMatch()', () => {
|
|
37
56
|
it('should allow a function for a model', () => {
|
|
38
57
|
const myModel = TestModel1.create()
|
|
@@ -85,6 +104,10 @@ describe('/src/validation.js', () => {
|
|
|
85
104
|
const actual = isRequired(1)
|
|
86
105
|
assert.isUndefined(actual)
|
|
87
106
|
})
|
|
107
|
+
it('should return undefined when a date is passed', () => {
|
|
108
|
+
const actual = isRequired(new Date())
|
|
109
|
+
assert.isUndefined(actual)
|
|
110
|
+
})
|
|
88
111
|
it('should return undefined when 0 is passed', () => {
|
|
89
112
|
const actual = isRequired(0)
|
|
90
113
|
assert.isUndefined(actual)
|