@dineroregnskab/eslint-plugin-custom-rules 2.0.6 → 2.1.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 (178) hide show
  1. package/README.md +49 -9
  2. package/eslint-plugin-custom-rules.js +10 -4
  3. package/package.json +2 -4
  4. package/rules/replace-first-with-take.js +31 -0
  5. package/rules/use-danish-currency-pipe.js +53 -0
  6. package/willy.yaml +5 -0
  7. package/node_modules/@aashutoshrathi/word-wrap/LICENSE +0 -21
  8. package/node_modules/@aashutoshrathi/word-wrap/README.md +0 -182
  9. package/node_modules/@aashutoshrathi/word-wrap/index.d.ts +0 -50
  10. package/node_modules/@aashutoshrathi/word-wrap/index.js +0 -52
  11. package/node_modules/@aashutoshrathi/word-wrap/package.json +0 -81
  12. package/node_modules/@angular-eslint/bundled-angular-compiler/README.md +0 -1
  13. package/node_modules/@angular-eslint/bundled-angular-compiler/dist/index.d.ts +0 -5
  14. package/node_modules/@angular-eslint/bundled-angular-compiler/dist/index.js +0 -32004
  15. package/node_modules/@angular-eslint/bundled-angular-compiler/package.json +0 -19
  16. package/node_modules/@angular-eslint/template-parser/LICENSE +0 -21
  17. package/node_modules/@angular-eslint/template-parser/README.md +0 -1
  18. package/node_modules/@angular-eslint/template-parser/dist/convert-source-span-to-loc.d.ts +0 -6
  19. package/node_modules/@angular-eslint/template-parser/dist/convert-source-span-to-loc.js +0 -69
  20. package/node_modules/@angular-eslint/template-parser/dist/index.d.ts +0 -44
  21. package/node_modules/@angular-eslint/template-parser/dist/index.js +0 -219
  22. package/node_modules/@angular-eslint/template-parser/package.json +0 -28
  23. package/node_modules/@eslint/eslintrc/LICENSE +0 -19
  24. package/node_modules/@eslint/eslintrc/README.md +0 -115
  25. package/node_modules/@eslint/eslintrc/conf/config-schema.js +0 -79
  26. package/node_modules/@eslint/eslintrc/conf/environments.js +0 -215
  27. package/node_modules/@eslint/eslintrc/dist/eslintrc-universal.cjs +0 -1104
  28. package/node_modules/@eslint/eslintrc/dist/eslintrc-universal.cjs.map +0 -1
  29. package/node_modules/@eslint/eslintrc/dist/eslintrc.cjs +0 -4333
  30. package/node_modules/@eslint/eslintrc/dist/eslintrc.cjs.map +0 -1
  31. package/node_modules/@eslint/eslintrc/lib/cascading-config-array-factory.js +0 -532
  32. package/node_modules/@eslint/eslintrc/lib/config-array/config-array.js +0 -523
  33. package/node_modules/@eslint/eslintrc/lib/config-array/config-dependency.js +0 -115
  34. package/node_modules/@eslint/eslintrc/lib/config-array/extracted-config.js +0 -145
  35. package/node_modules/@eslint/eslintrc/lib/config-array/ignore-pattern.js +0 -238
  36. package/node_modules/@eslint/eslintrc/lib/config-array/index.js +0 -19
  37. package/node_modules/@eslint/eslintrc/lib/config-array/override-tester.js +0 -225
  38. package/node_modules/@eslint/eslintrc/lib/config-array-factory.js +0 -1149
  39. package/node_modules/@eslint/eslintrc/lib/flat-compat.js +0 -318
  40. package/node_modules/@eslint/eslintrc/lib/index-universal.js +0 -29
  41. package/node_modules/@eslint/eslintrc/lib/index.js +0 -56
  42. package/node_modules/@eslint/eslintrc/lib/shared/ajv.js +0 -191
  43. package/node_modules/@eslint/eslintrc/lib/shared/config-ops.js +0 -135
  44. package/node_modules/@eslint/eslintrc/lib/shared/config-validator.js +0 -325
  45. package/node_modules/@eslint/eslintrc/lib/shared/deprecation-warnings.js +0 -63
  46. package/node_modules/@eslint/eslintrc/lib/shared/naming.js +0 -96
  47. package/node_modules/@eslint/eslintrc/lib/shared/relative-module-resolver.js +0 -42
  48. package/node_modules/@eslint/eslintrc/lib/shared/types.js +0 -149
  49. package/node_modules/@eslint/eslintrc/package.json +0 -82
  50. package/node_modules/@eslint/eslintrc/universal.js +0 -9
  51. package/node_modules/@eslint/js/LICENSE +0 -19
  52. package/node_modules/@eslint/js/README.md +0 -57
  53. package/node_modules/@eslint/js/package.json +0 -31
  54. package/node_modules/@eslint/js/src/configs/eslint-all.js +0 -211
  55. package/node_modules/@eslint/js/src/configs/eslint-recommended.js +0 -76
  56. package/node_modules/@eslint/js/src/index.js +0 -17
  57. package/node_modules/@eslint-community/eslint-utils/LICENSE +0 -21
  58. package/node_modules/@eslint-community/eslint-utils/README.md +0 -37
  59. package/node_modules/@eslint-community/eslint-utils/index.js +0 -2068
  60. package/node_modules/@eslint-community/eslint-utils/index.js.map +0 -1
  61. package/node_modules/@eslint-community/eslint-utils/index.mjs +0 -2027
  62. package/node_modules/@eslint-community/eslint-utils/index.mjs.map +0 -1
  63. package/node_modules/@eslint-community/eslint-utils/package.json +0 -73
  64. package/node_modules/@eslint-community/regexpp/LICENSE +0 -21
  65. package/node_modules/@eslint-community/regexpp/README.md +0 -177
  66. package/node_modules/@eslint-community/regexpp/index.d.ts +0 -1065
  67. package/node_modules/@eslint-community/regexpp/index.js +0 -2747
  68. package/node_modules/@eslint-community/regexpp/index.js.map +0 -1
  69. package/node_modules/@eslint-community/regexpp/index.mjs +0 -2737
  70. package/node_modules/@eslint-community/regexpp/index.mjs.map +0 -1
  71. package/node_modules/@eslint-community/regexpp/package.json +0 -93
  72. package/node_modules/@humanwhocodes/config-array/LICENSE +0 -201
  73. package/node_modules/@humanwhocodes/config-array/README.md +0 -342
  74. package/node_modules/@humanwhocodes/config-array/api.js +0 -1061
  75. package/node_modules/@humanwhocodes/config-array/package.json +0 -61
  76. package/node_modules/@humanwhocodes/module-importer/CHANGELOG.md +0 -15
  77. package/node_modules/@humanwhocodes/module-importer/LICENSE +0 -201
  78. package/node_modules/@humanwhocodes/module-importer/README.md +0 -80
  79. package/node_modules/@humanwhocodes/module-importer/dist/module-importer.cjs +0 -22
  80. package/node_modules/@humanwhocodes/module-importer/dist/module-importer.d.cts +0 -27
  81. package/node_modules/@humanwhocodes/module-importer/dist/module-importer.d.ts +0 -2
  82. package/node_modules/@humanwhocodes/module-importer/dist/module-importer.js +0 -18
  83. package/node_modules/@humanwhocodes/module-importer/package.json +0 -65
  84. package/node_modules/@humanwhocodes/module-importer/src/module-importer.cjs +0 -81
  85. package/node_modules/@humanwhocodes/module-importer/src/module-importer.js +0 -22
  86. package/node_modules/@humanwhocodes/object-schema/.eslintrc.js +0 -29
  87. package/node_modules/@humanwhocodes/object-schema/.github/workflows/nodejs-test.yml +0 -27
  88. package/node_modules/@humanwhocodes/object-schema/.github/workflows/release-please.yml +0 -39
  89. package/node_modules/@humanwhocodes/object-schema/CHANGELOG.md +0 -26
  90. package/node_modules/@humanwhocodes/object-schema/LICENSE +0 -29
  91. package/node_modules/@humanwhocodes/object-schema/README.md +0 -234
  92. package/node_modules/@humanwhocodes/object-schema/package.json +0 -33
  93. package/node_modules/@humanwhocodes/object-schema/src/index.js +0 -7
  94. package/node_modules/@humanwhocodes/object-schema/src/merge-strategy.js +0 -53
  95. package/node_modules/@humanwhocodes/object-schema/src/object-schema.js +0 -301
  96. package/node_modules/@humanwhocodes/object-schema/src/validation-strategy.js +0 -102
  97. package/node_modules/@humanwhocodes/object-schema/tests/merge-strategy.js +0 -66
  98. package/node_modules/@humanwhocodes/object-schema/tests/object-schema.js +0 -659
  99. package/node_modules/@humanwhocodes/object-schema/tests/validation-strategy.js +0 -186
  100. package/node_modules/@nodelib/fs.scandir/LICENSE +0 -21
  101. package/node_modules/@nodelib/fs.scandir/README.md +0 -171
  102. package/node_modules/@nodelib/fs.scandir/out/adapters/fs.d.ts +0 -20
  103. package/node_modules/@nodelib/fs.scandir/out/adapters/fs.js +0 -19
  104. package/node_modules/@nodelib/fs.scandir/out/constants.d.ts +0 -4
  105. package/node_modules/@nodelib/fs.scandir/out/constants.js +0 -17
  106. package/node_modules/@nodelib/fs.scandir/out/index.d.ts +0 -12
  107. package/node_modules/@nodelib/fs.scandir/out/index.js +0 -26
  108. package/node_modules/@nodelib/fs.scandir/out/providers/async.d.ts +0 -7
  109. package/node_modules/@nodelib/fs.scandir/out/providers/async.js +0 -104
  110. package/node_modules/@nodelib/fs.scandir/out/providers/common.d.ts +0 -1
  111. package/node_modules/@nodelib/fs.scandir/out/providers/common.js +0 -13
  112. package/node_modules/@nodelib/fs.scandir/out/providers/sync.d.ts +0 -5
  113. package/node_modules/@nodelib/fs.scandir/out/providers/sync.js +0 -54
  114. package/node_modules/@nodelib/fs.scandir/out/settings.d.ts +0 -20
  115. package/node_modules/@nodelib/fs.scandir/out/settings.js +0 -24
  116. package/node_modules/@nodelib/fs.scandir/out/types/index.d.ts +0 -20
  117. package/node_modules/@nodelib/fs.scandir/out/types/index.js +0 -2
  118. package/node_modules/@nodelib/fs.scandir/out/utils/fs.d.ts +0 -2
  119. package/node_modules/@nodelib/fs.scandir/out/utils/fs.js +0 -19
  120. package/node_modules/@nodelib/fs.scandir/out/utils/index.d.ts +0 -2
  121. package/node_modules/@nodelib/fs.scandir/out/utils/index.js +0 -5
  122. package/node_modules/@nodelib/fs.scandir/package.json +0 -44
  123. package/node_modules/@nodelib/fs.stat/LICENSE +0 -21
  124. package/node_modules/@nodelib/fs.stat/README.md +0 -126
  125. package/node_modules/@nodelib/fs.stat/out/adapters/fs.d.ts +0 -13
  126. package/node_modules/@nodelib/fs.stat/out/adapters/fs.js +0 -17
  127. package/node_modules/@nodelib/fs.stat/out/index.d.ts +0 -12
  128. package/node_modules/@nodelib/fs.stat/out/index.js +0 -26
  129. package/node_modules/@nodelib/fs.stat/out/providers/async.d.ts +0 -4
  130. package/node_modules/@nodelib/fs.stat/out/providers/async.js +0 -36
  131. package/node_modules/@nodelib/fs.stat/out/providers/sync.d.ts +0 -3
  132. package/node_modules/@nodelib/fs.stat/out/providers/sync.js +0 -23
  133. package/node_modules/@nodelib/fs.stat/out/settings.d.ts +0 -16
  134. package/node_modules/@nodelib/fs.stat/out/settings.js +0 -16
  135. package/node_modules/@nodelib/fs.stat/out/types/index.d.ts +0 -4
  136. package/node_modules/@nodelib/fs.stat/out/types/index.js +0 -2
  137. package/node_modules/@nodelib/fs.stat/package.json +0 -37
  138. package/node_modules/@nodelib/fs.walk/LICENSE +0 -21
  139. package/node_modules/@nodelib/fs.walk/README.md +0 -215
  140. package/node_modules/@nodelib/fs.walk/out/index.d.ts +0 -14
  141. package/node_modules/@nodelib/fs.walk/out/index.js +0 -34
  142. package/node_modules/@nodelib/fs.walk/out/providers/async.d.ts +0 -12
  143. package/node_modules/@nodelib/fs.walk/out/providers/async.js +0 -30
  144. package/node_modules/@nodelib/fs.walk/out/providers/index.d.ts +0 -4
  145. package/node_modules/@nodelib/fs.walk/out/providers/index.js +0 -9
  146. package/node_modules/@nodelib/fs.walk/out/providers/stream.d.ts +0 -12
  147. package/node_modules/@nodelib/fs.walk/out/providers/stream.js +0 -34
  148. package/node_modules/@nodelib/fs.walk/out/providers/sync.d.ts +0 -10
  149. package/node_modules/@nodelib/fs.walk/out/providers/sync.js +0 -14
  150. package/node_modules/@nodelib/fs.walk/out/readers/async.d.ts +0 -30
  151. package/node_modules/@nodelib/fs.walk/out/readers/async.js +0 -97
  152. package/node_modules/@nodelib/fs.walk/out/readers/common.d.ts +0 -7
  153. package/node_modules/@nodelib/fs.walk/out/readers/common.js +0 -31
  154. package/node_modules/@nodelib/fs.walk/out/readers/reader.d.ts +0 -6
  155. package/node_modules/@nodelib/fs.walk/out/readers/reader.js +0 -11
  156. package/node_modules/@nodelib/fs.walk/out/readers/sync.d.ts +0 -15
  157. package/node_modules/@nodelib/fs.walk/out/readers/sync.js +0 -59
  158. package/node_modules/@nodelib/fs.walk/out/settings.d.ts +0 -30
  159. package/node_modules/@nodelib/fs.walk/out/settings.js +0 -26
  160. package/node_modules/@nodelib/fs.walk/out/types/index.d.ts +0 -8
  161. package/node_modules/@nodelib/fs.walk/out/types/index.js +0 -2
  162. package/node_modules/@nodelib/fs.walk/package.json +0 -44
  163. package/node_modules/@ungap/structured-clone/.github/workflows/node.js.yml +0 -31
  164. package/node_modules/@ungap/structured-clone/LICENSE +0 -15
  165. package/node_modules/@ungap/structured-clone/README.md +0 -95
  166. package/node_modules/@ungap/structured-clone/cjs/deserialize.js +0 -78
  167. package/node_modules/@ungap/structured-clone/cjs/index.js +0 -27
  168. package/node_modules/@ungap/structured-clone/cjs/json.js +0 -24
  169. package/node_modules/@ungap/structured-clone/cjs/package.json +0 -1
  170. package/node_modules/@ungap/structured-clone/cjs/serialize.js +0 -160
  171. package/node_modules/@ungap/structured-clone/cjs/types.js +0 -22
  172. package/node_modules/@ungap/structured-clone/esm/deserialize.js +0 -79
  173. package/node_modules/@ungap/structured-clone/esm/index.js +0 -25
  174. package/node_modules/@ungap/structured-clone/esm/json.js +0 -21
  175. package/node_modules/@ungap/structured-clone/esm/serialize.js +0 -161
  176. package/node_modules/@ungap/structured-clone/esm/types.js +0 -11
  177. package/node_modules/@ungap/structured-clone/package.json +0 -53
  178. package/node_modules/@ungap/structured-clone/structured-json.js +0 -1
