@twin.org/entity 0.0.1-next.7 → 0.0.1-next.70

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.
@@ -61,7 +61,7 @@ function property(options) {
61
61
  const entitySchema = DecoratorHelper.getSchema(target);
62
62
  entitySchema.properties ??= [];
63
63
  const idx = entitySchema.properties.findIndex(p => p.property === propertyKey);
64
- if (idx >= 0) {
64
+ if (idx !== -1) {
65
65
  entitySchema.properties[idx] = {
66
66
  ...options,
67
67
  property: propertyKey
@@ -533,6 +533,60 @@ class EntitySchemaHelper {
533
533
  }
534
534
  return finalSortKeys;
535
535
  }
536
+ /**
537
+ * Validate the entity against the schema.
538
+ * @param entity The entity to validate.
539
+ * @param entitySchema The schema to validate against.
540
+ * @throws If the entity is invalid.
541
+ */
542
+ static validateEntity(entity, entitySchema) {
543
+ core.Guards.object(EntitySchemaHelper._CLASS_NAME, "entity", entity);
544
+ core.Guards.object(EntitySchemaHelper._CLASS_NAME, "entitySchema", entitySchema);
545
+ const properties = entitySchema.properties ?? [];
546
+ if (properties.length === 0 && core.Is.objectValue(entity)) {
547
+ throw new core.GeneralError(EntitySchemaHelper._CLASS_NAME, "invalidEntityProperties");
548
+ }
549
+ const allKeys = Object.keys(entity);
550
+ for (const prop of properties) {
551
+ const idx = allKeys.indexOf(prop.property);
552
+ if (idx !== -1) {
553
+ allKeys.splice(idx, 1);
554
+ }
555
+ const value = entity[prop.property];
556
+ if (core.Is.empty(value)) {
557
+ // If the value is empty but the property is not optional, then it's invalid
558
+ if (!prop.optional) {
559
+ throw new core.GeneralError(EntitySchemaHelper._CLASS_NAME, "invalidOptional", {
560
+ property: prop.property,
561
+ type: prop.type
562
+ });
563
+ }
564
+ }
565
+ else if (prop.type === "integer" && core.Is.integer(value)) ;
566
+ else if (prop.type === "object" &&
567
+ (core.Is.object(value) ||
568
+ core.Is.array(value) ||
569
+ core.Is.string(value) ||
570
+ core.Is.number(value) ||
571
+ core.Is.boolean(value) ||
572
+ core.Is.null(value))) ;
573
+ else if (prop.type === "array" && core.Is.array(value)) ;
574
+ else if (prop.type !== typeof value) {
575
+ // The schema type does not match the value type
576
+ throw new core.GeneralError(EntitySchemaHelper._CLASS_NAME, "invalidEntityProperty", {
577
+ value,
578
+ property: prop.property,
579
+ type: prop.type
580
+ });
581
+ }
582
+ }
583
+ if (allKeys.length > 0) {
584
+ // There are keys in the entity that are not in the schema
585
+ throw new core.GeneralError(EntitySchemaHelper._CLASS_NAME, "invalidEntityKeys", {
586
+ keys: allKeys.join(", ")
587
+ });
588
+ }
589
+ }
536
590
  }
537
591
 
538
592
  // Copyright 2024 IOTA Stiftung.
@@ -572,7 +626,9 @@ class EntitySorter {
572
626
  */
573
627
  static compare(entity1, entity2, prop, type, direction = SortDirection.Ascending) {
574
628
  let res = 0;
575
- if (!core.Is.empty(entity1[prop]) && !core.Is.empty(entity2[prop])) {
629
+ const hasProp1 = !core.Is.empty(entity1[prop]);
630
+ const hasProp2 = !core.Is.empty(entity2[prop]);
631
+ if (hasProp1 && hasProp2) {
576
632
  if (type === "number" || type === "integer") {
577
633
  res = entity1[prop] - entity2[prop];
578
634
  }
@@ -593,6 +649,12 @@ class EntitySorter {
593
649
  res = entity1[prop].localeCompare(entity2[prop]);
594
650
  }
595
651
  }
652
+ else if (hasProp1) {
653
+ res = -1;
654
+ }
655
+ else {
656
+ res = 1;
657
+ }
596
658
  return direction === SortDirection.Ascending ? res : res * -1;
597
659
  }
598
660
  }
@@ -59,7 +59,7 @@ function property(options) {
59
59
  const entitySchema = DecoratorHelper.getSchema(target);
60
60
  entitySchema.properties ??= [];
61
61
  const idx = entitySchema.properties.findIndex(p => p.property === propertyKey);
62
- if (idx >= 0) {
62
+ if (idx !== -1) {
63
63
  entitySchema.properties[idx] = {
64
64
  ...options,
65
65
  property: propertyKey
@@ -531,6 +531,60 @@ class EntitySchemaHelper {
531
531
  }
532
532
  return finalSortKeys;
533
533
  }
534
+ /**
535
+ * Validate the entity against the schema.
536
+ * @param entity The entity to validate.
537
+ * @param entitySchema The schema to validate against.
538
+ * @throws If the entity is invalid.
539
+ */
540
+ static validateEntity(entity, entitySchema) {
541
+ Guards.object(EntitySchemaHelper._CLASS_NAME, "entity", entity);
542
+ Guards.object(EntitySchemaHelper._CLASS_NAME, "entitySchema", entitySchema);
543
+ const properties = entitySchema.properties ?? [];
544
+ if (properties.length === 0 && Is.objectValue(entity)) {
545
+ throw new GeneralError(EntitySchemaHelper._CLASS_NAME, "invalidEntityProperties");
546
+ }
547
+ const allKeys = Object.keys(entity);
548
+ for (const prop of properties) {
549
+ const idx = allKeys.indexOf(prop.property);
550
+ if (idx !== -1) {
551
+ allKeys.splice(idx, 1);
552
+ }
553
+ const value = entity[prop.property];
554
+ if (Is.empty(value)) {
555
+ // If the value is empty but the property is not optional, then it's invalid
556
+ if (!prop.optional) {
557
+ throw new GeneralError(EntitySchemaHelper._CLASS_NAME, "invalidOptional", {
558
+ property: prop.property,
559
+ type: prop.type
560
+ });
561
+ }
562
+ }
563
+ else if (prop.type === "integer" && Is.integer(value)) ;
564
+ else if (prop.type === "object" &&
565
+ (Is.object(value) ||
566
+ Is.array(value) ||
567
+ Is.string(value) ||
568
+ Is.number(value) ||
569
+ Is.boolean(value) ||
570
+ Is.null(value))) ;
571
+ else if (prop.type === "array" && Is.array(value)) ;
572
+ else if (prop.type !== typeof value) {
573
+ // The schema type does not match the value type
574
+ throw new GeneralError(EntitySchemaHelper._CLASS_NAME, "invalidEntityProperty", {
575
+ value,
576
+ property: prop.property,
577
+ type: prop.type
578
+ });
579
+ }
580
+ }
581
+ if (allKeys.length > 0) {
582
+ // There are keys in the entity that are not in the schema
583
+ throw new GeneralError(EntitySchemaHelper._CLASS_NAME, "invalidEntityKeys", {
584
+ keys: allKeys.join(", ")
585
+ });
586
+ }
587
+ }
534
588
  }
535
589
 
536
590
  // Copyright 2024 IOTA Stiftung.
@@ -570,7 +624,9 @@ class EntitySorter {
570
624
  */
571
625
  static compare(entity1, entity2, prop, type, direction = SortDirection.Ascending) {
572
626
  let res = 0;
573
- if (!Is.empty(entity1[prop]) && !Is.empty(entity2[prop])) {
627
+ const hasProp1 = !Is.empty(entity1[prop]);
628
+ const hasProp2 = !Is.empty(entity2[prop]);
629
+ if (hasProp1 && hasProp2) {
574
630
  if (type === "number" || type === "integer") {
575
631
  res = entity1[prop] - entity2[prop];
576
632
  }
@@ -591,6 +647,12 @@ class EntitySorter {
591
647
  res = entity1[prop].localeCompare(entity2[prop]);
592
648
  }
593
649
  }
650
+ else if (hasProp1) {
651
+ res = -1;
652
+ }
653
+ else {
654
+ res = 1;
655
+ }
594
656
  return direction === SortDirection.Ascending ? res : res * -1;
595
657
  }
596
658
  }
@@ -35,4 +35,11 @@ export declare class EntitySchemaHelper {
35
35
  property: keyof T;
36
36
  sortDirection: SortDirection;
37
37
  }[]): IEntitySort<T>[] | undefined;
38
+ /**
39
+ * Validate the entity against the schema.
40
+ * @param entity The entity to validate.
41
+ * @param entitySchema The schema to validate against.
42
+ * @throws If the entity is invalid.
43
+ */
44
+ static validateEntity<T>(entity: T, entitySchema: IEntitySchema<T>): void;
38
45
  }
package/docs/changelog.md CHANGED
@@ -1,5 +1,316 @@
1
1
  # @twin.org/entity - Changelog
2
2
 
3
- ## 0.0.1-next.7
3
+ ## [0.0.1-next.70](https://github.com/twinfoundation/framework/compare/entity-v0.0.1-next.69...entity-v0.0.1-next.70) (2025-07-02)
4
+
5
+
6
+ ### Features
7
+
8
+ * add guards arrayEndsWith and arrayStartsWith ([95d875e](https://github.com/twinfoundation/framework/commit/95d875ec8ccb4713c145fdde941d4cfedcec2ed3))
9
+ * relocate core packages from tools ([bcab8f3](https://github.com/twinfoundation/framework/commit/bcab8f3160442ea4fcaf442947462504f3d6a17d))
10
+ * use new shared store mechanism ([#131](https://github.com/twinfoundation/framework/issues/131)) ([934385b](https://github.com/twinfoundation/framework/commit/934385b2fbaf9f5c00a505ebf9d093bd5a425f55))
11
+
12
+
13
+ ### Dependencies
14
+
15
+ * The following workspace dependencies were updated
16
+ * dependencies
17
+ * @twin.org/nameof bumped from 0.0.1-next.69 to 0.0.1-next.70
18
+ * @twin.org/core bumped from 0.0.1-next.69 to 0.0.1-next.70
19
+ * devDependencies
20
+ * @twin.org/nameof-transformer bumped from 0.0.1-next.69 to 0.0.1-next.70
21
+ * @twin.org/nameof-vitest-plugin bumped from 0.0.1-next.69 to 0.0.1-next.70
22
+
23
+ ## [0.0.1-next.69](https://github.com/twinfoundation/framework/compare/entity-v0.0.1-next.68...entity-v0.0.1-next.69) (2025-07-02)
24
+
25
+
26
+ ### Features
27
+
28
+ * add guards arrayEndsWith and arrayStartsWith ([95d875e](https://github.com/twinfoundation/framework/commit/95d875ec8ccb4713c145fdde941d4cfedcec2ed3))
29
+ * relocate core packages from tools ([bcab8f3](https://github.com/twinfoundation/framework/commit/bcab8f3160442ea4fcaf442947462504f3d6a17d))
30
+ * use new shared store mechanism ([#131](https://github.com/twinfoundation/framework/issues/131)) ([934385b](https://github.com/twinfoundation/framework/commit/934385b2fbaf9f5c00a505ebf9d093bd5a425f55))
31
+
32
+
33
+ ### Dependencies
34
+
35
+ * The following workspace dependencies were updated
36
+ * dependencies
37
+ * @twin.org/nameof bumped from 0.0.1-next.68 to 0.0.1-next.69
38
+ * @twin.org/core bumped from 0.0.1-next.68 to 0.0.1-next.69
39
+ * devDependencies
40
+ * @twin.org/nameof-transformer bumped from 0.0.1-next.68 to 0.0.1-next.69
41
+ * @twin.org/nameof-vitest-plugin bumped from 0.0.1-next.68 to 0.0.1-next.69
42
+
43
+ ## [0.0.1-next.68](https://github.com/twinfoundation/framework/compare/entity-v0.0.1-next.67...entity-v0.0.1-next.68) (2025-07-02)
44
+
45
+
46
+ ### Features
47
+
48
+ * relocate core packages from tools ([bcab8f3](https://github.com/twinfoundation/framework/commit/bcab8f3160442ea4fcaf442947462504f3d6a17d))
49
+
50
+
51
+ ### Dependencies
52
+
53
+ * The following workspace dependencies were updated
54
+ * dependencies
55
+ * @twin.org/nameof bumped from 0.0.1-next.67 to 0.0.1-next.68
56
+ * @twin.org/core bumped from 0.0.1-next.67 to 0.0.1-next.68
57
+ * devDependencies
58
+ * @twin.org/nameof-transformer bumped from 0.0.1-next.67 to 0.0.1-next.68
59
+ * @twin.org/nameof-vitest-plugin bumped from 0.0.1-next.67 to 0.0.1-next.68
60
+
61
+ ## [0.0.1-next.67](https://github.com/twinfoundation/framework/compare/entity-v0.0.1-next.66...entity-v0.0.1-next.67) (2025-06-26)
62
+
63
+
64
+ ### Miscellaneous Chores
65
+
66
+ * **entity:** Synchronize repo versions
67
+
68
+
69
+ ### Dependencies
70
+
71
+ * The following workspace dependencies were updated
72
+ * dependencies
73
+ * @twin.org/core bumped from 0.0.1-next.66 to 0.0.1-next.67
74
+
75
+ ## [0.0.1-next.66](https://github.com/twinfoundation/framework/compare/entity-v0.0.1-next.65...entity-v0.0.1-next.66) (2025-06-26)
76
+
77
+
78
+ ### Miscellaneous Chores
79
+
80
+ * **entity:** Synchronize repo versions
81
+
82
+
83
+ ### Dependencies
84
+
85
+ * The following workspace dependencies were updated
86
+ * dependencies
87
+ * @twin.org/core bumped from 0.0.1-next.65 to 0.0.1-next.66
88
+
89
+ ## [0.0.1-next.65](https://github.com/twinfoundation/framework/compare/entity-v0.0.1-next.64...entity-v0.0.1-next.65) (2025-06-19)
90
+
91
+
92
+ ### Miscellaneous Chores
93
+
94
+ * **entity:** Synchronize repo versions
95
+
96
+
97
+ ### Dependencies
98
+
99
+ * The following workspace dependencies were updated
100
+ * dependencies
101
+ * @twin.org/core bumped from 0.0.1-next.64 to 0.0.1-next.65
102
+
103
+ ## [0.0.1-next.64](https://github.com/twinfoundation/framework/compare/entity-v0.0.1-next.63...entity-v0.0.1-next.64) (2025-06-19)
104
+
105
+
106
+ ### Miscellaneous Chores
107
+
108
+ * **entity:** Synchronize repo versions
109
+
110
+
111
+ ### Dependencies
112
+
113
+ * The following workspace dependencies were updated
114
+ * dependencies
115
+ * @twin.org/core bumped from 0.0.1-next.63 to 0.0.1-next.64
116
+
117
+ ## [0.0.1-next.63](https://github.com/twinfoundation/framework/compare/entity-v0.0.1-next.62...entity-v0.0.1-next.63) (2025-06-18)
118
+
119
+
120
+ ### Miscellaneous Chores
121
+
122
+ * **entity:** Synchronize repo versions
123
+
124
+
125
+ ### Dependencies
126
+
127
+ * The following workspace dependencies were updated
128
+ * dependencies
129
+ * @twin.org/core bumped from 0.0.1-next.62 to 0.0.1-next.63
130
+
131
+ ## [0.0.1-next.62](https://github.com/twinfoundation/framework/compare/entity-v0.0.1-next.61...entity-v0.0.1-next.62) (2025-06-17)
132
+
133
+
134
+ ### Features
135
+
136
+ * add guards arrayEndsWith and arrayStartsWith ([95d875e](https://github.com/twinfoundation/framework/commit/95d875ec8ccb4713c145fdde941d4cfedcec2ed3))
137
+ * use new shared store mechanism ([#131](https://github.com/twinfoundation/framework/issues/131)) ([934385b](https://github.com/twinfoundation/framework/commit/934385b2fbaf9f5c00a505ebf9d093bd5a425f55))
138
+
139
+
140
+ ### Dependencies
141
+
142
+ * The following workspace dependencies were updated
143
+ * dependencies
144
+ * @twin.org/core bumped from 0.0.1-next.61 to 0.0.1-next.62
145
+
146
+ ## [0.0.1-next.61](https://github.com/twinfoundation/framework/compare/entity-v0.0.1-next.60...entity-v0.0.1-next.61) (2025-06-17)
147
+
148
+
149
+ ### Miscellaneous Chores
150
+
151
+ * **entity:** Synchronize repo versions
152
+
153
+
154
+ ### Dependencies
155
+
156
+ * The following workspace dependencies were updated
157
+ * dependencies
158
+ * @twin.org/core bumped from 0.0.1-next.60 to 0.0.1-next.61
159
+
160
+ ## [0.0.1-next.60](https://github.com/twinfoundation/framework/compare/entity-v0.0.1-next.59...entity-v0.0.1-next.60) (2025-06-17)
161
+
162
+
163
+ ### Miscellaneous Chores
164
+
165
+ * **entity:** Synchronize repo versions
166
+
167
+
168
+ ### Dependencies
169
+
170
+ * The following workspace dependencies were updated
171
+ * dependencies
172
+ * @twin.org/core bumped from 0.0.1-next.59 to 0.0.1-next.60
173
+
174
+ ## [0.0.1-next.59](https://github.com/twinfoundation/framework/compare/entity-v0.0.1-next.58...entity-v0.0.1-next.59) (2025-06-17)
175
+
176
+
177
+ ### Miscellaneous Chores
178
+
179
+ * **entity:** Synchronize repo versions
180
+
181
+
182
+ ### Dependencies
183
+
184
+ * The following workspace dependencies were updated
185
+ * dependencies
186
+ * @twin.org/core bumped from 0.0.1-next.58 to 0.0.1-next.59
187
+
188
+ ## [0.0.1-next.58](https://github.com/twinfoundation/framework/compare/entity-v0.0.1-next.57...entity-v0.0.1-next.58) (2025-06-13)
189
+
190
+
191
+ ### Miscellaneous Chores
192
+
193
+ * **entity:** Synchronize repo versions
194
+
195
+
196
+ ### Dependencies
197
+
198
+ * The following workspace dependencies were updated
199
+ * dependencies
200
+ * @twin.org/core bumped from 0.0.1-next.57 to 0.0.1-next.58
201
+
202
+ ## [0.0.1-next.57](https://github.com/twinfoundation/framework/compare/entity-v0.0.1-next.56...entity-v0.0.1-next.57) (2025-06-10)
203
+
204
+
205
+ ### Features
206
+
207
+ * add guards arrayEndsWith and arrayStartsWith ([95d875e](https://github.com/twinfoundation/framework/commit/95d875ec8ccb4713c145fdde941d4cfedcec2ed3))
208
+
209
+
210
+ ### Dependencies
211
+
212
+ * The following workspace dependencies were updated
213
+ * dependencies
214
+ * @twin.org/core bumped from 0.0.1-next.56 to 0.0.1-next.57
215
+
216
+ ## [0.0.1-next.56](https://github.com/twinfoundation/framework/compare/entity-v0.0.1-next.55...entity-v0.0.1-next.56) (2025-05-08)
217
+
218
+
219
+ ### Miscellaneous Chores
220
+
221
+ * **entity:** Synchronize repo versions
222
+
223
+
224
+ ### Dependencies
225
+
226
+ * The following workspace dependencies were updated
227
+ * dependencies
228
+ * @twin.org/core bumped from 0.0.1-next.55 to 0.0.1-next.56
229
+
230
+ ## [0.0.1-next.55](https://github.com/twinfoundation/framework/compare/entity-v0.0.1-next.54...entity-v0.0.1-next.55) (2025-05-07)
231
+
232
+
233
+ ### Miscellaneous Chores
234
+
235
+ * **entity:** Synchronize repo versions
236
+
237
+
238
+ ### Dependencies
239
+
240
+ * The following workspace dependencies were updated
241
+ * dependencies
242
+ * @twin.org/core bumped from 0.0.1-next.54 to 0.0.1-next.55
243
+
244
+ ## [0.0.1-next.54](https://github.com/twinfoundation/framework/compare/entity-v0.0.1-next.53...entity-v0.0.1-next.54) (2025-05-06)
245
+
246
+
247
+ ### Miscellaneous Chores
248
+
249
+ * **entity:** Synchronize repo versions
250
+
251
+
252
+ ### Dependencies
253
+
254
+ * The following workspace dependencies were updated
255
+ * dependencies
256
+ * @twin.org/core bumped from 0.0.1-next.53 to 0.0.1-next.54
257
+
258
+ ## [0.0.1-next.53](https://github.com/twinfoundation/framework/compare/entity-v0.0.1-next.52...entity-v0.0.1-next.53) (2025-05-01)
259
+
260
+
261
+ ### Miscellaneous Chores
262
+
263
+ * **entity:** Synchronize repo versions
264
+
265
+
266
+ ### Dependencies
267
+
268
+ * The following workspace dependencies were updated
269
+ * dependencies
270
+ * @twin.org/core bumped from 0.0.1-next.52 to 0.0.1-next.53
271
+
272
+ ## [0.0.1-next.52](https://github.com/twinfoundation/framework/compare/entity-v0.0.1-next.51...entity-v0.0.1-next.52) (2025-04-17)
273
+
274
+
275
+ ### Features
276
+
277
+ * use new shared store mechanism ([#131](https://github.com/twinfoundation/framework/issues/131)) ([934385b](https://github.com/twinfoundation/framework/commit/934385b2fbaf9f5c00a505ebf9d093bd5a425f55))
278
+
279
+
280
+ ### Dependencies
281
+
282
+ * The following workspace dependencies were updated
283
+ * dependencies
284
+ * @twin.org/core bumped from 0.0.1-next.51 to 0.0.1-next.52
285
+
286
+ ## [0.0.1-next.51](https://github.com/twinfoundation/framework/compare/entity-v0.0.1-next.50...entity-v0.0.1-next.51) (2025-03-27)
287
+
288
+
289
+ ### Miscellaneous Chores
290
+
291
+ * **entity:** Synchronize repo versions
292
+
293
+
294
+ ### Dependencies
295
+
296
+ * The following workspace dependencies were updated
297
+ * dependencies
298
+ * @twin.org/core bumped from 0.0.1-next.50 to 0.0.1-next.51
299
+
300
+ ## [0.0.1-next.50](https://github.com/twinfoundation/framework/compare/entity-v0.0.1-next.49...entity-v0.0.1-next.50) (2025-03-26)
301
+
302
+
303
+ ### Miscellaneous Chores
304
+
305
+ * **entity:** Synchronize repo versions
306
+
307
+
308
+ ### Dependencies
309
+
310
+ * The following workspace dependencies were updated
311
+ * dependencies
312
+ * @twin.org/core bumped from 0.0.1-next.49 to 0.0.1-next.50
313
+
314
+ ## 0.0.1-next.49
4
315
 
5
316
  - Initial Release
@@ -4,13 +4,13 @@ Class to help with decorators.
4
4
 
5
5
  ## Constructors
6
6
 
7
- ### new DecoratorHelper()
7
+ ### Constructor
8
8
 
9
- > **new DecoratorHelper**(): [`DecoratorHelper`](DecoratorHelper.md)
9
+ > **new DecoratorHelper**(): `DecoratorHelper`
10
10
 
11
11
  #### Returns
12
12
 
13
- [`DecoratorHelper`](DecoratorHelper.md)
13
+ `DecoratorHelper`
14
14
 
15
15
  ## Methods
16
16
 
@@ -22,11 +22,15 @@ Get the schema from the reflection metadata.
22
22
 
23
23
  #### Type Parameters
24
24
 
25
- **T** = `unknown`
25
+ ##### T
26
+
27
+ `T` = `unknown`
26
28
 
27
29
  #### Parameters
28
30
 
29
- **target**: `any`
31
+ ##### target
32
+
33
+ `any`
30
34
 
31
35
  The object to get the schema data from.
32
36
 
@@ -46,15 +50,21 @@ Set the schema from the reflection metadata.
46
50
 
47
51
  #### Type Parameters
48
52
 
49
- **T** = `unknown`
53
+ ##### T
54
+
55
+ `T` = `unknown`
50
56
 
51
57
  #### Parameters
52
58
 
53
- **target**: `any`
59
+ ##### target
60
+
61
+ `any`
54
62
 
55
63
  The object to get the schema data from.
56
64
 
57
- **entitySchema**: [`IEntitySchema`](../interfaces/IEntitySchema.md)\<`T`\>
65
+ ##### entitySchema
66
+
67
+ [`IEntitySchema`](../interfaces/IEntitySchema.md)\<`T`\>
58
68
 
59
69
  The schema to set.
60
70
 
@@ -4,33 +4,39 @@ Class to perform condition checks.
4
4
 
5
5
  ## Constructors
6
6
 
7
- ### new EntityConditions()
7
+ ### Constructor
8
8
 
9
- > **new EntityConditions**(): [`EntityConditions`](EntityConditions.md)
9
+ > **new EntityConditions**(): `EntityConditions`
10
10
 
11
11
  #### Returns
12
12
 
13
- [`EntityConditions`](EntityConditions.md)
13
+ `EntityConditions`
14
14
 
15
15
  ## Methods
16
16
 
17
17
  ### check()
18
18
 
19
- > `static` **check**\<`T`\>(`entity`, `condition`?): `boolean`
19
+ > `static` **check**\<`T`\>(`entity`, `condition?`): `boolean`
20
20
 
21
21
  See if the entity matches the conditions.
22
22
 
23
23
  #### Type Parameters
24
24
 
25
- **T**
25
+ ##### T
26
+
27
+ `T`
26
28
 
27
29
  #### Parameters
28
30
 
29
- **entity**: `T`
31
+ ##### entity
32
+
33
+ `T`
30
34
 
31
35
  The entity to test.
32
36
 
33
- **condition?**: [`EntityCondition`](../type-aliases/EntityCondition.md)\<`T`\>
37
+ ##### condition?
38
+
39
+ [`EntityCondition`](../type-aliases/EntityCondition.md)\<`T`\>
34
40
 
35
41
  The conditions to test.
36
42
 
@@ -50,15 +56,21 @@ See if the entity matches the conditions.
50
56
 
51
57
  #### Type Parameters
52
58
 
53
- **T**
59
+ ##### T
60
+
61
+ `T`
54
62
 
55
63
  #### Parameters
56
64
 
57
- **entity**: `T`
65
+ ##### entity
66
+
67
+ `T`
58
68
 
59
69
  The entity to test.
60
70
 
61
- **comparator**: [`IComparator`](../interfaces/IComparator.md)
71
+ ##### comparator
72
+
73
+ [`IComparator`](../interfaces/IComparator.md)
62
74
 
63
75
  The condition to test.
64
76
 
@@ -4,13 +4,13 @@ Class to help with entity schema operations.
4
4
 
5
5
  ## Constructors
6
6
 
7
- ### new EntitySchemaHelper()
7
+ ### Constructor
8
8
 
9
- > **new EntitySchemaHelper**(): [`EntitySchemaHelper`](EntitySchemaHelper.md)
9
+ > **new EntitySchemaHelper**(): `EntitySchemaHelper`
10
10
 
11
11
  #### Returns
12
12
 
13
- [`EntitySchemaHelper`](EntitySchemaHelper.md)
13
+ `EntitySchemaHelper`
14
14
 
15
15
  ## Methods
16
16
 
@@ -22,11 +22,15 @@ Get the schema for the specified object.
22
22
 
23
23
  #### Type Parameters
24
24
 
25
- **T** = `unknown`
25
+ ##### T
26
+
27
+ `T` = `unknown`
26
28
 
27
29
  #### Parameters
28
30
 
29
- **target**: `any`
31
+ ##### target
32
+
33
+ `any`
30
34
 
31
35
  The object to get the schema data for.
32
36
 
@@ -46,11 +50,15 @@ Get the primary key from the entity schema.
46
50
 
47
51
  #### Type Parameters
48
52
 
49
- **T**
53
+ ##### T
54
+
55
+ `T`
50
56
 
51
57
  #### Parameters
52
58
 
53
- **entitySchema**: [`IEntitySchema`](../interfaces/IEntitySchema.md)\<`T`\>
59
+ ##### entitySchema
60
+
61
+ [`IEntitySchema`](../interfaces/IEntitySchema.md)\<`T`\>
54
62
 
55
63
  The entity schema to find the primary key from.
56
64
 
@@ -74,11 +82,15 @@ Get the sort properties from the schema.
74
82
 
75
83
  #### Type Parameters
76
84
 
77
- **T**
85
+ ##### T
86
+
87
+ `T`
78
88
 
79
89
  #### Parameters
80
90
 
81
- **entitySchema**: [`IEntitySchema`](../interfaces/IEntitySchema.md)\<`T`\>
91
+ ##### entitySchema
92
+
93
+ [`IEntitySchema`](../interfaces/IEntitySchema.md)\<`T`\>
82
94
 
83
95
  The entity schema to find the primary key from.
84
96
 
@@ -92,21 +104,27 @@ The sort keys from the schema or undefined if there are none.
92
104
 
93
105
  ### buildSortProperties()
94
106
 
95
- > `static` **buildSortProperties**\<`T`\>(`entitySchema`, `overrideSortKeys`?): `undefined` \| [`IEntitySort`](../interfaces/IEntitySort.md)\<`T`\>[]
107
+ > `static` **buildSortProperties**\<`T`\>(`entitySchema`, `overrideSortKeys?`): `undefined` \| [`IEntitySort`](../interfaces/IEntitySort.md)\<`T`\>[]
96
108
 
97
109
  Build sort properties from the schema and override if necessary.
98
110
 
99
111
  #### Type Parameters
100
112
 
101
- **T**
113
+ ##### T
114
+
115
+ `T`
102
116
 
103
117
  #### Parameters
104
118
 
105
- **entitySchema**: [`IEntitySchema`](../interfaces/IEntitySchema.md)\<`T`\>
119
+ ##### entitySchema
120
+
121
+ [`IEntitySchema`](../interfaces/IEntitySchema.md)\<`T`\>
106
122
 
107
123
  The entity schema to retrieve the default sort keys.
108
124
 
109
- **overrideSortKeys?**: `object`[]
125
+ ##### overrideSortKeys?
126
+
127
+ `object`[]
110
128
 
111
129
  The override sort keys.
112
130
 
@@ -115,3 +133,39 @@ The override sort keys.
115
133
  `undefined` \| [`IEntitySort`](../interfaces/IEntitySort.md)\<`T`\>[]
116
134
 
117
135
  The finalised sort keys.
136
+
137
+ ***
138
+
139
+ ### validateEntity()
140
+
141
+ > `static` **validateEntity**\<`T`\>(`entity`, `entitySchema`): `void`
142
+
143
+ Validate the entity against the schema.
144
+
145
+ #### Type Parameters
146
+
147
+ ##### T
148
+
149
+ `T`
150
+
151
+ #### Parameters
152
+
153
+ ##### entity
154
+
155
+ `T`
156
+
157
+ The entity to validate.
158
+
159
+ ##### entitySchema
160
+
161
+ [`IEntitySchema`](../interfaces/IEntitySchema.md)\<`T`\>
162
+
163
+ The schema to validate against.
164
+
165
+ #### Returns
166
+
167
+ `void`
168
+
169
+ #### Throws
170
+
171
+ If the entity is invalid.
@@ -4,33 +4,39 @@ Class to perform sort operations on entities.
4
4
 
5
5
  ## Constructors
6
6
 
7
- ### new EntitySorter()
7
+ ### Constructor
8
8
 
9
- > **new EntitySorter**(): [`EntitySorter`](EntitySorter.md)
9
+ > **new EntitySorter**(): `EntitySorter`
10
10
 
11
11
  #### Returns
12
12
 
13
- [`EntitySorter`](EntitySorter.md)
13
+ `EntitySorter`
14
14
 
15
15
  ## Methods
16
16
 
17
17
  ### sort()
18
18
 
19
- > `static` **sort**\<`T`\>(`entities`, `entitySorters`?): `T`[]
19
+ > `static` **sort**\<`T`\>(`entities`, `entitySorters?`): `T`[]
20
20
 
21
21
  Sort a list of entities using multiple keys and direction.
22
22
 
23
23
  #### Type Parameters
24
24
 
25
- **T**
25
+ ##### T
26
+
27
+ `T`
26
28
 
27
29
  #### Parameters
28
30
 
29
- **entities**: `T`[]
31
+ ##### entities
32
+
33
+ `T`[]
30
34
 
31
35
  The list of entities.
32
36
 
33
- **entitySorters?**: [`IEntitySort`](../interfaces/IEntitySort.md)\<`T`\>[]
37
+ ##### entitySorters?
38
+
39
+ [`IEntitySort`](../interfaces/IEntitySort.md)\<`T`\>[]
34
40
 
35
41
  The sort keys to use.
36
42
 
@@ -50,27 +56,39 @@ Compare two properties.
50
56
 
51
57
  #### Type Parameters
52
58
 
53
- **T**
59
+ ##### T
60
+
61
+ `T`
54
62
 
55
63
  #### Parameters
56
64
 
57
- **entity1**: `T`
65
+ ##### entity1
66
+
67
+ `T`
58
68
 
59
69
  The first entity.
60
70
 
61
- **entity2**: `T`
71
+ ##### entity2
72
+
73
+ `T`
62
74
 
63
75
  The second entity.
64
76
 
65
- **prop**: keyof `T`
77
+ ##### prop
78
+
79
+ keyof `T`
66
80
 
67
81
  The property to compare.
68
82
 
69
- **type**: [`EntitySchemaPropertyType`](../type-aliases/EntitySchemaPropertyType.md)
83
+ ##### type
84
+
85
+ [`EntitySchemaPropertyType`](../type-aliases/EntitySchemaPropertyType.md)
70
86
 
71
87
  The type of the property.
72
88
 
73
- **direction**: [`SortDirection`](../type-aliases/SortDirection.md) = `SortDirection.Ascending`
89
+ ##### direction
90
+
91
+ [`SortDirection`](../type-aliases/SortDirection.md) = `SortDirection.Ascending`
74
92
 
75
93
  The direction of the sort.
76
94
 
@@ -1,12 +1,14 @@
1
1
  # Function: entity()
2
2
 
3
- > **entity**(`options`?): `any`
3
+ > **entity**(`options?`): `any`
4
4
 
5
5
  Decorator to produce schema data for entity.
6
6
 
7
7
  ## Parameters
8
8
 
9
- **options?**: [`IEntitySchemaOptions`](../interfaces/IEntitySchemaOptions.md)
9
+ ### options?
10
+
11
+ [`IEntitySchemaOptions`](../interfaces/IEntitySchemaOptions.md)
10
12
 
11
13
  The options for the entity.
12
14
 
@@ -6,7 +6,9 @@ Decorator to produce schema property data for entities.
6
6
 
7
7
  ## Parameters
8
8
 
9
- **options**: `Omit`\<[`IEntitySchemaProperty`](../interfaces/IEntitySchemaProperty.md)\<`unknown`\>, `"property"`\>
9
+ ### options
10
+
11
+ `Omit`\<[`IEntitySchemaProperty`](../interfaces/IEntitySchemaProperty.md), `"property"`\>
10
12
 
11
13
  The options for the property.
12
14
 
@@ -4,7 +4,9 @@ Interface defining condition group operator.
4
4
 
5
5
  ## Type Parameters
6
6
 
7
- **T** = `unknown`
7
+ ### T
8
+
9
+ `T` = `unknown`
8
10
 
9
11
  ## Properties
10
12
 
@@ -4,7 +4,9 @@ Definition for an entity schema.
4
4
 
5
5
  ## Type Parameters
6
6
 
7
- **T** = `unknown`
7
+ ### T
8
+
9
+ `T` = `unknown`
8
10
 
9
11
  ## Properties
10
12
 
@@ -4,7 +4,9 @@ Definition for an entity schema property.
4
4
 
5
5
  ## Type Parameters
6
6
 
7
- **T** = `unknown`
7
+ ### T
8
+
9
+ `T` = `unknown`
8
10
 
9
11
  ## Properties
10
12
 
@@ -4,7 +4,9 @@ Definition of an entity property sort details.
4
4
 
5
5
  ## Type Parameters
6
6
 
7
- **T**
7
+ ### T
8
+
9
+ `T`
8
10
 
9
11
  ## Properties
10
12
 
@@ -1,5 +1,5 @@
1
1
  # Type Alias: ComparisonOperator
2
2
 
3
- > **ComparisonOperator**: *typeof* [`ComparisonOperator`](../variables/ComparisonOperator.md)\[keyof *typeof* [`ComparisonOperator`](../variables/ComparisonOperator.md)\]
3
+ > **ComparisonOperator** = *typeof* [`ComparisonOperator`](../variables/ComparisonOperator.md)\[keyof *typeof* [`ComparisonOperator`](../variables/ComparisonOperator.md)\]
4
4
 
5
5
  The types of comparisons.
@@ -1,9 +1,11 @@
1
1
  # Type Alias: EntityCondition\<T\>
2
2
 
3
- > **EntityCondition**\<`T`\>: [`IComparator`](../interfaces/IComparator.md) \| [`IComparatorGroup`](../interfaces/IComparatorGroup.md)\<`T`\>
3
+ > **EntityCondition**\<`T`\> = [`IComparator`](../interfaces/IComparator.md) \| [`IComparatorGroup`](../interfaces/IComparatorGroup.md)\<`T`\>
4
4
 
5
5
  Type defining condition for entities filtering.
6
6
 
7
7
  ## Type Parameters
8
8
 
9
- **T**
9
+ ### T
10
+
11
+ `T`
@@ -1,5 +1,5 @@
1
1
  # Type Alias: EntitySchemaPropertyFormat
2
2
 
3
- > **EntitySchemaPropertyFormat**: *typeof* [`EntitySchemaPropertyFormat`](../variables/EntitySchemaPropertyFormat.md)\[keyof *typeof* [`EntitySchemaPropertyFormat`](../variables/EntitySchemaPropertyFormat.md)\]
3
+ > **EntitySchemaPropertyFormat** = *typeof* [`EntitySchemaPropertyFormat`](../variables/EntitySchemaPropertyFormat.md)\[keyof *typeof* [`EntitySchemaPropertyFormat`](../variables/EntitySchemaPropertyFormat.md)\]
4
4
 
5
5
  Definition of the entity property type's format.
@@ -1,5 +1,5 @@
1
1
  # Type Alias: EntitySchemaPropertyType
2
2
 
3
- > **EntitySchemaPropertyType**: *typeof* [`EntitySchemaPropertyType`](../variables/EntitySchemaPropertyType.md)\[keyof *typeof* [`EntitySchemaPropertyType`](../variables/EntitySchemaPropertyType.md)\]
3
+ > **EntitySchemaPropertyType** = *typeof* [`EntitySchemaPropertyType`](../variables/EntitySchemaPropertyType.md)\[keyof *typeof* [`EntitySchemaPropertyType`](../variables/EntitySchemaPropertyType.md)\]
4
4
 
5
5
  Definition of the entity property types.
@@ -1,5 +1,5 @@
1
1
  # Type Alias: LogicalOperator
2
2
 
3
- > **LogicalOperator**: *typeof* [`LogicalOperator`](../variables/LogicalOperator.md)\[keyof *typeof* [`LogicalOperator`](../variables/LogicalOperator.md)\]
3
+ > **LogicalOperator** = *typeof* [`LogicalOperator`](../variables/LogicalOperator.md)\[keyof *typeof* [`LogicalOperator`](../variables/LogicalOperator.md)\]
4
4
 
5
5
  The logical operators for condition combining.
@@ -1,5 +1,5 @@
1
1
  # Type Alias: SortDirection
2
2
 
3
- > **SortDirection**: *typeof* [`SortDirection`](../variables/SortDirection.md)\[keyof *typeof* [`SortDirection`](../variables/SortDirection.md)\]
3
+ > **SortDirection** = *typeof* [`SortDirection`](../variables/SortDirection.md)\[keyof *typeof* [`SortDirection`](../variables/SortDirection.md)\]
4
4
 
5
5
  The sort directions.
package/locales/en.json CHANGED
@@ -2,7 +2,11 @@
2
2
  "error": {
3
3
  "entitySchemaHelper": {
4
4
  "noIsPrimary": "Property \"entitySchema.properties\" must contain a value with isPrimary set",
5
- "multipleIsPrimary": "Property \"entitySchema.properties\" contains more than one property with isPrimary set"
5
+ "multipleIsPrimary": "Property \"entitySchema.properties\" contains more than one property with isPrimary set",
6
+ "invalidEntityProperties": "The schema has no properties defined, but the entity has properties",
7
+ "invalidEntityProperty": "The entity value of \"{value}\" does not match the type \"{type}\" for property \"{property}\"",
8
+ "invalidOptional": "The entity property \"{property}\" of type \"{type}\" is not optional, but no value has been provided",
9
+ "invalidEntityKeys": "The entity had additional properties that are not in the schema, \"{keys}\""
6
10
  }
7
11
  }
8
12
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@twin.org/entity",
3
- "version": "0.0.1-next.7",
3
+ "version": "0.0.1-next.70",
4
4
  "description": "Helpers for defining and working with entities",
5
5
  "repository": {
6
6
  "type": "git",
@@ -14,8 +14,8 @@
14
14
  "node": ">=20.0.0"
15
15
  },
16
16
  "dependencies": {
17
- "@twin.org/core": "0.0.1-next.7",
18
- "@twin.org/nameof": "next",
17
+ "@twin.org/core": "0.0.1-next.70",
18
+ "@twin.org/nameof": "0.0.1-next.70",
19
19
  "reflect-metadata": "0.2.2"
20
20
  },
21
21
  "main": "./dist/cjs/index.cjs",
@@ -23,11 +23,11 @@
23
23
  "types": "./dist/types/index.d.ts",
24
24
  "exports": {
25
25
  ".": {
26
+ "types": "./dist/types/index.d.ts",
26
27
  "require": "./dist/cjs/index.cjs",
27
- "import": "./dist/esm/index.mjs",
28
- "types": "./dist/types/index.d.ts"
28
+ "import": "./dist/esm/index.mjs"
29
29
  },
30
- "./locales": "./locales"
30
+ "./locales/*.json": "./locales/*.json"
31
31
  },
32
32
  "files": [
33
33
  "dist/cjs",