functional-models 1.0.21 → 1.0.23
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 +0 -6
- package/src/properties.js +2 -2
- package/src/validation.js +6 -6
package/package.json
CHANGED
package/src/models.js
CHANGED
|
@@ -5,7 +5,6 @@ const { createModelValidator } = require('./validation')
|
|
|
5
5
|
const { UniqueId } = require('./properties')
|
|
6
6
|
|
|
7
7
|
const MODEL_DEF_KEYS = ['meta', 'functions']
|
|
8
|
-
const PROTECTED_KEYS = ['model']
|
|
9
8
|
|
|
10
9
|
const Model = (
|
|
11
10
|
modelName,
|
|
@@ -33,11 +32,6 @@ const Model = (
|
|
|
33
32
|
[primaryKey]: getPrimaryKeyProperty(),
|
|
34
33
|
...keyToProperty,
|
|
35
34
|
}
|
|
36
|
-
PROTECTED_KEYS.forEach(key => {
|
|
37
|
-
if (key in keyToProperty) {
|
|
38
|
-
throw new Error(`Cannot use ${key}. This is a protected value.`)
|
|
39
|
-
}
|
|
40
|
-
})
|
|
41
35
|
const instanceProperties = Object.entries(keyToProperty).filter(
|
|
42
36
|
([key, _]) => MODEL_DEF_KEYS.includes(key) === false
|
|
43
37
|
)
|
package/src/properties.js
CHANGED
|
@@ -58,8 +58,8 @@ const Property = (config = {}, additionalMetadata = {}) => {
|
|
|
58
58
|
},
|
|
59
59
|
getValidator: valueGetter => {
|
|
60
60
|
const validator = createPropertyValidator(config)
|
|
61
|
-
const _propertyValidatorWrapper = async () => {
|
|
62
|
-
return validator(await valueGetter())
|
|
61
|
+
const _propertyValidatorWrapper = async (instance, instanceData) => {
|
|
62
|
+
return validator(await valueGetter(), instance, instanceData)
|
|
63
63
|
}
|
|
64
64
|
return _propertyValidatorWrapper
|
|
65
65
|
},
|
package/src/validation.js
CHANGED
|
@@ -208,11 +208,11 @@ const createPropertyValidator = config => {
|
|
|
208
208
|
: validators.includes(isRequired)
|
|
209
209
|
const validator =
|
|
210
210
|
validators.length > 0 ? aggregateValidator(validators) : emptyValidator
|
|
211
|
-
const _propertyValidator = async value => {
|
|
211
|
+
const _propertyValidator = async (value, instance, instanceData) => {
|
|
212
212
|
if (!value && !isRequiredValue) {
|
|
213
213
|
return []
|
|
214
214
|
}
|
|
215
|
-
const errors = await validator(value)
|
|
215
|
+
const errors = await validator(value, instance, instanceData)
|
|
216
216
|
return [...new Set(flatMap(errors))]
|
|
217
217
|
}
|
|
218
218
|
return _propertyValidator
|
|
@@ -223,17 +223,17 @@ const createModelValidator = (properties, modelValidators = []) => {
|
|
|
223
223
|
const keysAndFunctions = Object.entries(
|
|
224
224
|
get(properties, 'functions.validate', {})
|
|
225
225
|
)
|
|
226
|
+
const instanceData = await (modelValidators.length > 0
|
|
227
|
+
? instance.functions.toObj()
|
|
228
|
+
: {})
|
|
226
229
|
const data = await Promise.all(
|
|
227
230
|
keysAndFunctions.map(async ([key, validator]) => {
|
|
228
231
|
if (key === 'model') {
|
|
229
232
|
return [key, []]
|
|
230
233
|
}
|
|
231
|
-
return [key, await validator()]
|
|
234
|
+
return [key, await validator(instance, instanceData)]
|
|
232
235
|
})
|
|
233
236
|
)
|
|
234
|
-
const instanceData = await (modelValidators.length > 0
|
|
235
|
-
? instance.functions.toObj()
|
|
236
|
-
: {})
|
|
237
237
|
const modelValidationErrors = (
|
|
238
238
|
await Promise.all(
|
|
239
239
|
modelValidators.map(validator => validator(instance, instanceData))
|