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.
Files changed (105) hide show
  1. package/.eslintignore +1 -0
  2. package/.eslintrc +9 -15
  3. package/cucumber.js +10 -0
  4. package/dist/src/constants.d.ts +14 -0
  5. package/dist/src/constants.js +19 -0
  6. package/dist/src/constants.js.map +1 -0
  7. package/dist/src/errors.d.ts +9 -0
  8. package/dist/src/errors.js +15 -0
  9. package/dist/src/errors.js.map +1 -0
  10. package/dist/src/index.d.ts +9 -0
  11. package/dist/src/index.js +39 -0
  12. package/dist/src/index.js.map +1 -0
  13. package/dist/src/interfaces.d.ts +145 -0
  14. package/dist/src/interfaces.js +5 -0
  15. package/dist/src/interfaces.js.map +1 -0
  16. package/dist/src/lazy.d.ts +2 -0
  17. package/dist/src/lazy.js +37 -0
  18. package/dist/src/lazy.js.map +1 -0
  19. package/dist/src/methods.d.ts +4 -0
  20. package/dist/src/methods.js +18 -0
  21. package/dist/src/methods.js.map +1 -0
  22. package/dist/src/models.d.ts +3 -0
  23. package/dist/src/models.js +128 -0
  24. package/dist/src/models.js.map +1 -0
  25. package/dist/src/properties.d.ts +16 -0
  26. package/dist/src/properties.js +208 -0
  27. package/dist/src/properties.js.map +1 -0
  28. package/dist/src/serialization.d.ts +3 -0
  29. package/dist/src/serialization.js +49 -0
  30. package/dist/src/serialization.js.map +1 -0
  31. package/dist/src/utils.d.ts +4 -0
  32. package/dist/src/utils.js +44 -0
  33. package/dist/src/utils.js.map +1 -0
  34. package/dist/src/validation.d.ts +29 -0
  35. package/dist/src/validation.js +286 -0
  36. package/dist/src/validation.js.map +1 -0
  37. package/dist/stepDefinitions/oldSteps.d.ts +1 -0
  38. package/dist/stepDefinitions/oldSteps.js +191 -0
  39. package/dist/stepDefinitions/oldSteps.js.map +1 -0
  40. package/dist/stepDefinitions/tssteps.d.ts +1 -0
  41. package/dist/stepDefinitions/tssteps.js +96 -0
  42. package/dist/stepDefinitions/tssteps.js.map +1 -0
  43. package/dist/test/src/errors.test.d.ts +1 -0
  44. package/dist/test/src/errors.test.js +33 -0
  45. package/dist/test/src/errors.test.js.map +1 -0
  46. package/dist/test/src/lazy.test.d.ts +1 -0
  47. package/dist/test/src/lazy.test.js +28 -0
  48. package/dist/test/src/lazy.test.js.map +1 -0
  49. package/dist/test/src/methods.test.d.ts +1 -0
  50. package/dist/test/src/methods.test.js +48 -0
  51. package/dist/test/src/methods.test.js.map +1 -0
  52. package/dist/test/src/models.test.d.ts +1 -0
  53. package/dist/test/src/models.test.js +395 -0
  54. package/dist/test/src/models.test.js.map +1 -0
  55. package/dist/test/src/properties.test.d.ts +1 -0
  56. package/dist/test/src/properties.test.js +724 -0
  57. package/dist/test/src/properties.test.js.map +1 -0
  58. package/dist/test/src/serialization.test.d.ts +1 -0
  59. package/dist/test/src/serialization.test.js +91 -0
  60. package/dist/test/src/serialization.test.js.map +1 -0
  61. package/dist/test/src/utils.test.d.ts +1 -0
  62. package/dist/test/src/utils.test.js +81 -0
  63. package/dist/test/src/utils.test.js.map +1 -0
  64. package/dist/test/src/validation.test.d.ts +1 -0
  65. package/dist/test/src/validation.test.js +612 -0
  66. package/dist/test/src/validation.test.js.map +1 -0
  67. package/features/arrayFields.feature +7 -7
  68. package/features/basic-ts.feature +13 -0
  69. package/features/functions.feature +2 -3
  70. package/package.json +35 -10
  71. package/src/constants.ts +15 -0
  72. package/src/{errors.js → errors.ts} +6 -4
  73. package/src/index.ts +12 -0
  74. package/src/interfaces.ts +323 -0
  75. package/src/lazy.ts +24 -0
  76. package/src/methods.ts +30 -0
  77. package/src/models.ts +183 -0
  78. package/src/properties.ts +375 -0
  79. package/src/serialization.ts +39 -0
  80. package/src/{utils.js → utils.ts} +16 -26
  81. package/src/validation.ts +390 -0
  82. package/{features/stepDefinitions/steps.js → stepDefinitions/oldSteps.ts} +76 -53
  83. package/stepDefinitions/tssteps.ts +107 -0
  84. package/test/src/errors.test.ts +31 -0
  85. package/test/src/{lazy.test.js → lazy.test.ts} +4 -4
  86. package/test/src/methods.test.ts +45 -0
  87. package/test/src/models.test.ts +417 -0
  88. package/test/src/{properties.test.js → properties.test.ts} +251 -58
  89. package/test/src/serialization.test.ts +80 -0
  90. package/test/src/{utils.test.js → utils.test.ts} +29 -7
  91. package/test/src/{validation.test.js → validation.test.ts} +278 -210
  92. package/tsconfig.json +100 -0
  93. package/src/constants.js +0 -19
  94. package/src/functions.js +0 -7
  95. package/src/index.js +0 -10
  96. package/src/lazy.js +0 -19
  97. package/src/models.js +0 -152
  98. package/src/properties.js +0 -313
  99. package/src/serialization.js +0 -50
  100. package/src/validation.js +0 -285
  101. package/test/base/index.test.js +0 -5
  102. package/test/src/functions.test.js +0 -45
  103. package/test/src/index.test.js +0 -5
  104. package/test/src/models.test.js +0 -380
  105. package/test/src/serialization.test.js +0 -127
