@strapi/strapi 4.10.0-beta.1 → 4.10.1-experimental.0

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 (193) hide show
  1. package/bin/strapi.js +2 -468
  2. package/coverage/clover.xml +1613 -0
  3. package/coverage/coverage-final.json +48 -0
  4. package/coverage/lcov-report/base.css +224 -0
  5. package/coverage/lcov-report/block-navigation.js +87 -0
  6. package/coverage/lcov-report/commands/__tests__/data-transfer/shared/index.html +116 -0
  7. package/coverage/lcov-report/commands/__tests__/data-transfer/shared/transfer.test.utils.js.html +133 -0
  8. package/coverage/lcov-report/commands/admin-create.js.html +424 -0
  9. package/coverage/lcov-report/commands/admin-reset.js.html +241 -0
  10. package/coverage/lcov-report/commands/generate-template.js.html +373 -0
  11. package/coverage/lcov-report/commands/index.html +146 -0
  12. package/coverage/lcov-report/commands/transfer/export.js.html +619 -0
  13. package/coverage/lcov-report/commands/transfer/import.js.html +562 -0
  14. package/coverage/lcov-report/commands/transfer/index.html +146 -0
  15. package/coverage/lcov-report/commands/transfer/transfer.js.html +532 -0
  16. package/coverage/lcov-report/commands/utils/helpers.js.html +430 -0
  17. package/coverage/lcov-report/commands/utils/index.html +116 -0
  18. package/coverage/lcov-report/core/registries/custom-fields.js.html +301 -0
  19. package/coverage/lcov-report/core/registries/index.html +116 -0
  20. package/coverage/lcov-report/core-api/controller/collection-type.js.html +418 -0
  21. package/coverage/lcov-report/core-api/controller/index.html +161 -0
  22. package/coverage/lcov-report/core-api/controller/index.js.html +220 -0
  23. package/coverage/lcov-report/core-api/controller/single-type.js.html +274 -0
  24. package/coverage/lcov-report/core-api/controller/transform.js.html +376 -0
  25. package/coverage/lcov-report/core-api/service/collection-type.js.html +325 -0
  26. package/coverage/lcov-report/core-api/service/index.html +161 -0
  27. package/coverage/lcov-report/core-api/service/index.js.html +220 -0
  28. package/coverage/lcov-report/core-api/service/pagination.js.html +460 -0
  29. package/coverage/lcov-report/core-api/service/single-type.js.html +301 -0
  30. package/coverage/lcov-report/favicon.png +0 -0
  31. package/coverage/lcov-report/index.html +386 -0
  32. package/coverage/lcov-report/load/filepath-to-prop-path.js.html +151 -0
  33. package/coverage/lcov-report/load/index.html +116 -0
  34. package/coverage/lcov-report/prettify.css +1 -0
  35. package/coverage/lcov-report/prettify.js +2 -0
  36. package/coverage/lcov-report/services/content-api/index.html +116 -0
  37. package/coverage/lcov-report/services/content-api/index.js.html +307 -0
  38. package/coverage/lcov-report/services/content-api/permissions/engine.js.html +100 -0
  39. package/coverage/lcov-report/services/content-api/permissions/index.html +131 -0
  40. package/coverage/lcov-report/services/content-api/permissions/index.js.html +529 -0
  41. package/coverage/lcov-report/services/content-api/permissions/providers/action.js.html +142 -0
  42. package/coverage/lcov-report/services/content-api/permissions/providers/condition.js.html +142 -0
  43. package/coverage/lcov-report/services/content-api/permissions/providers/index.html +146 -0
  44. package/coverage/lcov-report/services/content-api/permissions/providers/index.js.html +112 -0
  45. package/coverage/lcov-report/services/core-store.js.html +520 -0
  46. package/coverage/lcov-report/services/entity-service/attributes/index.html +131 -0
  47. package/coverage/lcov-report/services/entity-service/attributes/index.js.html +178 -0
  48. package/coverage/lcov-report/services/entity-service/attributes/transforms.js.html +145 -0
  49. package/coverage/lcov-report/services/entity-service/components.js.html +1246 -0
  50. package/coverage/lcov-report/services/entity-service/index.html +146 -0
  51. package/coverage/lcov-report/services/entity-service/index.js.html +1120 -0
  52. package/coverage/lcov-report/services/entity-service/params.js.html +112 -0
  53. package/coverage/lcov-report/services/entity-validator/__tests__/relations/utils/index.html +116 -0
  54. package/coverage/lcov-report/services/entity-validator/__tests__/relations/utils/relations.testdata.js.html +544 -0
  55. package/coverage/lcov-report/services/entity-validator/index.html +131 -0
  56. package/coverage/lcov-report/services/entity-validator/index.js.html +1231 -0
  57. package/coverage/lcov-report/services/entity-validator/validators.js.html +733 -0
  58. package/coverage/lcov-report/services/event-hub.js.html +319 -0
  59. package/coverage/lcov-report/services/fs.js.html +259 -0
  60. package/coverage/lcov-report/services/index.html +161 -0
  61. package/coverage/lcov-report/services/metrics/admin-user-hash.js.html +148 -0
  62. package/coverage/lcov-report/services/metrics/index.html +206 -0
  63. package/coverage/lcov-report/services/metrics/index.js.html +265 -0
  64. package/coverage/lcov-report/services/metrics/is-truthy.js.html +112 -0
  65. package/coverage/lcov-report/services/metrics/middleware.js.html +184 -0
  66. package/coverage/lcov-report/services/metrics/rate-limiter.js.html +166 -0
  67. package/coverage/lcov-report/services/metrics/sender.js.html +394 -0
  68. package/coverage/lcov-report/services/metrics/stringify-deep.js.html +151 -0
  69. package/coverage/lcov-report/services/utils/index.html +116 -0
  70. package/coverage/lcov-report/services/utils/upload-files.js.html +322 -0
  71. package/coverage/lcov-report/services/worker-queue.js.html +262 -0
  72. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  73. package/coverage/lcov-report/sorter.js +196 -0
  74. package/coverage/lcov-report/utils/convert-custom-field-type.js.html +151 -0
  75. package/coverage/lcov-report/utils/index.html +146 -0
  76. package/coverage/lcov-report/utils/machine-id.js.html +127 -0
  77. package/coverage/lcov-report/utils/url-from-segments.js.html +121 -0
  78. package/ee/LICENSE.txt +21 -0
  79. package/ee/index.js +3 -1
  80. package/ee/license.js +8 -7
  81. package/lib/Strapi.js +3 -0
  82. package/lib/commands/__tests__/commands.test.js +20 -0
  83. package/lib/commands/__tests__/commands.test.utils.js +16 -0
  84. package/lib/commands/actions/admin/create-user/__tests__/admin.create-user.test.js +450 -0
  85. package/lib/commands/{admin-create.js → actions/admin/create-user/action.js} +2 -2
  86. package/lib/commands/actions/admin/create-user/command.js +19 -0
  87. package/lib/commands/actions/admin/reset-user-password/__tests__/admin.reset-user-password.test.js +145 -0
  88. package/lib/commands/{admin-reset.js → actions/admin/reset-user-password/action.js} +2 -2
  89. package/lib/commands/actions/admin/reset-user-password/command.js +17 -0
  90. package/lib/commands/{build.js → actions/build/action.js} +2 -2
  91. package/lib/commands/actions/build/command.js +15 -0
  92. package/lib/commands/{configurationDump.js → actions/configuration/dump/action.js} +1 -1
  93. package/lib/commands/actions/configuration/dump/command.js +17 -0
  94. package/lib/commands/{configurationRestore.js → actions/configuration/restore/action.js} +1 -1
  95. package/lib/commands/actions/configuration/restore/command.js +17 -0
  96. package/lib/commands/{console.js → actions/console/action.js} +2 -2
  97. package/lib/commands/actions/console/command.js +14 -0
  98. package/lib/commands/{content-types/list.js → actions/content-types/list/action.js} +2 -2
  99. package/lib/commands/actions/content-types/list/command.js +14 -0
  100. package/lib/commands/{controllers/list.js → actions/controllers/list/action.js} +2 -2
  101. package/lib/commands/actions/controllers/list/command.js +14 -0
  102. package/lib/commands/{develop.js → actions/develop/action.js} +3 -3
  103. package/lib/commands/actions/develop/command.js +19 -0
  104. package/lib/commands/actions/export/__tests__/export.test.js +175 -0
  105. package/lib/commands/{transfer/export.js → actions/export/action.js} +3 -2
  106. package/lib/commands/actions/export/command.js +45 -0
  107. package/lib/commands/actions/generate/command.js +18 -0
  108. package/lib/commands/{hooks/list.js → actions/hooks/list/action.js} +2 -2
  109. package/lib/commands/actions/hooks/list/command.js +14 -0
  110. package/lib/commands/actions/import/__tests__/import.test.js +143 -0
  111. package/lib/commands/{transfer/import.js → actions/import/action.js} +2 -2
  112. package/lib/commands/actions/import/command.js +97 -0
  113. package/lib/commands/{install.js → actions/install/action.js} +1 -1
  114. package/lib/commands/actions/install/command.js +14 -0
  115. package/lib/commands/{middlewares/list.js → actions/middlewares/list/action.js} +2 -2
  116. package/lib/commands/actions/middlewares/list/command.js +14 -0
  117. package/lib/commands/{new.js → actions/new/action.js} +1 -1
  118. package/lib/commands/actions/new/command.js +35 -0
  119. package/lib/commands/{policies/list.js → actions/policies/list/action.js} +2 -2
  120. package/lib/commands/actions/policies/list/command.js +14 -0
  121. package/lib/commands/{report.js → actions/report/action.js} +1 -1
  122. package/lib/commands/actions/report/command.js +17 -0
  123. package/lib/commands/{routes/list.js → actions/routes/list/action.js} +2 -2
  124. package/lib/commands/actions/routes/list/command.js +14 -0
  125. package/lib/commands/{services/list.js → actions/services/list/action.js} +2 -2
  126. package/lib/commands/actions/services/list/command.js +14 -0
  127. package/lib/commands/{start.js → actions/start/action.js} +1 -1
  128. package/lib/commands/actions/start/command.js +14 -0
  129. package/lib/commands/{opt-out-telemetry.js → actions/telemetry/disable/action.js} +1 -1
  130. package/lib/commands/actions/telemetry/disable/command.js +14 -0
  131. package/lib/commands/{opt-in-telemetry.js → actions/telemetry/enable/action.js} +1 -1
  132. package/lib/commands/actions/telemetry/enable/command.js +14 -0
  133. package/lib/commands/actions/templates/generate/__tests__/templates.generate.js +118 -0
  134. package/lib/commands/actions/templates/generate/command.js +14 -0
  135. package/lib/commands/actions/transfer/__tests__/transfer.test.js +178 -0
  136. package/lib/commands/{transfer/transfer.js → actions/transfer/action.js} +2 -2
  137. package/lib/commands/actions/transfer/command.js +115 -0
  138. package/lib/commands/{ts/generate-types.js → actions/ts/generate-types/action.js} +2 -2
  139. package/lib/commands/actions/ts/generate-types/command.js +21 -0
  140. package/lib/commands/{uninstall.js → actions/uninstall/action.js} +1 -1
  141. package/lib/commands/actions/uninstall/command.js +15 -0
  142. package/lib/commands/actions/version/command.js +19 -0
  143. package/lib/commands/{watchAdmin.js → actions/watch-admin/action.js} +4 -4
  144. package/lib/commands/actions/watch-admin/command.js +15 -0
  145. package/lib/commands/index.js +66 -0
  146. package/lib/commands/{transfer/utils.js → utils/data-transfer.js} +2 -2
  147. package/lib/commands/utils/helpers.js +54 -3
  148. package/lib/core/registries/__tests__/custom-fields.test.js +152 -0
  149. package/lib/core/registries/custom-fields.js +19 -2
  150. package/lib/core-api/__tests__/controller.test.js +39 -0
  151. package/lib/core-api/controller/__tests__/transform.test.js +226 -0
  152. package/lib/core-api/service/__tests__/index.test.js +127 -0
  153. package/lib/core-api/service/__tests__/pagination.test.js +275 -0
  154. package/lib/load/__tests__/filepath-to-prop-path.test.js +30 -0
  155. package/lib/middlewares/__tests__/errors.test.js +21 -0
  156. package/lib/services/__tests__/content-api-permissions.test.js +291 -0
  157. package/lib/services/__tests__/core-store.test.js +148 -0
  158. package/lib/services/__tests__/event-hub.test.js +126 -0
  159. package/lib/services/__tests__/fs.test.js +78 -0
  160. package/lib/services/__tests__/worker-queue.test.js +47 -0
  161. package/lib/services/entity-service/__tests__/entity-service-events.test.js +117 -0
  162. package/lib/services/entity-service/__tests__/entity-service.test.js +587 -0
  163. package/lib/services/entity-validator/__tests__/biginteger-validators.test.js +220 -0
  164. package/lib/services/entity-validator/__tests__/date-validators.test.js +183 -0
  165. package/lib/services/entity-validator/__tests__/datetime-validators.test.js +183 -0
  166. package/lib/services/entity-validator/__tests__/email-validators.test.js +56 -0
  167. package/lib/services/entity-validator/__tests__/enumeration-validators.test.js +43 -0
  168. package/lib/services/entity-validator/__tests__/float-validators.test.js +278 -0
  169. package/lib/services/entity-validator/__tests__/index.test.js +609 -0
  170. package/lib/services/entity-validator/__tests__/integer-validators.test.js +278 -0
  171. package/lib/services/entity-validator/__tests__/relations/attribute-level.test.js +123 -0
  172. package/lib/services/entity-validator/__tests__/relations/component-level.test.js +275 -0
  173. package/lib/services/entity-validator/__tests__/relations/dynamic-zone-level.test.js +159 -0
  174. package/lib/services/entity-validator/__tests__/relations/media-level.test.js +74 -0
  175. package/lib/services/entity-validator/__tests__/relations/utils/relations.testdata.js +153 -0
  176. package/lib/services/entity-validator/__tests__/string-validators.test.js +374 -0
  177. package/lib/services/entity-validator/__tests__/time-validators.test.js +183 -0
  178. package/lib/services/entity-validator/__tests__/timestamp-validators.test.js +204 -0
  179. package/lib/services/entity-validator/__tests__/uid-validators.test.js +229 -0
  180. package/lib/services/metrics/__tests__/admin-user-hash.test.js +41 -0
  181. package/lib/services/metrics/__tests__/index.test.js +157 -0
  182. package/lib/services/metrics/__tests__/is-truthy.js +33 -0
  183. package/lib/services/metrics/__tests__/middleware.test.js +60 -0
  184. package/lib/services/metrics/__tests__/rate-limiter.test.js +50 -0
  185. package/lib/services/metrics/__tests__/stringify-deep.test.js +27 -0
  186. package/lib/services/metrics/sender.js +1 -2
  187. package/lib/services/webhook-runner.js +4 -4
  188. package/lib/types/core/commands/index.d.ts +6 -0
  189. package/lib/utils/__tests__/convert-custom-field-type.test.js +69 -0
  190. package/lib/utils/__tests__/url-from-segments.test.js +40 -0
  191. package/lib/utils/fetch.js +23 -0
  192. package/package.json +17 -16
  193. /package/lib/commands/{generate-template.js → actions/templates/generate/action.js} +0 -0
