functional-models 1.0.27 → 1.1.2
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/.eslintignore +1 -0
- package/.eslintrc +9 -15
- package/cucumber.js +10 -0
- package/dist/src/constants.d.ts +14 -0
- package/dist/src/constants.js +19 -0
- package/dist/src/constants.js.map +1 -0
- package/dist/src/errors.d.ts +9 -0
- package/dist/src/errors.js +15 -0
- package/dist/src/errors.js.map +1 -0
- package/dist/src/index.d.ts +9 -0
- package/dist/src/index.js +39 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/interfaces.d.ts +145 -0
- package/dist/src/interfaces.js +5 -0
- package/dist/src/interfaces.js.map +1 -0
- package/dist/src/lazy.d.ts +2 -0
- package/dist/src/lazy.js +37 -0
- package/dist/src/lazy.js.map +1 -0
- package/dist/src/methods.d.ts +4 -0
- package/dist/src/methods.js +18 -0
- package/dist/src/methods.js.map +1 -0
- package/dist/src/models.d.ts +3 -0
- package/dist/src/models.js +128 -0
- package/dist/src/models.js.map +1 -0
- package/dist/src/properties.d.ts +16 -0
- package/dist/src/properties.js +208 -0
- package/dist/src/properties.js.map +1 -0
- package/dist/src/serialization.d.ts +3 -0
- package/dist/src/serialization.js +49 -0
- package/dist/src/serialization.js.map +1 -0
- package/dist/src/utils.d.ts +4 -0
- package/dist/src/utils.js +44 -0
- package/dist/src/utils.js.map +1 -0
- package/dist/src/validation.d.ts +29 -0
- package/dist/src/validation.js +286 -0
- package/dist/src/validation.js.map +1 -0
- package/dist/stepDefinitions/oldSteps.d.ts +1 -0
- package/dist/stepDefinitions/oldSteps.js +191 -0
- package/dist/stepDefinitions/oldSteps.js.map +1 -0
- package/dist/stepDefinitions/tssteps.d.ts +1 -0
- package/dist/stepDefinitions/tssteps.js +96 -0
- package/dist/stepDefinitions/tssteps.js.map +1 -0
- package/dist/test/src/errors.test.d.ts +1 -0
- package/dist/test/src/errors.test.js +33 -0
- package/dist/test/src/errors.test.js.map +1 -0
- package/dist/test/src/lazy.test.d.ts +1 -0
- package/dist/test/src/lazy.test.js +28 -0
- package/dist/test/src/lazy.test.js.map +1 -0
- package/dist/test/src/methods.test.d.ts +1 -0
- package/dist/test/src/methods.test.js +48 -0
- package/dist/test/src/methods.test.js.map +1 -0
- package/dist/test/src/models.test.d.ts +1 -0
- package/dist/test/src/models.test.js +395 -0
- package/dist/test/src/models.test.js.map +1 -0
- package/dist/test/src/properties.test.d.ts +1 -0
- package/dist/test/src/properties.test.js +724 -0
- package/dist/test/src/properties.test.js.map +1 -0
- package/dist/test/src/serialization.test.d.ts +1 -0
- package/dist/test/src/serialization.test.js +91 -0
- package/dist/test/src/serialization.test.js.map +1 -0
- package/dist/test/src/utils.test.d.ts +1 -0
- package/dist/test/src/utils.test.js +81 -0
- package/dist/test/src/utils.test.js.map +1 -0
- package/dist/test/src/validation.test.d.ts +1 -0
- package/dist/test/src/validation.test.js +612 -0
- package/dist/test/src/validation.test.js.map +1 -0
- package/features/arrayFields.feature +7 -7
- package/features/basic-ts.feature +13 -0
- package/features/functions.feature +2 -3
- package/package.json +35 -10
- package/src/constants.ts +15 -0
- package/src/{errors.js → errors.ts} +6 -4
- package/src/index.ts +12 -0
- package/src/interfaces.ts +323 -0
- package/src/lazy.ts +24 -0
- package/src/methods.ts +30 -0
- package/src/models.ts +183 -0
- package/src/properties.ts +375 -0
- package/src/serialization.ts +39 -0
- package/src/{utils.js → utils.ts} +16 -26
- package/src/validation.ts +390 -0
- package/{features/stepDefinitions/steps.js → stepDefinitions/oldSteps.ts} +76 -53
- package/stepDefinitions/tssteps.ts +107 -0
- package/test/src/errors.test.ts +31 -0
- package/test/src/{lazy.test.js → lazy.test.ts} +4 -4
- package/test/src/methods.test.ts +45 -0
- package/test/src/models.test.ts +417 -0
- package/test/src/{properties.test.js → properties.test.ts} +251 -58
- package/test/src/serialization.test.ts +80 -0
- package/test/src/{utils.test.js → utils.test.ts} +29 -7
- package/test/src/{validation.test.js → validation.test.ts} +278 -210
- package/tsconfig.json +100 -0
- package/src/constants.js +0 -19
- package/src/functions.js +0 -7
- package/src/index.js +0 -10
- package/src/lazy.js +0 -19
- package/src/models.js +0 -152
- package/src/properties.js +0 -313
- package/src/serialization.js +0 -50
- package/src/validation.js +0 -285
- package/test/base/index.test.js +0 -5
- package/test/src/functions.test.js +0 -45
- package/test/src/index.test.js +0 -5
- package/test/src/models.test.js +0 -380
- package/test/src/serialization.test.js +0 -127
package/src/validation.js
DELETED
|
@@ -1,285 +0,0 @@
|
|
|
1
|
-
const isEmpty = require('lodash/isEmpty')
|
|
2
|
-
const merge = require('lodash/merge')
|
|
3
|
-
const isFunction = require('lodash/isFunction')
|
|
4
|
-
const flatMap = require('lodash/flatMap')
|
|
5
|
-
const get = require('lodash/get')
|
|
6
|
-
|
|
7
|
-
const TYPE_PRIMATIVES = {
|
|
8
|
-
boolean: 'boolean',
|
|
9
|
-
string: 'string',
|
|
10
|
-
object: 'object',
|
|
11
|
-
number: 'number',
|
|
12
|
-
integer: 'integer',
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
const _trueOrError = (method, error) => value => {
|
|
16
|
-
if (method(value) === false) {
|
|
17
|
-
return error
|
|
18
|
-
}
|
|
19
|
-
return undefined
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
const _typeOrError = (type, errorMessage) => value => {
|
|
23
|
-
if (typeof value !== type) {
|
|
24
|
-
return errorMessage
|
|
25
|
-
}
|
|
26
|
-
return undefined
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
const isType = type => value => {
|
|
30
|
-
return _typeOrError(type, `Must be a ${type}`)(value)
|
|
31
|
-
}
|
|
32
|
-
const isNumber = isType('number')
|
|
33
|
-
const isInteger = _trueOrError(Number.isInteger, 'Must be an integer')
|
|
34
|
-
|
|
35
|
-
const isBoolean = isType('boolean')
|
|
36
|
-
const isString = isType('string')
|
|
37
|
-
const isArray = _trueOrError(v => Array.isArray(v), 'Value is not an array')
|
|
38
|
-
|
|
39
|
-
const PRIMATIVE_TO_SPECIAL_TYPE_VALIDATOR = {
|
|
40
|
-
[TYPE_PRIMATIVES.boolean]: isBoolean,
|
|
41
|
-
[TYPE_PRIMATIVES.string]: isString,
|
|
42
|
-
[TYPE_PRIMATIVES.integer]: isInteger,
|
|
43
|
-
[TYPE_PRIMATIVES.number]: isNumber,
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
const arrayType = type => value => {
|
|
47
|
-
const arrayError = isArray(value)
|
|
48
|
-
if (arrayError) {
|
|
49
|
-
return arrayError
|
|
50
|
-
}
|
|
51
|
-
const validator = PRIMATIVE_TO_SPECIAL_TYPE_VALIDATOR[type] || isType(type)
|
|
52
|
-
return value.reduce((acc, v) => {
|
|
53
|
-
if (acc) {
|
|
54
|
-
return acc
|
|
55
|
-
}
|
|
56
|
-
return validator(v)
|
|
57
|
-
}, undefined)
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
const multiplePropertiesMustMatch = (getKeyA, getKeyB, errorMessage='Properties do not match') => async (_, instance) => {
|
|
61
|
-
const keyA = await getKeyA(instance)
|
|
62
|
-
const keyB = await getKeyB(instance)
|
|
63
|
-
if (keyA !== keyB) {
|
|
64
|
-
return errorMessage
|
|
65
|
-
}
|
|
66
|
-
return undefined
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
const meetsRegex =
|
|
70
|
-
(regex, flags, errorMessage = 'Format was invalid') =>
|
|
71
|
-
value => {
|
|
72
|
-
const reg = new RegExp(regex, flags)
|
|
73
|
-
return _trueOrError(v => reg.test(v), errorMessage)(value)
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
const choices = choiceArray => value => {
|
|
77
|
-
if (Array.isArray(value)) {
|
|
78
|
-
const bad = value.find(v => !choiceArray.includes(v))
|
|
79
|
-
if (bad) {
|
|
80
|
-
return `${bad} is not a valid choice`
|
|
81
|
-
}
|
|
82
|
-
} else {
|
|
83
|
-
if (choiceArray.includes(value) === false) {
|
|
84
|
-
return `${value} is not a valid choice`
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
return undefined
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
const isDate = value => {
|
|
91
|
-
if (!value) {
|
|
92
|
-
return 'Date value is empty'
|
|
93
|
-
}
|
|
94
|
-
if (!value.toISOString) {
|
|
95
|
-
return 'Value is not a date'
|
|
96
|
-
}
|
|
97
|
-
return undefined
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
const isRequired = value => {
|
|
101
|
-
if (value === true || value === false) {
|
|
102
|
-
return undefined
|
|
103
|
-
}
|
|
104
|
-
if (isNumber(value) === undefined) {
|
|
105
|
-
return undefined
|
|
106
|
-
}
|
|
107
|
-
const empty = isEmpty(value)
|
|
108
|
-
if (empty) {
|
|
109
|
-
if (isDate(value)) {
|
|
110
|
-
return 'A value is required'
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
return undefined
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
const maxNumber = max => value => {
|
|
117
|
-
const numberError = isNumber(value)
|
|
118
|
-
if (numberError) {
|
|
119
|
-
return numberError
|
|
120
|
-
}
|
|
121
|
-
if (value > max) {
|
|
122
|
-
return `The maximum is ${max}`
|
|
123
|
-
}
|
|
124
|
-
return undefined
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
const minNumber = min => value => {
|
|
128
|
-
const numberError = isNumber(value)
|
|
129
|
-
if (numberError) {
|
|
130
|
-
return numberError
|
|
131
|
-
}
|
|
132
|
-
if (value < min) {
|
|
133
|
-
return `The minimum is ${min}`
|
|
134
|
-
}
|
|
135
|
-
return undefined
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
const maxTextLength = max => value => {
|
|
139
|
-
const stringError = isString(value)
|
|
140
|
-
if (stringError) {
|
|
141
|
-
return stringError
|
|
142
|
-
}
|
|
143
|
-
if (value.length > max) {
|
|
144
|
-
return `The maximum length is ${max}`
|
|
145
|
-
}
|
|
146
|
-
return undefined
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
const minTextLength = min => value => {
|
|
150
|
-
const stringError = isString(value)
|
|
151
|
-
if (stringError) {
|
|
152
|
-
return stringError
|
|
153
|
-
}
|
|
154
|
-
if (value.length < min) {
|
|
155
|
-
return `The minimum length is ${min}`
|
|
156
|
-
}
|
|
157
|
-
return undefined
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
const referenceTypeMatch = referencedModel => {
|
|
161
|
-
return value => {
|
|
162
|
-
// This needs to stay here, as it delays the creation long enough for
|
|
163
|
-
// self referencing types.
|
|
164
|
-
const model = isFunction(referencedModel)
|
|
165
|
-
? referencedModel()
|
|
166
|
-
: referencedModel
|
|
167
|
-
// Assumption: By the time this is received, value === a model instance.
|
|
168
|
-
const eModel = model.getName()
|
|
169
|
-
const aModel = value.meta.getModel().getName()
|
|
170
|
-
if (eModel !== aModel) {
|
|
171
|
-
return `Model should be ${eModel} instead, recieved ${aModel}`
|
|
172
|
-
}
|
|
173
|
-
return undefined
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
const aggregateValidator = methodOrMethods => {
|
|
178
|
-
const toDo = Array.isArray(methodOrMethods)
|
|
179
|
-
? methodOrMethods
|
|
180
|
-
: [methodOrMethods]
|
|
181
|
-
|
|
182
|
-
const _aggregativeValidator = async (...args) => {
|
|
183
|
-
const values = await Promise.all(
|
|
184
|
-
toDo.map(method => {
|
|
185
|
-
return method(...args)
|
|
186
|
-
})
|
|
187
|
-
)
|
|
188
|
-
return values.filter(x => x)
|
|
189
|
-
}
|
|
190
|
-
return _aggregativeValidator
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
const emptyValidator = () => []
|
|
194
|
-
|
|
195
|
-
const _boolChoice = method => value => {
|
|
196
|
-
return value ? method : undefined
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
const CONFIG_TO_VALIDATE_METHOD = {
|
|
200
|
-
required: _boolChoice(isRequired),
|
|
201
|
-
isInteger: _boolChoice(isInteger),
|
|
202
|
-
isNumber: _boolChoice(isNumber),
|
|
203
|
-
isString: _boolChoice(isString),
|
|
204
|
-
isArray: _boolChoice(isArray),
|
|
205
|
-
isBoolean: _boolChoice(isBoolean),
|
|
206
|
-
choices,
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
const createPropertyValidator = config => {
|
|
210
|
-
const validators = [
|
|
211
|
-
...Object.entries(config).map(([key, value]) => {
|
|
212
|
-
return (CONFIG_TO_VALIDATE_METHOD[key] || (() => undefined))(value)
|
|
213
|
-
}),
|
|
214
|
-
...(config.validators ? config.validators : []),
|
|
215
|
-
].filter(x => x)
|
|
216
|
-
const isRequiredValue = config.required
|
|
217
|
-
? true
|
|
218
|
-
: validators.includes(isRequired)
|
|
219
|
-
const validator =
|
|
220
|
-
validators.length > 0 ? aggregateValidator(validators) : emptyValidator
|
|
221
|
-
const _propertyValidator = async (value, instance, instanceData={}, options) => {
|
|
222
|
-
if (!value && !isRequiredValue) {
|
|
223
|
-
return []
|
|
224
|
-
}
|
|
225
|
-
const errors = await validator(value, instance, instanceData, options)
|
|
226
|
-
return [...new Set(flatMap(errors))]
|
|
227
|
-
}
|
|
228
|
-
return _propertyValidator
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
const createModelValidator = (properties, modelValidators = []) => {
|
|
232
|
-
const _modelValidator = async (instance, options) => {
|
|
233
|
-
if (!instance) {
|
|
234
|
-
throw new Error(`Instance cannot be empty`)
|
|
235
|
-
}
|
|
236
|
-
const keysAndFunctions = Object.entries(
|
|
237
|
-
get(properties, 'functions.validators', {})
|
|
238
|
-
)
|
|
239
|
-
const instanceData = await instance.functions.toObj()
|
|
240
|
-
const propertyValidationErrors = await Promise.all(
|
|
241
|
-
keysAndFunctions.map(async ([key, validator]) => {
|
|
242
|
-
return [key, await validator(instance, instanceData, options)]
|
|
243
|
-
})
|
|
244
|
-
)
|
|
245
|
-
const modelValidationErrors = (
|
|
246
|
-
await Promise.all(
|
|
247
|
-
modelValidators.map(validator => validator(instance, instanceData, options))
|
|
248
|
-
)
|
|
249
|
-
).filter(x => x)
|
|
250
|
-
const propertyErrors = propertyValidationErrors
|
|
251
|
-
.filter(([_, errors]) => Boolean(errors) && errors.length > 0)
|
|
252
|
-
.reduce((acc, [key, errors]) => {
|
|
253
|
-
return { ...acc, [key]: errors }
|
|
254
|
-
}, {})
|
|
255
|
-
return modelValidationErrors.length > 0
|
|
256
|
-
? merge(propertyErrors, { overall: modelValidationErrors })
|
|
257
|
-
: propertyErrors
|
|
258
|
-
}
|
|
259
|
-
return _modelValidator
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
module.exports = {
|
|
263
|
-
isNumber,
|
|
264
|
-
isBoolean,
|
|
265
|
-
isString,
|
|
266
|
-
isInteger,
|
|
267
|
-
isType,
|
|
268
|
-
isDate,
|
|
269
|
-
isArray,
|
|
270
|
-
isRequired,
|
|
271
|
-
maxNumber,
|
|
272
|
-
minNumber,
|
|
273
|
-
choices,
|
|
274
|
-
maxTextLength,
|
|
275
|
-
minTextLength,
|
|
276
|
-
meetsRegex,
|
|
277
|
-
aggregateValidator,
|
|
278
|
-
emptyValidator,
|
|
279
|
-
createPropertyValidator,
|
|
280
|
-
createModelValidator,
|
|
281
|
-
arrayType,
|
|
282
|
-
referenceTypeMatch,
|
|
283
|
-
multiplePropertiesMustMatch,
|
|
284
|
-
TYPE_PRIMATIVES,
|
|
285
|
-
}
|
package/test/base/index.test.js
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
const assert = require('chai').assert
|
|
2
|
-
const sinon = require('sinon')
|
|
3
|
-
const { Function } = require('../../src/functions')
|
|
4
|
-
|
|
5
|
-
describe('/src/functions.js', () => {
|
|
6
|
-
describe('#Function()', () => {
|
|
7
|
-
it('should return "Hello-world" when passed in', () => {
|
|
8
|
-
const method = sinon.stub().callsFake(input => {
|
|
9
|
-
return `${input}-world`
|
|
10
|
-
})
|
|
11
|
-
const myFunction = Function(method)
|
|
12
|
-
const wrappedObj = 'Hello'
|
|
13
|
-
const wrappedFunc = myFunction(wrappedObj)
|
|
14
|
-
const actual = wrappedFunc()
|
|
15
|
-
const expected = 'Hello-world'
|
|
16
|
-
assert.equal(actual, expected)
|
|
17
|
-
})
|
|
18
|
-
it('should call the method when Function()()() called', () => {
|
|
19
|
-
const method = sinon.stub().callsFake(input => {
|
|
20
|
-
return `${input}-world`
|
|
21
|
-
})
|
|
22
|
-
const myFunction = Function(method)
|
|
23
|
-
const wrappedObj = 'Hello'
|
|
24
|
-
const wrappedFunc = myFunction(wrappedObj)
|
|
25
|
-
const result = wrappedFunc()
|
|
26
|
-
sinon.assert.calledOnce(method)
|
|
27
|
-
})
|
|
28
|
-
it('should not call the method when Function()() called', () => {
|
|
29
|
-
const method = sinon.stub().callsFake(input => {
|
|
30
|
-
return `${input}-world`
|
|
31
|
-
})
|
|
32
|
-
const myFunction = Function(method)
|
|
33
|
-
const wrappedObj = 'Hello'
|
|
34
|
-
const wrappedFunc = myFunction(wrappedObj)
|
|
35
|
-
sinon.assert.notCalled(method)
|
|
36
|
-
})
|
|
37
|
-
it('should not call the method when Function() called', () => {
|
|
38
|
-
const method = sinon.stub().callsFake(input => {
|
|
39
|
-
return `${input}-world`
|
|
40
|
-
})
|
|
41
|
-
const myFunction = Function(method)
|
|
42
|
-
sinon.assert.notCalled(method)
|
|
43
|
-
})
|
|
44
|
-
})
|
|
45
|
-
})
|