@@ -1,659 +0,0 @@
1
- /**
2
- * @filedescription Object Schema Tests
3
- */
4
- /* global it, describe, beforeEach */
5
-
6
- "use strict";
7
-
8
- //-----------------------------------------------------------------------------
9
- // Requirements
10
- //-----------------------------------------------------------------------------
11
-
12
- const assert = require("chai").assert;
13
- const { ObjectSchema } = require("../src/");
14
-
15
- //-----------------------------------------------------------------------------
16
- // Class
17
- //-----------------------------------------------------------------------------
18
-
19
- describe("ObjectSchema", () => {
20
-
21
- let schema;
22
-
23
- describe("new ObjectSchema()", () => {
24
-
25
- it("should add a new key when a strategy is passed", () => {
26
- schema = new ObjectSchema({
27
- foo: {
28
- merge() {},
29
- validate() {}
30
- }
31
- });
32
-
33
- assert.isTrue(schema.hasKey("foo"));
34
- });
35
-
36
- it("should throw an error when a strategy is missing a merge() method", () => {
37
- assert.throws(() => {
38
- schema = new ObjectSchema({
39
- foo: {
40
- validate() { }
41
- }
42
- });
43
- }, /Definition for key "foo" must have a merge property/);
44
- });
45
-
46
- it("should throw an error when a strategy is missing a merge() method", () => {
47
- assert.throws(() => {
48
- schema = new ObjectSchema();
49
- }, /Schema definitions missing/);
50
- });
51
-
52
- it("should throw an error when a strategy is missing a validate() method", () => {
53
- assert.throws(() => {
54
- schema = new ObjectSchema({
55
- foo: {
56
- merge() { },
57
- }
58
- });
59
- }, /Definition for key "foo" must have a validate\(\) method/);
60
- });
61
-
62
- it("should throw an error when merge is an invalid string", () => {
63
- assert.throws(() => {
64
- new ObjectSchema({
65
- foo: {
66
- merge: "bar",
67
- validate() { }
68
- }
69
- });
70
- }, /key "foo" missing valid merge strategy/);
71
- });
72
-
73
- it("should throw an error when validate is an invalid string", () => {
74
- assert.throws(() => {
75
- new ObjectSchema({
76
- foo: {
77
- merge: "assign",
78
- validate: "s"
79
- }
80
- });
81
- }, /key "foo" missing valid validation strategy/);
82
- });
83
-
84
- });
85
-
86
-
87
- describe("merge()", () => {
88
-
89
- it("should throw an error when an unexpected key is found", () => {
90
- let schema = new ObjectSchema({});
91
-
92
- assert.throws(() => {
93
- schema.merge({ foo: true }, { foo: true });
94
- }, /Unexpected key "foo"/);
95
- });
96
-
97
- it("should throw an error when merge() throws an error", () => {
98
- let schema = new ObjectSchema({
99
- foo: {
100
- merge() {
101
- throw new Error("Boom!");
102
- },
103
- validate() {}
104
- }
105
- });
106
-
107
- assert.throws(() => {
108
- schema.merge({ foo: true }, { foo: true });
109
- }, /Key "foo": Boom!/);
110
-
111
- });
112
-
113
- it("should throw an error when merge() throws an error with a readonly message", () => {
114
- let schema = new ObjectSchema({
115
- foo: {
116
- merge() {
117
- throw {
118
- get message() {
119
- return "Boom!";
120
- }
121
- };
122
- },
123
- validate() {}
124
- }
125
- });
126
-
127
- assert.throws(() => {
128
- schema.merge({ foo: true }, { foo: true });
129
- }, /Key "foo": Boom!/);
130
-
131
- });
132
-
133
- it("should throw an error with custom properties when merge() throws an error with custom properties", () => {
134
- let schema = new ObjectSchema({
135
- foo: {
136
- merge() {
137
- throw {
138
- get message() {
139
- return "Boom!";
140
- },
141
- booya: true
142
- };
143
- },
144
- validate() {}
145
- }
146
- });
147
-
148
- let errorThrown = false;
149
-
150
- try {
151
- schema.merge({ foo: true }, { foo: true });
152
- } catch (ex) {
153
- errorThrown = true;
154
- assert.isTrue(ex.booya);
155
- }
156
-
157
- assert.isTrue(errorThrown);
158
-
159
- });
160
-
161
- it("should call the merge() strategy for one key when called", () => {
162
-
163
- schema = new ObjectSchema({
164
- foo: {
165
- merge() {
166
- return "bar";
167
- },
168
- validate() {}
169
- }
170
- });
171
-
172
- const result = schema.merge({ foo: true }, { foo: false });
173
- assert.propertyVal(result, "foo", "bar");
174
- });
175
-
176
- it("should not call the merge() strategy when both objects don't contain the key", () => {
177
-
178
- let called = false;
179
-
180
- schema = new ObjectSchema({
181
- foo: {
182
- merge() {
183
- called = true;
184
- },
185
- validate() {}
186
- }
187
- });
188
-
189
- schema.merge({}, {});
190
- assert.isFalse(called, "The merge() strategy should not have been called.");
191
- });
192
-
193
- it("should omit returning the key when the merge() strategy returns undefined", () => {
194
- schema = new ObjectSchema({
195
- foo: {
196
- merge() {
197
- return undefined;
198
- },
199
- validate() { }
200
- }
201
- });
202
-
203
- const result = schema.merge({ foo: true }, { foo: false });
204
- assert.notProperty(result, "foo");
205
- });
206
-
207
- it("should call the merge() strategy for two keys when called", () => {
208
- schema = new ObjectSchema({
209
- foo: {
210
- merge() {
211
- return "bar";
212
- },
213
- validate() { }
214
- },
215
- bar: {
216
- merge() {
217
- return "baz";
218
- },
219
- validate() {}
220
- }
221
- });
222
-
223
- const result = schema.merge({ foo: true, bar: 1 }, { foo: true, bar: 2 });
224
- assert.propertyVal(result, "foo", "bar");
225
- assert.propertyVal(result, "bar", "baz");
226
- });
227
-
228
- it("should call the merge() strategy for two keys when called on three objects", () => {
229
- schema = new ObjectSchema({
230
- foo: {
231
- merge() {
232
- return "bar";
233
- },
234
- validate() { }
235
- },
236
- bar: {
237
- merge() {
238
- return "baz";
239
- },
240
- validate() { }
241
- }
242
- });
243
-
244
- const result = schema.merge(
245
- { foo: true, bar: 1 },
246
- { foo: true, bar: 3 },
247
- { foo: false, bar: 2 }
248
- );
249
- assert.propertyVal(result, "foo", "bar");
250
- assert.propertyVal(result, "bar", "baz");
251
- });
252
-
253
- it("should call the merge() strategy when defined as 'overwrite'", () => {
254
- schema = new ObjectSchema({
255
- foo: {
256
- merge: "overwrite",
257
- validate() { }
258
- }
259
- });
260
-
261
- const result = schema.merge(
262
- { foo: true },
263
- { foo: false }
264
- );
265
- assert.propertyVal(result, "foo", false);
266
- });
267
-
268
- it("should call the merge() strategy when defined as 'assign'", () => {
269
- schema = new ObjectSchema({
270
- foo: {
271
- merge: "assign",
272
- validate() { }
273
- }
274
- });
275
-
276
- const result = schema.merge(
277
- { foo: { bar: true } },
278
- { foo: { baz: false } }
279
- );
280
-
281
- assert.strictEqual(result.foo.bar, true);
282
- assert.strictEqual(result.foo.baz, false);
283
- });
284
-
285
- it("should call the merge strategy when there's a subschema", () => {
286
-
287
- schema = new ObjectSchema({
288
- name: {
289
- schema: {
290
- first: {
291
- merge: "replace",
292
- validate: "string"
293
- },
294
- last: {
295
- merge: "replace",
296
- validate: "string"
297
- }
298
- }
299
- }
300
- });
301
-
302
- const result = schema.merge({
303
- name: {
304
- first: "n",
305
- last: "z"
306
- }
307
- }, {
308
- name: {
309
- first: "g"
310
- }
311
- });
312
-
313
- assert.strictEqual(result.name.first, "g");
314
- assert.strictEqual(result.name.last, "z");
315
- });
316
-
317
- it("should return separate objects when using subschema", () => {
318
-
319
- schema = new ObjectSchema({
320
- age: {
321
- merge: "replace",
322
- validate: "number"
323
- },
324
- address: {
325
- schema: {
326
- street: {
327
- schema: {
328
- number: {
329
- merge: "replace",
330
- validate: "number"
331
- },
332
- streetName: {
333
- merge: "replace",
334
- validate: "string"
335
- }
336
- }
337
- },
338
- state: {
339
- merge: "replace",
340
- validate: "string"
341
- }
342
- }
343
- }
344
- });
345
-
346
- const baseObject = {
347
- address: {
348
- street: {
349
- number: 100,
350
- streetName: "Foo St"
351
- },
352
- state: "HA"
353
- }
354
- };
355
-
356
- const result = schema.merge(baseObject, {
357
- age: 29
358
- });
359
-
360
- assert.notStrictEqual(result.address.street, baseObject.address.street);
361
- assert.deepStrictEqual(result.address, baseObject.address);
362
- });
363
-
364
- it("should not error when calling the merge strategy when there's a subschema and no matching key in second object", () => {
365
-
366
- schema = new ObjectSchema({
367
- name: {
368
- schema: {
369
- first: {
370
- merge: "replace",
371
- validate: "string"
372
- },
373
- last: {
374
- merge: "replace",
375
- validate: "string"
376
- }
377
- }
378
- }
379
- });
380
-
381
- const result = schema.merge({
382
- name: {
383
- first: "n",
384
- last: "z"
385
- }
386
- }, {
387
- });
388
-
389
- assert.strictEqual(result.name.first, "n");
390
- assert.strictEqual(result.name.last, "z");
391
- });
392
-
393
- it("should not error when calling the merge strategy when there's multiple subschemas and no matching key in second object", () => {
394
-
395
- schema = new ObjectSchema({
396
- user: {
397
- schema: {
398
- name: {
399
- schema: {
400
- first: {
401
- merge: "replace",
402
- validate: "string"
403
- },
404
- last: {
405
- merge: "replace",
406
- validate: "string"
407
- }
408
- }
409
- }
410
-
411
- }
412
- }
413
- });
414
-
415
- const result = schema.merge({
416
- user: {
417
- name: {
418
- first: "n",
419
- last: "z"
420
- }
421
- }
422
- }, {
423
- });
424
-
425
- assert.strictEqual(result.user.name.first, "n");
426
- assert.strictEqual(result.user.name.last, "z");
427
- });
428
-
429
-
430
- });
431
-
432
- describe("validate()", () => {
433
-
434
- it("should throw an error when an unexpected key is found", () => {
435
- let schema = new ObjectSchema({});
436
- assert.throws(() => {
437
- schema.validate({ foo: true });
438
- }, /Unexpected key "foo"/);
439
- });
440
-
441
- it("should not throw an error when an expected key is found", () => {
442
- schema = new ObjectSchema({
443
- foo: {
444
- merge() {
445
- return "bar";
446
- },
447
- validate() {}
448
- }
449
- });
450
-
451
- schema.validate({ foo: true });
452
- });
453
-
454
- it("should pass the property value into validate() when key is found", () => {
455
- schema = new ObjectSchema({
456
- foo: {
457
- merge() {
458
- return "bar";
459
- },
460
- validate(value) {
461
- assert.isTrue(value);
462
- }
463
- }
464
- });
465
-
466
- schema.validate({ foo: true });
467
- });
468
-
469
- it("should not throw an error when expected keys are found", () => {
470
- schema = new ObjectSchema({
471
- foo: {
472
- merge() {
473
- return "bar";
474
- },
475
- validate() {}
476
- },
477
- bar: {
478
- merge() {
479
- return "baz";
480
- },
481
- validate() {}
482
- }
483
- });
484
-
485
- schema.validate({ foo: true, bar: true });
486
- });
487
-
488
- it("should not throw an error when expected keys are found with required keys", () => {
489
- schema = new ObjectSchema({
490
- foo: {
491
- merge() {
492
- return "bar";
493
- },
494
- validate() { }
495
- },
496
- bar: {
497
- requires: ["foo"],
498
- merge() {
499
- return "baz";
500
- },
501
- validate() { }
502
- }
503
- });
504
-
505
- schema.validate({ foo: true, bar: true });
506
- });
507
-
508
- it("should throw an error when expected keys are found without required keys", () => {
509
- schema = new ObjectSchema({
510
- foo: {
511
- merge() {
512
- return "bar";
513
- },
514
- validate() { }
515
- },
516
- baz: {
517
- merge() {
518
- return "baz";
519
- },
520
- validate() { }
521
- },
522
- bar: {
523
- name: "bar",
524
- requires: ["foo", "baz"],
525
- merge() { },
526
- validate() { }
527
- }
528
- });
529
-
530
- assert.throws(() => {
531
- schema.validate({ bar: true });
532
- }, /Key "bar" requires keys "foo", "baz"./);
533
- });
534
-
535
-
536
- it("should throw an error when an expected key is found but is invalid", () => {
537
-
538
- schema = new ObjectSchema({
539
- foo: {
540
- merge() {
541
- return "bar";
542
- },
543
- validate() {
544
- throw new Error("Invalid key.");
545
- }
546
- }
547
- });
548
-
549
- assert.throws(() => {
550
- schema.validate({ foo: true });
551
- }, /Key "foo": Invalid key/);
552
- });
553
-
554
- it("should throw an error when an expected key is found but is invalid with a string validator", () => {
555
-
556
- schema = new ObjectSchema({
557
- foo: {
558
- merge() {
559
- return "bar";
560
- },
561
- validate: "string"
562
- }
563
- });
564
-
565
- assert.throws(() => {
566
- schema.validate({ foo: true });
567
- }, /Key "foo": Expected a string/);
568
- });
569
-
570
- it("should throw an error when an expected key is found but is invalid with a number validator", () => {
571
-
572
- schema = new ObjectSchema({
573
- foo: {
574
- merge() {
575
- return "bar";
576
- },
577
- validate: "number"
578
- }
579
- });
580
-
581
- assert.throws(() => {
582
- schema.validate({ foo: true });
583
- }, /Key "foo": Expected a number/);
584
- });
585
-
586
- it("should throw an error when a required key is missing", () => {
587
-
588
- schema = new ObjectSchema({
589
- foo: {
590
- required: true,
591
- merge() {
592
- return "bar";
593
- },
594
- validate() {}
595
- }
596
- });
597
-
598
- assert.throws(() => {
599
- schema.validate({});
600
- }, /Missing required key "foo"/);
601
- });
602
-
603
- it("should throw an error when a subschema is provided and the value doesn't validate", () => {
604
-
605
- schema = new ObjectSchema({
606
- name: {
607
- schema: {
608
- first: {
609
- merge: "replace",
610
- validate: "string"
611
- },
612
- last: {
613
- merge: "replace",
614
- validate: "string"
615
- }
616
- }
617
- }
618
- });
619
-
620
- assert.throws(() => {
621
- schema.validate({
622
- name: {
623
- first: 123,
624
- last: "z"
625
- }
626
- });
627
-
628
- }, /Key "name": Key "first": Expected a string/);
629
- });
630
-
631
- it("should not throw an error when a subschema is provided and the value validates", () => {
632
-
633
- schema = new ObjectSchema({
634
- name: {
635
- schema: {
636
- first: {
637
- merge: "replace",
638
- validate: "string"
639
- },
640
- last: {
641
- merge: "replace",
642
- validate: "string"
643
- }
644
- }
645
- }
646
- });
647
-
648
- schema.validate({
649
- name: {
650
- first: "n",
651
- last: "z"
652
- }
653
- });
654
-
655
- });
656
-
657
- });
658
-
659
- });