@@ -0,0 +1,374 @@
1
+ 'use strict';
2
+
3
+ const strapiUtils = require('@strapi/utils');
4
+ const {
5
+ errors: { YupValidationError },
6
+ } = require('@strapi/utils');
7
+ const validators = require('../validators');
8
+
9
+ describe('String validator', () => {
10
+ describe('unique', () => {
11
+ const fakeFindOne = jest.fn();
12
+
13
+ global.strapi = {
14
+ db: {
15
+ query: jest.fn(() => ({
16
+ findOne: fakeFindOne,
17
+ })),
18
+ },
19
+ };
20
+
21
+ afterEach(() => {
22
+ jest.clearAllMocks();
23
+ fakeFindOne.mockReset();
24
+ });
25
+
26
+ const fakeModel = {
27
+ kind: 'contentType',
28
+ modelName: 'test-model',
29
+ uid: 'test-uid',
30
+ privateAttributes: [],
31
+ options: {},
32
+ attributes: {
33
+ attrStringUnique: { type: 'string', unique: true },
34
+ },
35
+ };
36
+
37
+ test('it does not validates the unique constraint if the attribute is not set as unique', async () => {
38
+ fakeFindOne.mockResolvedValueOnce(null);
39
+
40
+ const validator = strapiUtils.validateYupSchema(
41
+ validators.string(
42
+ {
43
+ attr: { type: 'string' },
44
+ model: fakeModel,
45
+ updatedAttribute: {
46
+ name: 'attrStringUnique',
47
+ value: 'non-unique-test-data',
48
+ },
49
+ entity: null,
50
+ },
51
+ { isDraft: false }
52
+ )
53
+ );
54
+
55
+ await validator('non-unique-test-data');
56
+
57
+ expect(fakeFindOne).not.toHaveBeenCalled();
58
+ });
59
+
60
+ test('it does not validates the unique constraint if the attribute value is `null`', async () => {
61
+ fakeFindOne.mockResolvedValueOnce(null);
62
+
63
+ const validator = strapiUtils.validateYupSchema(
64
+ validators
65
+ .string(
66
+ {
67
+ attr: { type: 'string', unique: true },
68
+ model: fakeModel,
69
+ updatedAttribute: {
70
+ name: 'attrStringUnique',
71
+ value: null,
72
+ },
73
+ entity: null,
74
+ },
75
+ { isDraft: false }
76
+ )
77
+ .nullable()
78
+ );
79
+
80
+ await validator(null);
81
+
82
+ expect(fakeFindOne).not.toHaveBeenCalled();
83
+ });
84
+
85
+ test('it validates the unique constraint if there is no other record in the database', async () => {
86
+ fakeFindOne.mockResolvedValueOnce(null);
87
+
88
+ const validator = strapiUtils.validateYupSchema(
89
+ validators.string(
90
+ {
91
+ attr: { type: 'string', unique: true },
92
+ model: fakeModel,
93
+ updatedAttribute: {
94
+ name: 'attrStringUnique',
95
+ value: 'non-unique-test-data',
96
+ },
97
+ entity: null,
98
+ },
99
+ { isDraft: false }
100
+ )
101
+ );
102
+
103
+ expect(await validator('non-unique-test-data')).toBe('non-unique-test-data');
104
+ });
105
+
106
+ test('it fails the validation of the unique constraint if the database contains a record with the same attribute value', async () => {
107
+ expect.assertions(1);
108
+ fakeFindOne.mockResolvedValueOnce({ attrStringUnique: 'unique-test-data' });
109
+
110
+ const validator = strapiUtils.validateYupSchema(
111
+ validators.string(
112
+ {
113
+ attr: { type: 'string', unique: true },
114
+ model: fakeModel,
115
+ updatedAttribute: {
116
+ name: 'attrStringUnique',
117
+ value: 'unique-test-data',
118
+ },
119
+ entity: null,
120
+ },
121
+ { isDraft: false }
122
+ )
123
+ );
124
+
125
+ try {
126
+ await validator('unique-test-data');
127
+ } catch (err) {
128
+ expect(err).toBeInstanceOf(YupValidationError);
129
+ }
130
+ });
131
+
132
+ test('it validates the unique constraint if the attribute data has not changed even if there is a record in the database with the same attribute value', async () => {
133
+ fakeFindOne.mockResolvedValueOnce({ attrStringUnique: 'non-updated-unique-test-data' });
134
+
135
+ const validator = strapiUtils.validateYupSchema(
136
+ validators.string(
137
+ {
138
+ attr: { type: 'string', unique: true },
139
+ model: fakeModel,
140
+ updatedAttribute: {
141
+ name: 'attrStringUnique',
142
+ value: 'non-updated-unique-test-data',
143
+ },
144
+ entity: { id: 1, attrStringUnique: 'non-updated-unique-test-data' },
145
+ },
146
+ { isDraft: false }
147
+ )
148
+ );
149
+
150
+ expect(await validator('non-updated-unique-test-data')).toBe('non-updated-unique-test-data');
151
+ });
152
+
153
+ test('it checks the database for records with the same value for the checked attribute', async () => {
154
+ fakeFindOne.mockResolvedValueOnce(null);
155
+
156
+ const validator = strapiUtils.validateYupSchema(
157
+ validators.string(
158
+ {
159
+ attr: { type: 'string', unique: true },
160
+ model: fakeModel,
161
+ updatedAttribute: {
162
+ name: 'attrStringUnique',
163
+ value: 'test-data',
164
+ },
165
+ entity: null,
166
+ },
167
+ { isDraft: false }
168
+ )
169
+ );
170
+
171
+ await validator('test-data');
172
+
173
+ expect(fakeFindOne).toHaveBeenCalledWith({
174
+ select: ['id'],
175
+ where: { attrStringUnique: 'test-data' },
176
+ });
177
+ });
178
+
179
+ test('it checks the database for records with the same value but not the same id for the checked attribute if an entity is passed', async () => {
180
+ fakeFindOne.mockResolvedValueOnce(null);
181
+
182
+ const validator = strapiUtils.validateYupSchema(
183
+ validators.string(
184
+ {
185
+ attr: { type: 'string', unique: true },
186
+ model: fakeModel,
187
+ updatedAttribute: {
188
+ name: 'attrStringUnique',
189
+ value: 'test-data',
190
+ },
191
+ entity: { id: 1, attrStringUnique: 'other-data' },
192
+ },
193
+ { isDraft: false }
194
+ )
195
+ );
196
+
197
+ await validator('test-data');
198
+
199
+ expect(fakeFindOne).toHaveBeenCalledWith({
200
+ select: ['id'],
201
+ where: { $and: [{ attrStringUnique: 'test-data' }, { $not: { id: 1 } }] },
202
+ });
203
+ });
204
+ });
205
+
206
+ describe('minLength', () => {
207
+ test('it does not validates the minLength constraint if the attribute minLength is not an integer', async () => {
208
+ const validator = strapiUtils.validateYupSchema(
209
+ validators.string(
210
+ {
211
+ attr: { type: 'string', minLength: '123' },
212
+ },
213
+ { isDraft: false }
214
+ )
215
+ );
216
+
217
+ expect(await validator('a')).toBe('a');
218
+ });
219
+
220
+ test('it does not validates the minLength constraint if it is a draft', async () => {
221
+ const validator = strapiUtils.validateYupSchema(
222
+ validators.string(
223
+ {
224
+ attr: { type: 'string', minLength: 3 },
225
+ },
226
+ { isDraft: true }
227
+ )
228
+ );
229
+
230
+ expect(await validator('a')).toBe('a');
231
+ });
232
+
233
+ test('it fails the validation if the string is shorter than the define minLength', async () => {
234
+ expect.assertions(1);
235
+
236
+ const validator = strapiUtils.validateYupSchema(
237
+ validators.string(
238
+ {
239
+ attr: { type: 'string', minLength: 3 },
240
+ },
241
+ { isDraft: false }
242
+ )
243
+ );
244
+
245
+ try {
246
+ await validator('a');
247
+ } catch (err) {
248
+ expect(err).toBeInstanceOf(YupValidationError);
249
+ }
250
+ });
251
+
252
+ test('it validates the minLength constraint if the string is longer than the define minLength', async () => {
253
+ const validator = strapiUtils.validateYupSchema(
254
+ validators.string(
255
+ {
256
+ attr: { type: 'string', minLength: 3 },
257
+ },
258
+ { isDraft: false }
259
+ )
260
+ );
261
+
262
+ expect(await validator('this string is longer than the minLenght')).toBe(
263
+ 'this string is longer than the minLenght'
264
+ );
265
+ });
266
+ });
267
+
268
+ describe('maxLength', () => {
269
+ test('it does not validates the maxLength constraint if the attribute maxLength is not an integer', async () => {
270
+ const validator = strapiUtils.validateYupSchema(
271
+ validators.string(
272
+ {
273
+ attr: { type: 'string', maxLength: '123' },
274
+ },
275
+ { isDraft: false }
276
+ )
277
+ );
278
+
279
+ expect(await validator('a')).toBe('a');
280
+ });
281
+
282
+ test('it fails the validation if the string is longer than the define maxLength', async () => {
283
+ expect.assertions(1);
284
+
285
+ const validator = strapiUtils.validateYupSchema(
286
+ validators.string(
287
+ {
288
+ attr: { type: 'string', maxLength: 3 },
289
+ },
290
+ { isDraft: false }
291
+ )
292
+ );
293
+
294
+ try {
295
+ await validator('this string is too long');
296
+ } catch (err) {
297
+ expect(err).toBeInstanceOf(YupValidationError);
298
+ }
299
+ });
300
+
301
+ test('it validates the maxLength constraint if the string is shorter than the define maxLength', async () => {
302
+ const validator = strapiUtils.validateYupSchema(
303
+ validators.string(
304
+ {
305
+ attr: { type: 'string', maxLength: 3 },
306
+ },
307
+ { isDraft: false }
308
+ )
309
+ );
310
+
311
+ expect(await validator('a')).toBe('a');
312
+ });
313
+ });
314
+
315
+ describe('regExp', () => {
316
+ test('it fails the validation of an empty string for a required field', async () => {
317
+ expect.assertions(1);
318
+
319
+ const validator = strapiUtils.validateYupSchema(
320
+ validators.string(
321
+ {
322
+ attr: { type: 'string', required: true, regex: '^\\w+$' },
323
+ },
324
+ { isDraft: false }
325
+ )
326
+ );
327
+
328
+ try {
329
+ await validator('');
330
+ } catch (err) {
331
+ expect(err).toBeInstanceOf(YupValidationError);
332
+ }
333
+ });
334
+
335
+ test('it validates a string for required field according to the regex constraint', async () => {
336
+ const validator = strapiUtils.validateYupSchema(
337
+ validators.string(
338
+ {
339
+ attr: { type: 'string', required: true, regex: '^\\w+$' },
340
+ },
341
+ { isDraft: false }
342
+ )
343
+ );
344
+
345
+ expect(await validator('Strapi')).toBe('Strapi');
346
+ });
347
+
348
+ test('it validates an empty string for non-required field with a regex constraint', async () => {
349
+ const validator = strapiUtils.validateYupSchema(
350
+ validators.string(
351
+ {
352
+ attr: { type: 'string', required: false, regex: '^\\w+$' },
353
+ },
354
+ { isDraft: false }
355
+ )
356
+ );
357
+
358
+ expect(await validator('')).toBe('');
359
+ });
360
+
361
+ test('it validates a string for non-required field according to the regex constraint', async () => {
362
+ const validator = strapiUtils.validateYupSchema(
363
+ validators.string(
364
+ {
365
+ attr: { type: 'string', required: false, regex: '^\\w+$' },
366
+ },
367
+ { isDraft: false }
368
+ )
369
+ );
370
+
371
+ expect(await validator('Strapi')).toBe('Strapi');
372
+ });
373
+ });
374
+ });
@@ -0,0 +1,183 @@
1
+ 'use strict';
2
+
3
+ const strapiUtils = require('@strapi/utils');
4
+ const {
5
+ errors: { YupValidationError },
6
+ } = require('@strapi/utils');
7
+ const validators = require('../validators');
8
+
9
+ describe('Time validator', () => {
10
+ describe('unique', () => {
11
+ const fakeFindOne = jest.fn();
12
+
13
+ global.strapi = {
14
+ db: {
15
+ query: jest.fn(() => ({
16
+ findOne: fakeFindOne,
17
+ })),
18
+ },
19
+ };
20
+
21
+ afterEach(() => {
22
+ jest.clearAllMocks();
23
+ fakeFindOne.mockReset();
24
+ });
25
+
26
+ const fakeModel = {
27
+ kind: 'contentType',
28
+ modelName: 'test-model',
29
+ uid: 'test-uid',
30
+ privateAttributes: [],
31
+ options: {},
32
+ attributes: {
33
+ attrTimeUnique: { type: 'time', unique: true },
34
+ },
35
+ };
36
+
37
+ test('it does not validates the unique constraint if the attribute is not set as unique', async () => {
38
+ fakeFindOne.mockResolvedValueOnce(null);
39
+
40
+ const validator = strapiUtils.validateYupSchema(
41
+ validators.time(
42
+ {
43
+ attr: { type: 'time' },
44
+ model: fakeModel,
45
+ updatedAttribute: { name: 'attrTimeUnique', value: '00:00:00.000Z' },
46
+ entity: null,
47
+ },
48
+ { isDraft: false }
49
+ )
50
+ );
51
+
52
+ await validator('00:00:00.000Z');
53
+
54
+ expect(fakeFindOne).not.toHaveBeenCalled();
55
+ });
56
+
57
+ test('it does not validates the unique constraint if the attribute value is `null`', async () => {
58
+ fakeFindOne.mockResolvedValueOnce(null);
59
+
60
+ const validator = strapiUtils.validateYupSchema(
61
+ validators
62
+ .time(
63
+ {
64
+ attr: { type: 'time', unique: true },
65
+ model: fakeModel,
66
+ updatedAttribute: { name: 'attrTimeUnique', value: null },
67
+ entity: { id: 1, attrTimeUnique: '00:00:00.000Z' },
68
+ },
69
+ { isDraft: false }
70
+ )
71
+ .nullable()
72
+ );
73
+
74
+ await validator(null);
75
+ expect(fakeFindOne).not.toHaveBeenCalled();
76
+ });
77
+
78
+ test('it validates the unique constraint if there is no other record in the database', async () => {
79
+ fakeFindOne.mockResolvedValueOnce(null);
80
+
81
+ const validator = strapiUtils.validateYupSchema(
82
+ validators.time(
83
+ {
84
+ attr: { type: 'time', unique: true },
85
+ model: fakeModel,
86
+ updatedAttribute: { name: 'attrTimeUnique', value: '00:00:00.000Z' },
87
+ entity: null,
88
+ },
89
+ { isDraft: false }
90
+ )
91
+ );
92
+
93
+ expect(await validator('00:00:00.000Z')).toBe('00:00:00.000Z');
94
+ });
95
+
96
+ test('it fails the validation of the unique constraint if the database contains a record with the same attribute value', async () => {
97
+ expect.assertions(1);
98
+ fakeFindOne.mockResolvedValueOnce({ attrTimeUnique: '00:00:00.000Z' });
99
+
100
+ const validator = strapiUtils.validateYupSchema(
101
+ validators.time(
102
+ {
103
+ attr: { type: 'time', unique: true },
104
+ model: fakeModel,
105
+ updatedAttribute: { name: 'attrTimeUnique', value: '00:00:00.000Z' },
106
+ entity: null,
107
+ },
108
+ { isDraft: false }
109
+ )
110
+ );
111
+
112
+ try {
113
+ await validator('00:00:00.000Z');
114
+ } catch (err) {
115
+ expect(err).toBeInstanceOf(YupValidationError);
116
+ }
117
+ });
118
+
119
+ test('it validates the unique constraint if the attribute data has not changed even if there is a record in the database with the same attribute value', async () => {
120
+ fakeFindOne.mockResolvedValueOnce({ attrTimeUnique: '00:00:00.000Z' });
121
+
122
+ const validator = strapiUtils.validateYupSchema(
123
+ validators.time(
124
+ {
125
+ attr: { type: 'time', unique: true },
126
+ model: fakeModel,
127
+ updatedAttribute: { name: 'attrTimeUnique', value: '00:00:00.000Z' },
128
+ entity: { id: 1, attrTimeUnique: '00:00:00.000Z' },
129
+ },
130
+ { isDraft: false }
131
+ )
132
+ );
133
+
134
+ expect(await validator('00:00:00.000Z')).toBe('00:00:00.000Z');
135
+ });
136
+
137
+ test('it checks the database for records with the same value for the checked attribute', async () => {
138
+ fakeFindOne.mockResolvedValueOnce(null);
139
+
140
+ const validator = strapiUtils.validateYupSchema(
141
+ validators.time(
142
+ {
143
+ attr: { type: 'time', unique: true },
144
+ model: fakeModel,
145
+ updatedAttribute: { name: 'attrTimeUnique', value: '00:00:00.000Z' },
146
+ entity: null,
147
+ },
148
+ { isDraft: false }
149
+ )
150
+ );
151
+
152
+ await validator('00:00:00.000Z');
153
+
154
+ expect(fakeFindOne).toHaveBeenCalledWith({
155
+ select: ['id'],
156
+ where: { attrTimeUnique: '00:00:00.000Z' },
157
+ });
158
+ });
159
+
160
+ test('it checks the database for records with the same value but not the same id for the checked attribute if an entity is passed', async () => {
161
+ fakeFindOne.mockResolvedValueOnce(null);
162
+
163
+ const validator = strapiUtils.validateYupSchema(
164
+ validators.time(
165
+ {
166
+ attr: { type: 'time', unique: true },
167
+ model: fakeModel,
168
+ updatedAttribute: { name: 'attrTimeUnique', value: '00:00:00.000Z' },
169
+ entity: { id: 1, attrTimeUnique: '01:00:00.000Z' },
170
+ },
171
+ { isDraft: false }
172
+ )
173
+ );
174
+
175
+ await validator('00:00:00.000Z');
176
+
177
+ expect(fakeFindOne).toHaveBeenCalledWith({
178
+ select: ['id'],
179
+ where: { $and: [{ attrTimeUnique: '00:00:00.000Z' }, { $not: { id: 1 } }] },
180
+ });
181
+ });
182
+ });
183
+ });