@@ -1,11 +1,10 @@
1
- const chai = require('chai')
2
- const assert = chai.assert
3
- const chaiAsPromised = require('chai-as-promised')
1
+ import chai from 'chai'
2
+ import chaiAsPromised from 'chai-as-promised'
4
3
  chai.use(chaiAsPromised)
5
- const sinon = require('sinon')
6
- const { Model } = require('../../src/models')
7
- const { UniqueId, TextProperty } = require('../../src/properties')
8
- const {
4
+ import sinon from 'sinon'
5
+ import { BaseModel } from '../../src/models'
6
+ import { TextProperty } from '../../src/properties'
7
+ import {
9
8
  isNumber,
10
9
  isBoolean,
11
10
  isInteger,
@@ -25,236 +24,289 @@ const {
25
24
  emptyValidator,
26
25
  createModelValidator,
27
26
  createPropertyValidator,
28
- TYPE_PRIMATIVES,
29
- } = require('../../src/validation')
27
+ TYPE_PRIMITIVES,
28
+ } from '../../src/validation'
29
+ import { ModelComponentValidator } from '../../src/interfaces'
30
+
31
+ const assert = chai.assert
30
32
 
31
- const TestModel1 = Model('TestModel1', {
32
- id: UniqueId(),
33
+ const TestModel1 = BaseModel('TestModel1', {
34
+ properties: {},
33
35
  })
34
36
 
35
- const TestModel2 = Model('TestModel2', {
36
- id: UniqueId(),
37
+ const TestModel2 = BaseModel('TestModel2', {
38
+ properties: {},
37
39
  })
38
40
 
39
- const createTestModel3 = modelValidators =>
40
- Model(
41
- 'TestModel3',
42
- {
43
- id: UniqueId(),
41
+ const createTestModel3 = (modelValidators: ModelComponentValidator[]) =>
42
+ BaseModel<{ name: string }>('TestModel3', {
43
+ properties: {
44
44
  name: TextProperty(),
45
45
  },
46
- {
47
- modelValidators,
48
- }
49
- )
46
+ modelValidators,
47
+ })
48
+
49
+ type EMPTY_MODEL_TYPE = {}
50
+ const EMPTY_MODEL = BaseModel<EMPTY_MODEL_TYPE>('EmptyModel', {
51
+ properties: {},
52
+ })
53
+ const EMPTY_MODEL_INSTANCE = EMPTY_MODEL.create({})
50
54
 
51
- describe('/src/validation.js', () => {
55
+ describe('/src/validation.ts', () => {
52
56
  describe('#isDate()', () => {
53
57
  it('should return an error if value is null', () => {
54
- const actual = isDate(null)
58
+ // @ts-ignore
59
+ const actual = isDate(null, EMPTY_MODEL_INSTANCE, {})
55
60
  assert.isOk(actual)
56
61
  })
57
62
  it('should return an error if value is undefined', () => {
58
- const actual = isDate(undefined)
63
+ // @ts-ignore
64
+ const actual = isDate(undefined, EMPTY_MODEL_INSTANCE, {})
59
65
  assert.isOk(actual)
60
66
  })
61
67
  it('should return an error object does not have toISOString', () => {
62
- const actual = isDate({})
68
+ // @ts-ignore
69
+ const actual = isDate({}, EMPTY_MODEL_INSTANCE, {})
63
70
  assert.isOk(actual)
64
71
  })
65
72
  it('should return undefined if a date', () => {
66
- const actual = isDate(new Date())
73
+ const actual = isDate(new Date(), EMPTY_MODEL_INSTANCE, {})
67
74
  assert.isUndefined(actual)
68
75
  })
69
76
  })
70
77
  describe('#referenceTypeMatch()', () => {
71
- it('should allow a function for a model', () => {
72
- const myModel = TestModel1.create()
73
- const actual = referenceTypeMatch(() => TestModel1)(myModel)
78
+ it('should return an error if undefined is passed as a value', () => {
79
+ const myModel = TestModel1.create({})
80
+ const actual = referenceTypeMatch(TestModel1)(
81
+ // @ts-ignore
82
+ undefined,
83
+ EMPTY_MODEL_INSTANCE,
84
+ {}
85
+ )
86
+ assert.isOk(actual)
87
+ })
88
+ it('should return an error if null is passed as a value', () => {
89
+ const myModel = TestModel1.create({})
90
+ const actual = referenceTypeMatch(TestModel1)(
91
+ // @ts-ignore
92
+ null,
93
+ EMPTY_MODEL_INSTANCE,
94
+ {}
95
+ )
96
+ assert.isOk(actual)
97
+ })
98
+ it('should allow a function for a model', async () => {
99
+ const myModel = EMPTY_MODEL.create({})
100
+ const actual = referenceTypeMatch<EMPTY_MODEL_TYPE>(() => EMPTY_MODEL)(
101
+ myModel,
102
+ myModel,
103
+ await myModel.toObj()
104
+ )
74
105
  const expected = undefined
75
106
  assert.equal(actual, expected)
76
107
  })
77
- it('should validate when the correct object matches the model', () => {
78
- const myModel = TestModel1.create()
79
- const actual = referenceTypeMatch(TestModel1)(myModel)
108
+ it('should validate when the correct object matches the model', async () => {
109
+ const myModel = EMPTY_MODEL.create({})
110
+ const actual = referenceTypeMatch<EMPTY_MODEL_TYPE>(EMPTY_MODEL)(
111
+ myModel,
112
+ myModel,
113
+ await myModel.toObj()
114
+ )
80
115
  const expected = undefined
81
116
  assert.equal(actual, expected)
82
117
  })
83
118
  it('should return an error when the input does not match the model', () => {
84
- const myModel = TestModel2.create()
85
- const actual = referenceTypeMatch(TestModel1)(myModel)
119
+ const myModel = EMPTY_MODEL.create({})
120
+ const actual = referenceTypeMatch(TestModel1)(
121
+ // @ts-ignore
122
+ myModel,
123
+ myModel,
124
+ {}
125
+ )
86
126
  assert.isOk(actual)
87
127
  })
88
128
  })
89
129
  describe('#isNumber()', () => {
90
130
  it('should return an error when empty is passed', () => {
91
- const actual = isNumber(null)
131
+ const actual = isNumber(null, EMPTY_MODEL_INSTANCE, {})
92
132
  assert.isOk(actual)
93
133
  })
94
134
  it('should return an error when "asdf" is passed', () => {
95
- const actual = isNumber('asdf')
135
+ const actual = isNumber('asdf', EMPTY_MODEL_INSTANCE, {})
96
136
  assert.isOk(actual)
97
137
  })
98
138
  it('should return undefined when 1 is passed', () => {
99
- const actual = isNumber(1)
139
+ const actual = isNumber(1, EMPTY_MODEL_INSTANCE, {})
100
140
  assert.isUndefined(actual)
101
141
  })
102
142
  it('should return error when "1" is passed', () => {
103
- const actual = isNumber('1')
143
+ const actual = isNumber('1', EMPTY_MODEL_INSTANCE, {})
104
144
  assert.isOk(actual)
105
145
  })
106
146
  })
107
147
  describe('#isString()', () => {
108
148
  it('should return undefined when "1" is passed', () => {
109
- const actual = isString('1')
149
+ const actual = isString('1', EMPTY_MODEL_INSTANCE, {})
110
150
  assert.isUndefined(actual)
111
151
  })
112
152
  it('should return error when 1 is passed', () => {
113
- const actual = isString(1)
153
+ const actual = isString(1, EMPTY_MODEL_INSTANCE, {})
114
154
  assert.isOk(actual)
115
155
  })
116
156
  })
117
157
  describe('#isRequired()', () => {
118
158
  it('should return undefined when 1 is passed', () => {
119
- const actual = isRequired(1)
159
+ const actual = isRequired(1, EMPTY_MODEL_INSTANCE, {})
120
160
  assert.isUndefined(actual)
121
161
  })
122
162
  it('should return undefined when a date is passed', () => {
123
- const actual = isRequired(new Date())
163
+ const actual = isRequired(new Date(), EMPTY_MODEL_INSTANCE, {})
124
164
  assert.isUndefined(actual)
125
165
  })
126
166
  it('should return undefined when 0 is passed', () => {
127
- const actual = isRequired(0)
167
+ const actual = isRequired(0, EMPTY_MODEL_INSTANCE, {})
128
168
  assert.isUndefined(actual)
129
169
  })
130
170
  it('should return undefined when "something" is passed', () => {
131
- const actual = isRequired('something')
171
+ const actual = isRequired('something', EMPTY_MODEL_INSTANCE, {})
132
172
  assert.isUndefined(actual)
133
173
  })
134
174
  it('should return error when null is passed', () => {
135
- const actual = isRequired(null)
175
+ const actual = isRequired(null, EMPTY_MODEL_INSTANCE, {})
136
176
  assert.isOk(actual)
137
177
  })
138
178
  it('should return error when undefined is passed', () => {
139
- const actual = isRequired(undefined)
179
+ const actual = isRequired(undefined, EMPTY_MODEL_INSTANCE, {})
140
180
  assert.isOk(actual)
141
181
  })
142
182
  it('should return undefined when false is passed', () => {
143
- const actual = isRequired(false)
183
+ const actual = isRequired(false, EMPTY_MODEL_INSTANCE, {})
144
184
  assert.isUndefined(actual)
145
185
  })
146
186
  it('should return undefined when true is passed', () => {
147
- const actual = isRequired(true)
187
+ const actual = isRequired(true, EMPTY_MODEL_INSTANCE, {})
148
188
  assert.isUndefined(actual)
149
189
  })
150
190
  })
151
191
  describe('#isBoolean()', () => {
152
192
  it('should return error when "true" is passed"', () => {
153
- const actual = isBoolean('true')
193
+ const actual = isBoolean('true', EMPTY_MODEL_INSTANCE, {})
154
194
  assert.isOk(actual)
155
195
  })
156
196
  it('should return an error when "false" is passed', () => {
157
- const actual = isBoolean('false')
197
+ const actual = isBoolean('false', EMPTY_MODEL_INSTANCE, {})
158
198
  assert.isOk(actual)
159
199
  })
160
200
  it('should return undefined when true is passed"', () => {
161
- const actual = isBoolean(true)
201
+ const actual = isBoolean(true, EMPTY_MODEL_INSTANCE, {})
162
202
  assert.isUndefined(actual)
163
203
  })
164
204
  it('should return undefined when false is passed', () => {
165
- const actual = isBoolean(false)
205
+ const actual = isBoolean(false, EMPTY_MODEL_INSTANCE, {})
166
206
  assert.isUndefined(actual)
167
207
  })
168
208
  })
169
209
  describe('#maxNumber()', () => {
170
210
  it('should return error if max=5 and value="hello world"', () => {
171
- const actual = maxNumber(5)('hello world')
211
+ // @ts-ignore
212
+ const actual = maxNumber(5)('hello world', EMPTY_MODEL_INSTANCE, {})
172
213
  assert.isOk(actual)
173
214
  })
174
215
  it('should return error if max=5 and value=6', () => {
175
- const actual = maxNumber(5)(6)
216
+ const actual = maxNumber(5)(6, EMPTY_MODEL_INSTANCE, {})
176
217
  assert.isOk(actual)
177
218
  })
178
219
  it('should return undefined if max=5 and value=5', () => {
179
- const actual = maxNumber(5)(5)
220
+ const actual = maxNumber(5)(5, EMPTY_MODEL_INSTANCE, {})
180
221
  assert.isUndefined(actual)
181
222
  })
182
223
  it('should return undefined if max=5 and value=4', () => {
183
- const actual = maxNumber(5)(4)
224
+ const actual = maxNumber(5)(4, EMPTY_MODEL_INSTANCE, {})
184
225
  assert.isUndefined(actual)
185
226
  })
186
227
  })
187
228
  describe('#minNumber()', () => {
188
229
  it('should return error if min=5 and value="hello world"', () => {
189
- const actual = minNumber(5)('hello world')
230
+ // @ts-ignore
231
+ const actual = minNumber(5)('hello world', EMPTY_MODEL_INSTANCE, {})
190
232
  assert.isOk(actual)
191
233
  })
192
234
  it('should return error if min=5 and value=4', () => {
193
- const actual = minNumber(5)(4)
235
+ const actual = minNumber(5)(4, EMPTY_MODEL_INSTANCE, {})
194
236
  assert.isOk(actual)
195
237
  })
196
238
  it('should return undefined if min=5 and value=4', () => {
197
- const actual = minNumber(5)(5)
239
+ const actual = minNumber(5)(5, EMPTY_MODEL_INSTANCE, {})
198
240
  assert.isUndefined(actual)
199
241
  })
200
242
  it('should return undefined if min=5 and value=6', () => {
201
- const actual = minNumber(5)(6)
243
+ const actual = minNumber(5)(6, EMPTY_MODEL_INSTANCE, {})
202
244
  assert.isUndefined(actual)
203
245
  })
204
246
  })
205
247
  describe('#choices()', () => {
206
248
  it('should return an error if choices are [1,2,3] and value is 4', () => {
207
- const actual = choices([1, 2, 3])(4)
249
+ const actual = choices(['1', '2', '3'])('4', EMPTY_MODEL_INSTANCE, {})
208
250
  assert.isOk(actual)
209
251
  })
210
252
  it('should return undefined if choices are [1,2,3] and value is 1', () => {
211
- const actual = choices([1, 2, 3])(1)
253
+ const actual = choices(['1', '2', '3'])('1', EMPTY_MODEL_INSTANCE, {})
212
254
  assert.isUndefined(actual)
213
255
  })
214
256
  })
215
257
  describe('#minTextLength()', () => {
216
258
  it('should return error if min=5 and value=5', () => {
217
- const actual = minTextLength(5)(5)
259
+ // @ts-ignore
260
+ const actual = minTextLength(5)(5, EMPTY_MODEL_INSTANCE, {})
261
+ assert.isOk(actual)
262
+ })
263
+ it('should return error if min=5 and value=5', () => {
264
+ const actual = minTextLength(5)('5', EMPTY_MODEL_INSTANCE, {})
218
265
  assert.isOk(actual)
219
266
  })
220
267
  it('should return error if length=5 and value="asdf"', () => {
221
- const actual = minTextLength(5)('asdf')
268
+ const actual = minTextLength(5)('asdf', EMPTY_MODEL_INSTANCE, {})
222
269
  assert.isOk(actual)
223
270
  })
224
271
  it('should return undefined if length=5 and value="hello"', () => {
225
- const actual = minTextLength(5)('hello')
272
+ const actual = minTextLength(5)('hello', EMPTY_MODEL_INSTANCE, {})
226
273
  assert.isUndefined(actual)
227
274
  })
228
275
  it('should return undefined if length=5 and value="hello world"', () => {
229
- const actual = minTextLength(5)('hello world')
276
+ const actual = minTextLength(5)('hello world', EMPTY_MODEL_INSTANCE, {})
230
277
  assert.isUndefined(actual)
231
278
  })
232
279
  })
233
280
  describe('#maxTextLength()', () => {
234
- it('should return error if max=5 and value=5', () => {
235
- const actual = maxTextLength(5)(5)
281
+ it('should return error if length=5 and value=5', () => {
282
+ // @ts-ignore
283
+ const actual = maxTextLength(5)(5, EMPTY_MODEL_INSTANCE, {})
236
284
  assert.isOk(actual)
237
285
  })
238
286
  it('should return error if length=5 and value="hello world"', () => {
239
- const actual = maxTextLength(5)('hello world')
287
+ const actual = maxTextLength(5)('hello world', EMPTY_MODEL_INSTANCE, {})
240
288
  assert.isOk(actual)
241
289
  })
242
290
  it('should return undefined if length=5 and value="hello"', () => {
243
- const actual = maxTextLength(5)('hello')
291
+ const actual = maxTextLength(5)('hello', EMPTY_MODEL_INSTANCE, {})
244
292
  assert.isUndefined(actual)
245
293
  })
246
294
  it('should return undefined if length=5 and value="asdf"', () => {
247
- const actual = maxTextLength(5)('asdf')
295
+ const actual = maxTextLength(5)('asdf', EMPTY_MODEL_INSTANCE, {})
248
296
  assert.isUndefined(actual)
249
297
  })
250
298
  })
251
299
  describe('#meetsRegex()', () => {
252
300
  it('should return an error with regex=/asdf/ flags="g" and value="hello world"', () => {
253
- const actual = meetsRegex(/asdf/, 'g')('hello world')
301
+ const actual = meetsRegex(/asdf/, 'g')(
302
+ 'hello world',
303
+ EMPTY_MODEL_INSTANCE,
304
+ {}
305
+ )
254
306
  assert.isOk(actual)
255
307
  })
256
308
  it('should return undefined with regex=/asdf/ flags="g" and value="hello asdf world"', () => {
257
- const actual = meetsRegex(/asdf/, 'g')('asdf')
309
+ const actual = meetsRegex(/asdf/, 'g')('asdf', EMPTY_MODEL_INSTANCE, {})
258
310
  assert.isUndefined(actual)
259
311
  })
260
312
  })
@@ -262,52 +314,56 @@ describe('/src/validation.js', () => {
262
314
  it('should return two errors when two validators are passed, and the value fails both', async () => {
263
315
  const validators = [minTextLength(10), isNumber]
264
316
  const value = 'asdf'
265
- const actual = (await aggregateValidator(validators)('asdf')).length
317
+ const actual = (
318
+ await aggregateValidator(value, validators)(EMPTY_MODEL_INSTANCE, {})
319
+ ).length
266
320
  const expected = 2
267
321
  assert.equal(actual, expected)
268
322
  })
269
323
  it('should return one error when one validator is passed, and the value fails', async () => {
270
324
  const validators = minTextLength(10)
271
325
  const value = 'asdf'
272
- const actual = (await aggregateValidator(validators)('asdf')).length
326
+ const actual = (
327
+ await aggregateValidator(value, validators)(EMPTY_MODEL_INSTANCE, {})
328
+ ).length
273
329
  const expected = 1
274
330
  assert.equal(actual, expected)
275
331
  })
276
332
  })
277
333
  describe('#emptyValidator()', () => {
278
- it('should return an empty array with a value of 1', () => {
279
- const actual = emptyValidator(1).length
280
- const expected = 0
334
+ it('should return undefined with a value of 1', () => {
335
+ const actual = emptyValidator(1, EMPTY_MODEL_INSTANCE, {})
336
+ const expected = undefined
281
337
  assert.equal(actual, expected)
282
338
  })
283
- it('should return an empty array with a value of "1"', () => {
284
- const actual = emptyValidator('1').length
285
- const expected = 0
339
+ it('should return undefined with a value of "1"', () => {
340
+ const actual = emptyValidator('1', EMPTY_MODEL_INSTANCE, {})
341
+ const expected = undefined
286
342
  assert.equal(actual, expected)
287
343
  })
288
- it('should return an empty array with a value of true', () => {
289
- const actual = emptyValidator(true).length
290
- const expected = 0
344
+ it('should return undefined with a value of true', () => {
345
+ const actual = emptyValidator(true, EMPTY_MODEL_INSTANCE, {})
346
+ const expected = undefined
291
347
  assert.equal(actual, expected)
292
348
  })
293
- it('should return an empty array with a value of false', () => {
294
- const actual = emptyValidator(false).length
295
- const expected = 0
349
+ it('should return undefined with a value of false', () => {
350
+ const actual = emptyValidator(false, EMPTY_MODEL_INSTANCE, {})
351
+ const expected = undefined
296
352
  assert.equal(actual, expected)
297
353
  })
298
- it('should return an empty array with a value of undefined', () => {
299
- const actual = emptyValidator(undefined).length
300
- const expected = 0
354
+ it('should return undefined with a value of undefined', () => {
355
+ const actual = emptyValidator(undefined, EMPTY_MODEL_INSTANCE, {})
356
+ const expected = undefined
301
357
  assert.equal(actual, expected)
302
358
  })
303
359
  })
304
360
  describe('#isInteger()', () => {
305
361
  it('should return an error with a value of "1"', () => {
306
- const actual = isInteger('1')
362
+ const actual = isInteger('1', EMPTY_MODEL_INSTANCE, {})
307
363
  assert.isOk(actual)
308
364
  })
309
365
  it('should return undefined with a value of 1', () => {
310
- const actual = isInteger(1)
366
+ const actual = isInteger(1, EMPTY_MODEL_INSTANCE, {})
311
367
  assert.isUndefined(actual)
312
368
  })
313
369
  })
@@ -315,33 +371,30 @@ describe('/src/validation.js', () => {
315
371
  it('should throw an exception if instance is null', () => {
316
372
  const modelValidator = sinon.stub().returns(undefined)
317
373
  const properties = {
318
- functions: {
319
- validators: {
320
- id: sinon.stub().returns(undefined),
321
- name: sinon.stub().returns(undefined),
322
- },
323
- },
374
+ id: sinon.stub().returns(undefined),
375
+ name: sinon.stub().returns(undefined),
324
376
  }
325
377
  const validator = createModelValidator(properties, [modelValidator])
326
- assert.isRejected(validator())
378
+ // @ts-ignore
379
+ assert.isRejected(validator(undefined, {}))
327
380
  })
328
381
  it('should call the model validator passed in', async () => {
329
382
  const modelValidator = sinon.stub().returns(undefined)
330
383
  const testModel3 = createTestModel3([modelValidator])
331
384
  const properties = {
332
- functions: {
333
- validators: {
334
- id: sinon.stub().returns(undefined),
335
- name: sinon.stub().returns(undefined),
336
- },
337
- },
385
+ id: sinon.stub().returns(undefined),
386
+ name: sinon.stub().returns(undefined),
338
387
  }
339
388
  const validator = createModelValidator(properties, [modelValidator])
340
389
  await validator(
341
390
  testModel3.create({
342
391
  id: 'test-id',
343
392
  name: 'my-name',
344
- })
393
+ }),
394
+ {
395
+ id: 'test-id',
396
+ name: 'my-name',
397
+ }
345
398
  )
346
399
  sinon.assert.calledOnce(modelValidator)
347
400
  })
@@ -349,19 +402,15 @@ describe('/src/validation.js', () => {
349
402
  const modelValidator = sinon.stub().returns(undefined)
350
403
  const testModel3 = createTestModel3([modelValidator])
351
404
  const properties = {
352
- functions: {
353
- validators: {
354
- id: sinon.stub().returns(undefined),
355
- name: sinon.stub().returns(undefined),
356
- },
357
- },
405
+ id: sinon.stub().returns(undefined),
406
+ name: sinon.stub().returns(undefined),
358
407
  }
359
408
  const validator = createModelValidator(properties, [modelValidator])
360
409
  const instance = testModel3.create({
361
410
  id: 'test-id',
362
411
  name: 'my-name',
363
412
  })
364
- await validator(instance)
413
+ await validator(instance, {})
365
414
 
366
415
  const actual = modelValidator.getCall(0).args[0]
367
416
  const expected = instance
@@ -371,20 +420,16 @@ describe('/src/validation.js', () => {
371
420
  const modelValidator = sinon.stub().returns(undefined)
372
421
  const testModel3 = createTestModel3([modelValidator])
373
422
  const properties = {
374
- functions: {
375
- validators: {
376
- id: sinon.stub().returns(undefined),
377
- name: sinon.stub().returns(undefined),
378
- },
379
- },
423
+ id: sinon.stub().returns(undefined),
424
+ name: sinon.stub().returns(undefined),
380
425
  }
381
426
  const validator = createModelValidator(properties, [modelValidator])
382
427
  const instance = testModel3.create({
383
428
  id: 'test-id',
384
429
  name: 'my-name',
385
430
  })
386
- const expected = await instance.functions.toObj()
387
- await validator(instance)
431
+ const expected = await instance.toObj()
432
+ await validator(instance, {})
388
433
 
389
434
  const actual = modelValidator.getCall(0).args[1]
390
435
  assert.deepEqual(actual, expected)
@@ -393,19 +438,15 @@ describe('/src/validation.js', () => {
393
438
  const modelValidator = sinon.stub().returns('my-validation-error')
394
439
  const testModel3 = createTestModel3([modelValidator])
395
440
  const properties = {
396
- functions: {
397
- validators: {
398
- id: sinon.stub().returns(undefined),
399
- name: sinon.stub().returns(undefined),
400
- },
401
- },
441
+ id: sinon.stub().returns(undefined),
442
+ name: sinon.stub().returns(undefined),
402
443
  }
403
444
  const validator = createModelValidator(properties, [modelValidator])
404
445
  const instance = testModel3.create({
405
446
  id: 'test-id',
406
447
  name: 'my-name',
407
448
  })
408
- const actual = await validator(instance)
449
+ const actual = await validator(instance, {})
409
450
  const expected = {
410
451
  overall: ['my-validation-error'],
411
452
  }
@@ -416,12 +457,8 @@ describe('/src/validation.js', () => {
416
457
  const modelValidator2 = sinon.stub().resolves(undefined)
417
458
  const testModel3 = createTestModel3([modelValidator1, modelValidator2])
418
459
  const properties = {
419
- functions: {
420
- validators: {
421
- id: sinon.stub().returns(undefined),
422
- name: sinon.stub().returns(undefined),
423
- },
424
- },
460
+ id: sinon.stub().returns(undefined),
461
+ name: sinon.stub().returns(undefined),
425
462
  }
426
463
  const validator = createModelValidator(properties, [
427
464
  modelValidator1,
@@ -431,182 +468,213 @@ describe('/src/validation.js', () => {
431
468
  id: 'test-id',
432
469
  name: 'my-name',
433
470
  })
434
- const actual = await validator(instance)
471
+ const actual = await validator(instance, {})
435
472
  const expected = {}
436
473
  assert.deepEqual(actual, expected)
437
474
  })
438
475
  it('should use both functions.validate for two objects', async () => {
439
- const propertys = {
440
- functions: {
441
- validators: {
442
- id: sinon.stub().returns(undefined),
443
- type: sinon.stub().returns(undefined),
444
- },
445
- },
476
+ const properties = {
477
+ id: sinon.stub().returns(undefined),
478
+ type: sinon.stub().returns(undefined),
446
479
  }
447
- const validator = createModelValidator(propertys)
448
- const testModel3 = Model('Model', {})
480
+ const validator = createModelValidator(properties)
481
+ const testModel3 = BaseModel('Model', { properties: {} })
449
482
  const instance = testModel3.create({
450
483
  id: 'test-id',
451
484
  name: 'my-name',
452
485
  })
453
- await validator(instance)
454
- sinon.assert.calledOnce(propertys.functions.validators.id)
455
- sinon.assert.calledOnce(propertys.functions.validators.type)
486
+ await validator(instance, {})
487
+ sinon.assert.calledOnce(properties.id)
488
+ sinon.assert.calledOnce(properties.type)
456
489
  })
457
490
  it('should run a validators.model() function', async () => {
458
- const propertys = {
459
- functions: {
460
- validators: {
461
- id: sinon.stub().returns(undefined),
462
- type: sinon.stub().returns(undefined),
463
- model: sinon.stub().returns(undefined),
464
- },
465
- },
491
+ const properties = {
492
+ id: sinon.stub().returns(undefined),
493
+ type: sinon.stub().returns(undefined),
494
+ model: sinon.stub().returns(undefined),
466
495
  }
467
- const validator = createModelValidator(propertys)
468
- const testModel3 = Model('Model', {})
496
+ const validator = createModelValidator(properties)
497
+ const testModel3 = BaseModel('Model', { properties: {} })
469
498
  const instance = testModel3.create({
470
499
  id: 'test-id',
471
500
  name: 'my-name',
472
501
  })
473
- await validator(instance)
474
- sinon.assert.called(propertys.functions.validators.model)
475
- })
476
- it('should combine results for both functions.validators for two objects that error', async () => {
477
- const propertys = {
478
- functions: {
479
- validators: {
480
- id: sinon.stub().returns('error1'),
481
- type: sinon.stub().returns('error2'),
482
- },
483
- },
502
+ await validator(instance, {})
503
+ sinon.assert.called(properties.model)
504
+ })
505
+ it('should combine results for both validators for two objects that error', async () => {
506
+ const properties = {
507
+ id: sinon.stub().returns(['error1']),
508
+ type: sinon.stub().returns(['error2']),
484
509
  }
485
- const validator = createModelValidator(propertys)
486
- const testModel3 = Model('Model', {})
510
+ const validator = createModelValidator(properties)
511
+ const testModel3 = BaseModel('Model', { properties: {} })
487
512
  const instance = testModel3.create({
488
513
  id: 'test-id',
489
514
  type: 'my-name',
490
515
  })
491
- const actual = await validator(instance)
516
+ const actual = await validator(instance, {})
492
517
  const expected = {
493
- id: 'error1',
494
- type: 'error2',
518
+ id: ['error1'],
519
+ type: ['error2'],
495
520
  }
496
521
  assert.deepEqual(actual, expected)
497
522
  })
498
523
  it('should take the error of the one of two functions', async () => {
499
- const propertys = {
500
- functions: {
501
- validators: {
502
- id: sinon.stub().returns(undefined),
503
- type: sinon.stub().returns('error2'),
504
- },
505
- },
524
+ const properties = {
525
+ id: sinon.stub().returns(undefined),
526
+ type: sinon.stub().returns(['error2']),
506
527
  }
507
- const validator = createModelValidator(propertys)
508
- const testModel3 = Model('Model', {})
528
+ const validator = createModelValidator(properties)
529
+ const testModel3 = BaseModel('Model', { properties: {} })
509
530
  const instance = testModel3.create({
510
531
  id: 'test-id',
511
532
  type: 'my-name',
512
533
  })
513
- const actual = await validator(instance)
534
+ const actual = await validator(instance, {})
514
535
  const expected = {
515
- type: 'error2',
536
+ type: ['error2'],
516
537
  }
517
538
  assert.deepEqual(actual, expected)
518
539
  })
519
540
  })
520
541
  describe('#createPropertyValidator()', () => {
542
+ it('should accept an undefined configuration', async () => {
543
+ // @ts-ignore
544
+ const validator = createPropertyValidator(() => null, undefined)
545
+ const actual = await validator(EMPTY_MODEL_INSTANCE, {})
546
+ const expected: readonly string[] = []
547
+ assert.deepEqual(actual, expected)
548
+ })
549
+
550
+ it('should accept unhandled configurations without exception', async () => {
551
+ const validator = createPropertyValidator(() => null, {
552
+ // @ts-ignore
553
+ notarealarg: false,
554
+ })
555
+ const actual = await validator(EMPTY_MODEL_INSTANCE, {})
556
+ const expected: readonly string[] = []
557
+ assert.deepEqual(actual, expected)
558
+ })
521
559
  it('should not include isRequired if required=false, returning []', async () => {
522
- const validator = createPropertyValidator({ required: false })
523
- const actual = await validator(null)
524
- const expected = []
560
+ const validator = createPropertyValidator(() => null, { required: false })
561
+ // @ts-ignore
562
+ const actual = await validator(null, {})
563
+ const expected: readonly string[] = []
525
564
  assert.deepEqual(actual, expected)
526
565
  })
527
566
  it('should return [] if no configs are provided', async () => {
528
- const validator = createPropertyValidator({})
529
- const actual = await validator(null)
530
- const expected = []
567
+ const validator = createPropertyValidator(() => null, {})
568
+ // @ts-ignore
569
+ const actual = await validator(null, {})
570
+ const expected: readonly string[] = []
531
571
  assert.deepEqual(actual, expected)
532
572
  })
533
573
  it('should use isRequired if required=false, returning one error', async () => {
534
- const validator = createPropertyValidator({ required: true })
535
- const actual = await validator(null)
574
+ const validator = createPropertyValidator(() => null, { required: true })
575
+ // @ts-ignore
576
+ const actual = await validator(null, {})
536
577
  const expected = 1
537
578
  assert.equal(actual.length, expected)
538
579
  })
539
580
  it('should use validators.isRequired returning one error', async () => {
540
- const validator = createPropertyValidator({ validators: [isRequired] })
541
- const actual = await validator(null)
581
+ const validator = createPropertyValidator(() => null, {
582
+ validators: [isRequired],
583
+ })
584
+ // @ts-ignore
585
+ const actual = await validator(null, {})
542
586
  const expected = 1
543
587
  assert.equal(actual.length, expected)
544
588
  })
545
589
  })
546
590
  describe('#isArray()', () => {
547
591
  it('should return an error for null', () => {
548
- const actual = isArray(null)
592
+ const actual = isArray(null, EMPTY_MODEL_INSTANCE, {})
549
593
  assert.isOk(actual)
550
594
  })
551
595
  it('should return an error for undefined', () => {
552
- const actual = isArray(undefined)
596
+ const actual = isArray(undefined, EMPTY_MODEL_INSTANCE, {})
553
597
  assert.isOk(actual)
554
598
  })
555
599
  it('should return an error for 1', () => {
556
- const actual = isArray(1)
600
+ const actual = isArray(1, EMPTY_MODEL_INSTANCE, {})
557
601
  assert.isOk(actual)
558
602
  })
559
603
  it('should return an error for "1"', () => {
560
- const actual = isArray('1')
604
+ const actual = isArray('1', EMPTY_MODEL_INSTANCE, {})
561
605
  assert.isOk(actual)
562
606
  })
563
607
  it('should return undefined for [1,2,3]', () => {
564
- const actual = isArray([1, 2, 3])
608
+ const actual = isArray([1, 2, 3], EMPTY_MODEL_INSTANCE, {})
565
609
  assert.isUndefined(actual)
566
610
  })
567
611
  it('should return undefined for []', () => {
568
- const actual = isArray([])
612
+ const actual = isArray([], EMPTY_MODEL_INSTANCE, {})
569
613
  assert.isUndefined(actual)
570
614
  })
571
615
  })
572
616
  describe('#arrayType()', () => {
573
617
  describe('#(object)()', () => {
574
618
  it('should return an error for null, even though its an object, its not an array', () => {
575
- const actual = arrayType('object')(null)
619
+ const actual = arrayType('object')(null, EMPTY_MODEL_INSTANCE, {})
576
620
  assert.isOk(actual)
577
621
  })
578
622
  it('should return an error for 1', () => {
579
- const actual = arrayType('object')(1)
623
+ const actual = arrayType('object')(1, EMPTY_MODEL_INSTANCE, {})
580
624
  assert.isOk(actual)
581
625
  })
582
626
  it('should return undefined for [{}]', () => {
583
- const actual = arrayType('object')([{}])
627
+ const actual = arrayType('object')([{}], EMPTY_MODEL_INSTANCE, {})
584
628
  assert.isUndefined(actual)
585
629
  })
586
630
  })
587
631
  describe('#(integer)()', () => {
588
632
  it('should return an error for null', () => {
589
- const actual = arrayType(TYPE_PRIMATIVES.integer)(null)
633
+ const actual = arrayType(TYPE_PRIMITIVES.integer)(
634
+ null,
635
+ EMPTY_MODEL_INSTANCE,
636
+ {}
637
+ )
590
638
  assert.isOk(actual)
591
639
  })
592
640
  it('should return an error for undefined', () => {
593
- const actual = arrayType(TYPE_PRIMATIVES.integer)(undefined)
641
+ const actual = arrayType(TYPE_PRIMITIVES.integer)(
642
+ undefined,
643
+ EMPTY_MODEL_INSTANCE,
644
+ {}
645
+ )
594
646
  assert.isOk(actual)
595
647
  })
596
648
  it('should return an error for 1', () => {
597
- const actual = arrayType(TYPE_PRIMATIVES.integer)(1)
649
+ const actual = arrayType(TYPE_PRIMITIVES.integer)(
650
+ 1,
651
+ EMPTY_MODEL_INSTANCE,
652
+ {}
653
+ )
598
654
  assert.isOk(actual)
599
655
  })
600
656
  it('should return an error for "1"', () => {
601
- const actual = arrayType(TYPE_PRIMATIVES.integer)('1')
657
+ const actual = arrayType(TYPE_PRIMITIVES.integer)(
658
+ '1',
659
+ EMPTY_MODEL_INSTANCE,
660
+ {}
661
+ )
602
662
  assert.isOk(actual)
603
663
  })
604
664
  it('should return undefined for [1,2,3]', () => {
605
- const actual = arrayType(TYPE_PRIMATIVES.integer)([1, 2, 3])
665
+ const actual = arrayType(TYPE_PRIMITIVES.integer)(
666
+ [1, 2, 3],
667
+ EMPTY_MODEL_INSTANCE,
668
+ {}
669
+ )
606
670
  assert.isUndefined(actual)
607
671
  })
608
672
  it('should return an error for [1,"2",3]', () => {
609
- const actual = arrayType(TYPE_PRIMATIVES.integer)([1, '2', 3])
673
+ const actual = arrayType(TYPE_PRIMITIVES.integer)(
674
+ [1, '2', 3],
675
+ EMPTY_MODEL_INSTANCE,
676
+ {}
677
+ )
610
678
  assert.isOk(actual)
611
679
  })
612
680
  })