@cullet/erp-core 1.1.0 → 1.3.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 (196) hide show
  1. package/KIT_CONTEXT.md +1 -67
  2. package/dist/app-error.d.cts +108 -0
  3. package/dist/application/index.cjs +26 -0
  4. package/dist/application/index.d.cts +2 -0
  5. package/dist/application/index.d.ts +1 -1
  6. package/dist/application/index.js +2 -1
  7. package/dist/domain/index.cjs +16 -0
  8. package/dist/domain/index.d.cts +3 -0
  9. package/dist/domain/index.d.ts +3 -0
  10. package/dist/domain/index.js +3 -0
  11. package/dist/domain-event-contracts.cjs +33 -0
  12. package/dist/domain-event-contracts.cjs.map +1 -0
  13. package/dist/domain-event-contracts.d.cts +27 -0
  14. package/dist/domain-event-contracts.d.ts +27 -0
  15. package/dist/domain-event-contracts.js +22 -0
  16. package/dist/domain-event-contracts.js.map +1 -0
  17. package/dist/domain-exception.cjs +17 -0
  18. package/dist/domain-exception.cjs.map +1 -0
  19. package/dist/domain-exception.d.cts +7 -0
  20. package/dist/domain-exception.d.ts +7 -0
  21. package/dist/domain-exception.js +12 -0
  22. package/dist/domain-exception.js.map +1 -0
  23. package/dist/errors/index.cjs +41 -0
  24. package/dist/errors/index.d.cts +3 -0
  25. package/dist/errors/index.js +1 -1
  26. package/dist/exceptions/index.cjs +17 -0
  27. package/dist/exceptions/index.d.cts +5 -0
  28. package/dist/exceptions/index.d.ts +5 -0
  29. package/dist/exceptions/index.js +7 -0
  30. package/dist/exceptions/validation-field.cjs +3 -0
  31. package/dist/exceptions/validation-field.d.cts +2 -0
  32. package/dist/gate-engine-registry.cjs +308 -0
  33. package/dist/gate-engine-registry.cjs.map +1 -0
  34. package/dist/gate-engine-registry.d.cts +81 -0
  35. package/dist/gate-engine-registry.d.ts +2 -1
  36. package/dist/gate-engine-registry.js +2 -1
  37. package/dist/gate-engine-registry.js.map +1 -1
  38. package/dist/gate-types.d.cts +171 -0
  39. package/dist/gate-types.d.ts +3 -139
  40. package/dist/gate-v1-payload.schema.cjs +638 -0
  41. package/dist/gate-v1-payload.schema.cjs.map +1 -0
  42. package/dist/gate-v1-payload.schema.js +2 -116
  43. package/dist/gate-v1-payload.schema.js.map +1 -1
  44. package/dist/hashing.cjs +66 -0
  45. package/dist/hashing.cjs.map +1 -0
  46. package/dist/immutable.cjs +77 -0
  47. package/dist/immutable.cjs.map +1 -0
  48. package/dist/immutable.d.cts +6 -0
  49. package/dist/immutable.d.ts +6 -0
  50. package/dist/immutable.js +54 -0
  51. package/dist/immutable.js.map +1 -0
  52. package/dist/index.cjs +163 -0
  53. package/dist/index.cjs.map +1 -0
  54. package/dist/index.d.cts +30 -0
  55. package/dist/index.d.ts +15 -160
  56. package/dist/index.js +19 -176
  57. package/dist/index.js.map +1 -1
  58. package/dist/invalid-state-transition-exception.cjs +47 -0
  59. package/dist/invalid-state-transition-exception.cjs.map +1 -0
  60. package/dist/invalid-state-transition-exception.js +30 -0
  61. package/dist/invalid-state-transition-exception.js.map +1 -0
  62. package/dist/invariant-violation-exception.cjs +16 -0
  63. package/dist/invariant-violation-exception.cjs.map +1 -0
  64. package/dist/invariant-violation-exception.js +11 -0
  65. package/dist/invariant-violation-exception.js.map +1 -0
  66. package/dist/not-found-error.cjs +65 -0
  67. package/dist/not-found-error.cjs.map +1 -0
  68. package/dist/not-found-error.js +1 -1
  69. package/dist/outcome.cjs +97 -0
  70. package/dist/outcome.cjs.map +1 -0
  71. package/dist/outcome.d.cts +140 -0
  72. package/dist/outcome.d.ts +140 -0
  73. package/dist/outcome.js +1 -1
  74. package/dist/parse-gate-payload.d.cts +62 -0
  75. package/dist/parse-gate-payload.d.ts +2 -1
  76. package/dist/path.d.cts +90 -0
  77. package/dist/path.d.ts +2 -1
  78. package/dist/plugin.cjs +79 -0
  79. package/dist/plugin.cjs.map +1 -0
  80. package/dist/plugin.d.cts +85 -0
  81. package/dist/plugin.d.ts +85 -0
  82. package/dist/plugin.js +74 -0
  83. package/dist/plugin.js.map +1 -0
  84. package/dist/plugins/index.cjs +3 -0
  85. package/dist/plugins/index.d.cts +2 -0
  86. package/dist/plugins/index.d.ts +2 -0
  87. package/dist/plugins/index.js +2 -0
  88. package/dist/policies/engines/index.cjs +10 -0
  89. package/dist/policies/engines/index.d.cts +4 -0
  90. package/dist/policies/engines/index.d.ts +1 -1
  91. package/dist/policies/engines/v1/gate/index.cjs +91 -0
  92. package/dist/policies/engines/v1/gate/index.cjs.map +1 -0
  93. package/dist/policies/engines/v1/gate/index.d.cts +121 -0
  94. package/dist/policies/engines/v1/gate/index.d.ts +2 -1
  95. package/dist/policies/engines/v1/gate/index.js +2 -1
  96. package/dist/policies/engines/v1/gate/index.js.map +1 -1
  97. package/dist/policies/index.cjs +41 -0
  98. package/dist/policies/index.d.cts +7 -0
  99. package/dist/policies/index.d.ts +2 -1
  100. package/dist/policies/index.js +2 -1
  101. package/dist/policy-service.cjs +1190 -0
  102. package/dist/policy-service.cjs.map +1 -0
  103. package/dist/policy-service.d.cts +559 -0
  104. package/dist/policy-service.d.ts +2 -1
  105. package/dist/policy-service.js +6 -3
  106. package/dist/policy-service.js.map +1 -1
  107. package/dist/result/index.cjs +7 -0
  108. package/dist/result/index.d.cts +2 -0
  109. package/dist/result/index.d.ts +2 -0
  110. package/dist/result/index.js +3 -0
  111. package/dist/result.cjs +135 -0
  112. package/dist/result.cjs.map +1 -0
  113. package/dist/result.js +118 -0
  114. package/dist/result.js.map +1 -0
  115. package/dist/ruleset-registry.cjs +47 -0
  116. package/dist/ruleset-registry.cjs.map +1 -0
  117. package/dist/ruleset-registry.js +42 -0
  118. package/dist/ruleset-registry.js.map +1 -0
  119. package/dist/rulesets/index.cjs +3 -0
  120. package/dist/rulesets/index.d.cts +2 -0
  121. package/dist/rulesets/index.d.ts +2 -0
  122. package/dist/rulesets/index.js +2 -0
  123. package/dist/temporal-guards.cjs +32 -0
  124. package/dist/temporal-guards.cjs.map +1 -0
  125. package/dist/temporal-guards.js +2 -17
  126. package/dist/temporal-guards.js.map +1 -1
  127. package/dist/temporal-use-case.cjs +191 -0
  128. package/dist/temporal-use-case.cjs.map +1 -0
  129. package/dist/temporal-use-case.d.cts +304 -0
  130. package/dist/temporal-use-case.d.ts +4 -9
  131. package/dist/temporal-use-case.js +6 -140
  132. package/dist/temporal-use-case.js.map +1 -1
  133. package/dist/unexpected-error.cjs +208 -0
  134. package/dist/unexpected-error.cjs.map +1 -0
  135. package/dist/unexpected-error.js +179 -0
  136. package/dist/unexpected-error.js.map +1 -0
  137. package/dist/use-case.cjs +96 -0
  138. package/dist/use-case.cjs.map +1 -0
  139. package/dist/use-case.js +91 -0
  140. package/dist/use-case.js.map +1 -0
  141. package/dist/uuid-identifier.cjs +65 -0
  142. package/dist/uuid-identifier.cjs.map +1 -0
  143. package/dist/uuid-identifier.d.cts +230 -0
  144. package/dist/uuid-identifier.d.ts +230 -0
  145. package/dist/uuid-identifier.js +60 -0
  146. package/dist/uuid-identifier.js.map +1 -0
  147. package/dist/validation-code.cjs +60 -0
  148. package/dist/validation-code.cjs.map +1 -0
  149. package/dist/validation-code.d.cts +23 -0
  150. package/dist/validation-code.d.ts +23 -0
  151. package/dist/validation-code.js +1 -177
  152. package/dist/validation-code.js.map +1 -1
  153. package/dist/validation-error.cjs +1020 -0
  154. package/dist/validation-error.cjs.map +1 -0
  155. package/dist/validation-error.d.cts +777 -0
  156. package/dist/validation-error.d.ts +1 -21
  157. package/dist/validation-error.js +1 -1
  158. package/dist/validation-exception.cjs +41 -0
  159. package/dist/validation-exception.cjs.map +1 -0
  160. package/dist/validation-exception.d.cts +50 -0
  161. package/dist/validation-exception.d.ts +50 -0
  162. package/dist/validation-exception.js +8 -2
  163. package/dist/validation-exception.js.map +1 -1
  164. package/dist/validation-field.cjs +28 -0
  165. package/dist/validation-field.cjs.map +1 -0
  166. package/dist/validation-field.d.cts +12 -0
  167. package/dist/value-object-ruleset.contracts.d.cts +36 -0
  168. package/dist/value-object-ruleset.contracts.d.ts +36 -0
  169. package/dist/value-object.cjs +208 -0
  170. package/dist/value-object.cjs.map +1 -0
  171. package/dist/value-object.js +191 -0
  172. package/dist/value-object.js.map +1 -0
  173. package/dist/version.d.cts +10 -0
  174. package/dist/version.d.ts +10 -0
  175. package/dist/versioning/index.cjs +7 -0
  176. package/dist/versioning/index.d.cts +3 -0
  177. package/dist/versioning/index.d.ts +3 -0
  178. package/dist/versioning/index.js +3 -0
  179. package/meta.json +18 -3
  180. package/package.json +165 -17
  181. package/src/core/domain/rulesets/index.ts +7 -0
  182. package/src/core/domain/uuid-identifier.ts +72 -0
  183. package/src/core/domain/value-object.ts +37 -5
  184. package/src/core/exceptions/index.ts +13 -0
  185. package/src/core/index.ts +14 -2
  186. package/src/core/plugins/index.ts +7 -0
  187. package/src/core/plugins/plugin.ts +99 -0
  188. package/src/core/plugins/types.ts +53 -0
  189. package/src/core/versioning/index.ts +14 -0
  190. package/src/domain/index.ts +7 -0
  191. package/src/exceptions/index.ts +1 -0
  192. package/src/plugins/index.ts +1 -0
  193. package/src/result/index.ts +2 -0
  194. package/src/rulesets/index.ts +1 -0
  195. package/src/version.ts +1 -1
  196. package/src/versioning/index.ts +1 -0
@@ -0,0 +1,191 @@
1
+ import { t as InvariantViolationException } from "./invariant-violation-exception.js";
2
+ import { i as version, n as __decorate, t as makeImmutable } from "./immutable.js";
3
+ import { n as cloneDate, t as assertValidDate } from "./temporal-guards.js";
4
+ import { t as PluginManager } from "./plugin.js";
5
+ //#region src/core/shared/aggregate-version.ts
6
+ function assertValidAggregateVersion(aggregateVersion) {
7
+ if (!Number.isInteger(aggregateVersion) || aggregateVersion < 0) throw new InvariantViolationException(`aggregateVersion must be a non-negative integer. Received: ${aggregateVersion}`);
8
+ }
9
+ //#endregion
10
+ //#region src/core/domain/entity.ts
11
+ var _Entity;
12
+ /**
13
+ * Base class for domain entities — objects defined by a stable identity rather
14
+ * than by their attributes. Two entities are "the same" when their `id`
15
+ * matches, even if every other field differs; this is the opposite of a
16
+ * {@link ValueObject}, which is defined entirely by its contents.
17
+ *
18
+ * The class owns the bookkeeping common to every aggregate root: a creation
19
+ * timestamp that never changes, a last-modified timestamp, and an
20
+ * `aggregateVersion` counter used for optimistic concurrency control. All three
21
+ * are validated on construction and the dates are defensively cloned, so an
22
+ * entity can never be built into — or leak — an inconsistent temporal state.
23
+ *
24
+ * @typeParam TIdentifier - The identity type (e.g. a branded string id or a VO).
25
+ */
26
+ let Entity = class Entity {
27
+ static {
28
+ _Entity = this;
29
+ }
30
+ /**
31
+ * Reconstitutes an entity from its persisted {@link EntityState}.
32
+ *
33
+ * Validates the temporal invariants up front so an invalid entity is
34
+ * impossible to construct: both dates must be valid, the aggregate version
35
+ * must be a non-negative integer, and `updatedAt` may not predate
36
+ * `createdAt`. Both dates are cloned on the way in so a later mutation of
37
+ * the caller's `Date` objects cannot reach into the entity's internal state.
38
+ *
39
+ * Declared `protected` because entities are reconstituted through a
40
+ * subclass factory, never instantiated directly by application code.
41
+ *
42
+ * @throws {InvariantViolationException} When `updatedAt` is earlier than `createdAt`.
43
+ */
44
+ constructor(state) {
45
+ assertValidDate("createdAt", state.createdAt);
46
+ assertValidDate("updatedAt", state.updatedAt);
47
+ assertValidAggregateVersion(state.aggregateVersion);
48
+ if (state.updatedAt.getTime() < state.createdAt.getTime()) throw new InvariantViolationException("updatedAt cannot be earlier than createdAt");
49
+ this._id = state.id;
50
+ this._createdAt = cloneDate(state.createdAt);
51
+ this._updatedAt = cloneDate(state.updatedAt);
52
+ this._aggregateVersion = state.aggregateVersion;
53
+ }
54
+ /** The entity's stable identity — the basis for equality between entities. */
55
+ get id() {
56
+ return this._id;
57
+ }
58
+ /**
59
+ * When the entity was first created.
60
+ *
61
+ * Returns a clone so callers cannot mutate the entity's internal `Date`;
62
+ * the value is fixed at construction and never changes thereafter.
63
+ */
64
+ get createdAt() {
65
+ return cloneDate(this._createdAt);
66
+ }
67
+ /**
68
+ * When the entity was last modified.
69
+ *
70
+ * Returns a clone for the same encapsulation reason as {@link createdAt};
71
+ * the underlying value advances only through {@link markAsModified}.
72
+ */
73
+ get updatedAt() {
74
+ return cloneDate(this._updatedAt);
75
+ }
76
+ /**
77
+ * Monotonic counter incremented on every mutation, used for optimistic
78
+ * concurrency control: a writer reads this value, and the persistence layer
79
+ * rejects the write if the stored version has moved on in the meantime.
80
+ */
81
+ get aggregateVersion() {
82
+ return this._aggregateVersion;
83
+ }
84
+ /**
85
+ * The schema/contract version stamped on this entity type by the
86
+ * `@version` decorator — used to detect and migrate state persisted under
87
+ * an older shape.
88
+ */
89
+ get contractVersion() {
90
+ return _Entity.CONTRACT_VERSION;
91
+ }
92
+ /**
93
+ * The single seam through which an entity records a mutation: it advances
94
+ * `updatedAt` and bumps {@link aggregateVersion} by one. Subclasses must
95
+ * call this from every state-changing method so the version counter stays
96
+ * an accurate optimistic-lock token.
97
+ *
98
+ * @param updatedAt - The modification instant; defaults to now. Validated
99
+ * and required not to predate `createdAt`, preserving the same invariant
100
+ * the constructor enforces.
101
+ * @returns The new aggregate version after the increment.
102
+ * @throws {InvariantViolationException} When `updatedAt` is earlier than `createdAt`.
103
+ */
104
+ markAsModified(updatedAt = /* @__PURE__ */ new Date()) {
105
+ assertValidDate("updatedAt", updatedAt);
106
+ if (updatedAt.getTime() < this._createdAt.getTime()) throw new InvariantViolationException("updatedAt cannot be earlier than createdAt");
107
+ this._updatedAt = cloneDate(updatedAt);
108
+ this._aggregateVersion += 1;
109
+ return this._aggregateVersion;
110
+ }
111
+ };
112
+ Entity = _Entity = __decorate([version("1.0")], Entity);
113
+ //#endregion
114
+ //#region src/core/domain/value-object.ts
115
+ var _ValueObject;
116
+ /**
117
+ * Base class for value objects — domain concepts defined entirely by their
118
+ * contents, with no identity of their own. Two value objects are
119
+ * interchangeable when they hold equal data, which is the opposite of an
120
+ * {@link Entity} (compared by id). Money, a CPF, a date range: replacing one
121
+ * instance with an equal one changes nothing about the model.
122
+ *
123
+ * Immutability is the defining guarantee here. The wrapped `value` is
124
+ * deep-frozen on construction, so a value object can be shared freely without
125
+ * any risk of a consumer mutating shared state. Subclasses seal the instance
126
+ * itself with {@link finalize} once their own fields are set.
127
+ *
128
+ * @typeParam T - The shape of the wrapped data.
129
+ * @typeParam P - The primitive form produced by {@link toPrimitive} / {@link toJSON}.
130
+ */
131
+ let ValueObject = class ValueObject {
132
+ static {
133
+ _ValueObject = this;
134
+ }
135
+ static {
136
+ this.plugins = new PluginManager();
137
+ }
138
+ /**
139
+ * Wraps `value`, deep-freezing it so the value object is immutable from the
140
+ * moment it exists. Declared `protected` because value objects are built
141
+ * through a validating subclass factory, never instantiated directly.
142
+ */
143
+ constructor(value) {
144
+ this.value = makeImmutable(value);
145
+ }
146
+ /**
147
+ * The schema/contract version stamped on this value-object type by the
148
+ * `@version` decorator — used to detect state persisted under an older shape.
149
+ */
150
+ get contractVersion() {
151
+ return _ValueObject.CONTRACT_VERSION;
152
+ }
153
+ /**
154
+ * Freezes the instance shell, blocking reassignment of any own field.
155
+ *
156
+ * `value` is already deep-frozen by the constructor, so the wrapped data is
157
+ * immutable regardless. The instance itself is NOT frozen automatically
158
+ * because a subclass may still need to assign its own fields after
159
+ * `super(value)` runs. Call `finalize()` at the very end of the subclass
160
+ * constructor (after all fields are set) to make the whole value object
161
+ * immutable.
162
+ */
163
+ finalize() {
164
+ Object.freeze(this);
165
+ }
166
+ /**
167
+ * Hook for `JSON.stringify`. Delegates to {@link toPrimitive} so a value
168
+ * object serializes to its primitive form rather than exposing the internal
169
+ * `value` wrapper.
170
+ */
171
+ toJSON() {
172
+ return this.toPrimitive();
173
+ }
174
+ /**
175
+ * Compares this value object with another by content. Because value objects
176
+ * carry no identity, two independently constructed instances holding the
177
+ * same data are considered equal.
178
+ *
179
+ * Delegates to the registered equality {@link plugins}; with no plugin
180
+ * registered it falls back to comparing the serialized wrapped `value`.
181
+ * Subclasses may still override for a faster or domain-specific comparison.
182
+ */
183
+ equals(other) {
184
+ return _ValueObject.plugins.invoke("equals", [this, other], { fallback: (a, b) => JSON.stringify(a.value) === JSON.stringify(b.value) });
185
+ }
186
+ };
187
+ ValueObject = _ValueObject = __decorate([version("1.0")], ValueObject);
188
+ //#endregion
189
+ export { Entity as n, assertValidAggregateVersion as r, ValueObject as t };
190
+
191
+ //# sourceMappingURL=value-object.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"value-object.js","names":[],"sources":["../src/core/shared/aggregate-version.ts","../src/core/domain/entity.ts","../src/core/domain/value-object.ts"],"sourcesContent":["import { InvariantViolationException } from \"../exceptions/invariant-violation-exception.js\";\n\nfunction assertValidAggregateVersion(aggregateVersion: number): void {\n if (!Number.isInteger(aggregateVersion) || aggregateVersion < 0) {\n throw new InvariantViolationException(\n `aggregateVersion must be a non-negative integer. Received: ${aggregateVersion}`,\n );\n }\n}\n\nexport { assertValidAggregateVersion };\n","import { InvariantViolationException } from \"../exceptions/invariant-violation-exception.js\";\nimport { assertValidAggregateVersion } from \"../shared/aggregate-version.js\";\nimport { assertValidDate, cloneDate } from \"../shared/temporal-guards.js\";\nimport { type ContractVersion, version } from \"../versioning/version.js\";\n\n/**\n * The minimal persisted shape needed to reconstitute an {@link Entity}.\n *\n * This is the contract between storage and the domain: a repository maps a row\n * (or document) onto these four fields and hands them to a subclass constructor.\n * `aggregateVersion` travels with the state so optimistic-concurrency checks\n * survive a round-trip through the database.\n */\ninterface EntityState<TIdentifier> {\n readonly id: TIdentifier;\n readonly createdAt: Date;\n readonly updatedAt: Date;\n readonly aggregateVersion: number;\n}\n\n/**\n * Base class for domain entities — objects defined by a stable identity rather\n * than by their attributes. Two entities are \"the same\" when their `id`\n * matches, even if every other field differs; this is the opposite of a\n * {@link ValueObject}, which is defined entirely by its contents.\n *\n * The class owns the bookkeeping common to every aggregate root: a creation\n * timestamp that never changes, a last-modified timestamp, and an\n * `aggregateVersion` counter used for optimistic concurrency control. All three\n * are validated on construction and the dates are defensively cloned, so an\n * entity can never be built into — or leak — an inconsistent temporal state.\n *\n * @typeParam TIdentifier - The identity type (e.g. a branded string id or a VO).\n */\n@version(\"1.0\")\nabstract class Entity<TIdentifier> {\n declare public static readonly CONTRACT_VERSION: ContractVersion;\n\n private readonly _id: TIdentifier;\n private readonly _createdAt: Date;\n private _updatedAt: Date;\n private _aggregateVersion: number;\n\n /**\n * Reconstitutes an entity from its persisted {@link EntityState}.\n *\n * Validates the temporal invariants up front so an invalid entity is\n * impossible to construct: both dates must be valid, the aggregate version\n * must be a non-negative integer, and `updatedAt` may not predate\n * `createdAt`. Both dates are cloned on the way in so a later mutation of\n * the caller's `Date` objects cannot reach into the entity's internal state.\n *\n * Declared `protected` because entities are reconstituted through a\n * subclass factory, never instantiated directly by application code.\n *\n * @throws {InvariantViolationException} When `updatedAt` is earlier than `createdAt`.\n */\n protected constructor(state: EntityState<TIdentifier>) {\n assertValidDate(\"createdAt\", state.createdAt);\n assertValidDate(\"updatedAt\", state.updatedAt);\n assertValidAggregateVersion(state.aggregateVersion);\n\n if (state.updatedAt.getTime() < state.createdAt.getTime()) {\n throw new InvariantViolationException(\n \"updatedAt cannot be earlier than createdAt\",\n );\n }\n\n this._id = state.id;\n this._createdAt = cloneDate(state.createdAt);\n this._updatedAt = cloneDate(state.updatedAt);\n this._aggregateVersion = state.aggregateVersion;\n }\n\n /** The entity's stable identity — the basis for equality between entities. */\n public get id(): TIdentifier {\n return this._id;\n }\n\n /**\n * When the entity was first created.\n *\n * Returns a clone so callers cannot mutate the entity's internal `Date`;\n * the value is fixed at construction and never changes thereafter.\n */\n public get createdAt(): Date {\n return cloneDate(this._createdAt);\n }\n\n /**\n * When the entity was last modified.\n *\n * Returns a clone for the same encapsulation reason as {@link createdAt};\n * the underlying value advances only through {@link markAsModified}.\n */\n public get updatedAt(): Date {\n return cloneDate(this._updatedAt);\n }\n\n /**\n * Monotonic counter incremented on every mutation, used for optimistic\n * concurrency control: a writer reads this value, and the persistence layer\n * rejects the write if the stored version has moved on in the meantime.\n */\n public get aggregateVersion(): number {\n return this._aggregateVersion;\n }\n\n /**\n * The schema/contract version stamped on this entity type by the\n * `@version` decorator — used to detect and migrate state persisted under\n * an older shape.\n */\n public get contractVersion(): ContractVersion {\n return Entity.CONTRACT_VERSION;\n }\n\n /**\n * The single seam through which an entity records a mutation: it advances\n * `updatedAt` and bumps {@link aggregateVersion} by one. Subclasses must\n * call this from every state-changing method so the version counter stays\n * an accurate optimistic-lock token.\n *\n * @param updatedAt - The modification instant; defaults to now. Validated\n * and required not to predate `createdAt`, preserving the same invariant\n * the constructor enforces.\n * @returns The new aggregate version after the increment.\n * @throws {InvariantViolationException} When `updatedAt` is earlier than `createdAt`.\n */\n protected markAsModified(updatedAt: Date = new Date()): number {\n assertValidDate(\"updatedAt\", updatedAt);\n\n if (updatedAt.getTime() < this._createdAt.getTime()) {\n throw new InvariantViolationException(\n \"updatedAt cannot be earlier than createdAt\",\n );\n }\n\n this._updatedAt = cloneDate(updatedAt);\n this._aggregateVersion += 1;\n\n return this._aggregateVersion;\n }\n}\n\nexport { Entity, type EntityState };\n","import { PluginManager } from \"../plugins/index.js\";\nimport { type DeepReadonly, makeImmutable } from \"../shared/immutable.js\";\nimport { type ContractVersion, version } from \"../versioning/version.js\";\n\n/**\n * Extension point for value-object equality. A plugin implementing this\n * contract overrides how any two value objects are compared, letting the host\n * application swap in a structural comparator (e.g. `lodash.isEqual` over the\n * wrapped `value`) without every value object having to implement `equals` by\n * hand.\n */\ntype ValueObjectPluginContract = {\n equals: (\n a: ValueObject<unknown, unknown>,\n b: ValueObject<unknown, unknown>,\n ) => boolean;\n};\n\n/**\n * Base class for value objects — domain concepts defined entirely by their\n * contents, with no identity of their own. Two value objects are\n * interchangeable when they hold equal data, which is the opposite of an\n * {@link Entity} (compared by id). Money, a CPF, a date range: replacing one\n * instance with an equal one changes nothing about the model.\n *\n * Immutability is the defining guarantee here. The wrapped `value` is\n * deep-frozen on construction, so a value object can be shared freely without\n * any risk of a consumer mutating shared state. Subclasses seal the instance\n * itself with {@link finalize} once their own fields are set.\n *\n * @typeParam T - The shape of the wrapped data.\n * @typeParam P - The primitive form produced by {@link toPrimitive} / {@link toJSON}.\n */\n@version(\"1.0\")\nabstract class ValueObject<T, P> {\n declare public static readonly CONTRACT_VERSION: ContractVersion;\n\n /**\n * Registry of equality plugins shared by every value object. Empty by\n * default — when nothing is registered, {@link equals} falls back to a\n * structural comparison of the wrapped `value`. Hosts register a plugin\n * (e.g. `lodash.isEqual`) once at startup to customise equality globally.\n */\n public static readonly plugins =\n new PluginManager<ValueObjectPluginContract>();\n\n /** The wrapped data, deep-frozen so it can never be mutated after construction. */\n public readonly value: DeepReadonly<T>;\n\n /**\n * Wraps `value`, deep-freezing it so the value object is immutable from the\n * moment it exists. Declared `protected` because value objects are built\n * through a validating subclass factory, never instantiated directly.\n */\n protected constructor(value: T) {\n this.value = makeImmutable(value);\n }\n\n /**\n * The schema/contract version stamped on this value-object type by the\n * `@version` decorator — used to detect state persisted under an older shape.\n */\n public get contractVersion(): ContractVersion {\n return ValueObject.CONTRACT_VERSION;\n }\n\n /**\n * Freezes the instance shell, blocking reassignment of any own field.\n *\n * `value` is already deep-frozen by the constructor, so the wrapped data is\n * immutable regardless. The instance itself is NOT frozen automatically\n * because a subclass may still need to assign its own fields after\n * `super(value)` runs. Call `finalize()` at the very end of the subclass\n * constructor (after all fields are set) to make the whole value object\n * immutable.\n */\n protected finalize(): void {\n Object.freeze(this);\n }\n\n /**\n * Hook for `JSON.stringify`. Delegates to {@link toPrimitive} so a value\n * object serializes to its primitive form rather than exposing the internal\n * `value` wrapper.\n */\n public toJSON(): P {\n return this.toPrimitive();\n }\n\n /**\n * Compares this value object with another by content. Because value objects\n * carry no identity, two independently constructed instances holding the\n * same data are considered equal.\n *\n * Delegates to the registered equality {@link plugins}; with no plugin\n * registered it falls back to comparing the serialized wrapped `value`.\n * Subclasses may still override for a faster or domain-specific comparison.\n */\n public equals(other: this): boolean {\n return ValueObject.plugins.invoke(\"equals\", [this, other], {\n fallback: (a, b) =>\n JSON.stringify(a.value) === JSON.stringify(b.value),\n });\n }\n\n /**\n * Projects the value object down to a plain, serializable primitive form —\n * the representation suitable for persistence, transport, or comparison.\n */\n public abstract toPrimitive(): P;\n}\n\nexport { type DeepReadonly, ValueObject, type ValueObjectPluginContract };\n"],"mappings":";;;;;AAEA,SAAS,4BAA4B,kBAAgC;CACjE,IAAI,CAAC,OAAO,UAAU,gBAAgB,KAAK,mBAAmB,GAC1D,MAAM,IAAI,4BACN,8DAA8D,kBAClE;AAER;;;;;;;;;;;;;;;;;;AC0BA,IAAA,SAAA,MACe,OAAoB;;;;;;;;;;;;;;;;;;CAsB/B,YAAsB,OAAiC;EACnD,gBAAgB,aAAa,MAAM,SAAS;EAC5C,gBAAgB,aAAa,MAAM,SAAS;EAC5C,4BAA4B,MAAM,gBAAgB;EAElD,IAAI,MAAM,UAAU,QAAQ,IAAI,MAAM,UAAU,QAAQ,GACpD,MAAM,IAAI,4BACN,4CACJ;EAGJ,KAAK,MAAM,MAAM;EACjB,KAAK,aAAa,UAAU,MAAM,SAAS;EAC3C,KAAK,aAAa,UAAU,MAAM,SAAS;EAC3C,KAAK,oBAAoB,MAAM;CACnC;;CAGA,IAAW,KAAkB;EACzB,OAAO,KAAK;CAChB;;;;;;;CAQA,IAAW,YAAkB;EACzB,OAAO,UAAU,KAAK,UAAU;CACpC;;;;;;;CAQA,IAAW,YAAkB;EACzB,OAAO,UAAU,KAAK,UAAU;CACpC;;;;;;CAOA,IAAW,mBAA2B;EAClC,OAAO,KAAK;CAChB;;;;;;CAOA,IAAW,kBAAmC;EAC1C,OAAA,QAAc;CAClB;;;;;;;;;;;;;CAcA,eAAyB,4BAAkB,IAAI,KAAK,GAAW;EAC3D,gBAAgB,aAAa,SAAS;EAEtC,IAAI,UAAU,QAAQ,IAAI,KAAK,WAAW,QAAQ,GAC9C,MAAM,IAAI,4BACN,4CACJ;EAGJ,KAAK,aAAa,UAAU,SAAS;EACrC,KAAK,qBAAqB;EAE1B,OAAO,KAAK;CAChB;AACJ;+BA7GC,QAAQ,KAAK,CAAA,GAAA,MAAA;;;;;;;;;;;;;;;;;;;ACDd,IAAA,cAAA,MACe,YAAkB;;;;;iBAUzB,IAAI,cAAyC;;;;;;;CAUjD,YAAsB,OAAU;EAC5B,KAAK,QAAQ,cAAc,KAAK;CACpC;;;;;CAMA,IAAW,kBAAmC;EAC1C,OAAA,aAAmB;CACvB;;;;;;;;;;;CAYA,WAA2B;EACvB,OAAO,OAAO,IAAI;CACtB;;;;;;CAOA,SAAmB;EACf,OAAO,KAAK,YAAY;CAC5B;;;;;;;;;;CAWA,OAAc,OAAsB;EAChC,OAAA,aAAmB,QAAQ,OAAO,UAAU,CAAC,MAAM,KAAK,GAAG,EACvD,WAAW,GAAG,MACV,KAAK,UAAU,EAAE,KAAK,MAAM,KAAK,UAAU,EAAE,KAAK,EAC1D,CAAC;CACL;AAOJ;yCA7EC,QAAQ,KAAK,CAAA,GAAA,WAAA"}
@@ -0,0 +1,10 @@
1
+ //#region src/core/versioning/version.d.ts
2
+ type ContractVersion = `${number}.${number}`;
3
+ type VersionedTarget = {
4
+ readonly prototype: object;
5
+ };
6
+ declare const CONTRACT_VERSION_PROPERTY: "CONTRACT_VERSION";
7
+ declare function version<const TVersion extends ContractVersion>(contractVersion: TVersion): (target: VersionedTarget) => void;
8
+ //#endregion
9
+ export { version as i, ContractVersion as n, VersionedTarget as r, CONTRACT_VERSION_PROPERTY as t };
10
+ //# sourceMappingURL=version.d.cts.map
@@ -0,0 +1,10 @@
1
+ //#region src/core/versioning/version.d.ts
2
+ type ContractVersion = `${number}.${number}`;
3
+ type VersionedTarget = {
4
+ readonly prototype: object;
5
+ };
6
+ declare const CONTRACT_VERSION_PROPERTY: "CONTRACT_VERSION";
7
+ declare function version<const TVersion extends ContractVersion>(contractVersion: TVersion): (target: VersionedTarget) => void;
8
+ //#endregion
9
+ export { version as i, ContractVersion as n, VersionedTarget as r, CONTRACT_VERSION_PROPERTY as t };
10
+ //# sourceMappingURL=version.d.ts.map
@@ -0,0 +1,7 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ const require_immutable = require("../immutable.cjs");
3
+ const require_domain_event_contracts = require("../domain-event-contracts.cjs");
4
+ exports.CONTRACT_VERSION_PROPERTY = require_immutable.CONTRACT_VERSION_PROPERTY;
5
+ exports.buildDomainEventContractVersions = require_domain_event_contracts.buildDomainEventContractVersions;
6
+ exports.createDomainEventEnvelope = require_domain_event_contracts.createDomainEventEnvelope;
7
+ exports.version = require_immutable.version;
@@ -0,0 +1,3 @@
1
+ import { i as version, n as ContractVersion, r as VersionedTarget, t as CONTRACT_VERSION_PROPERTY } from "../version.cjs";
2
+ import { a as buildDomainEventContractVersions, i as DomainEventEnvelope, n as DomainEventContractSelection, o as createDomainEventEnvelope, r as DomainEventContractVersions, t as CreateDomainEventEnvelopeInput } from "../domain-event-contracts.cjs";
3
+ export { CONTRACT_VERSION_PROPERTY, ContractVersion, CreateDomainEventEnvelopeInput, DomainEventContractSelection, DomainEventContractVersions, DomainEventEnvelope, VersionedTarget, buildDomainEventContractVersions, createDomainEventEnvelope, version };
@@ -0,0 +1,3 @@
1
+ import { i as version, n as ContractVersion, r as VersionedTarget, t as CONTRACT_VERSION_PROPERTY } from "../version.js";
2
+ import { a as buildDomainEventContractVersions, i as DomainEventEnvelope, n as DomainEventContractSelection, o as createDomainEventEnvelope, r as DomainEventContractVersions, t as CreateDomainEventEnvelopeInput } from "../domain-event-contracts.js";
3
+ export { CONTRACT_VERSION_PROPERTY, ContractVersion, CreateDomainEventEnvelopeInput, DomainEventContractSelection, DomainEventContractVersions, DomainEventEnvelope, VersionedTarget, buildDomainEventContractVersions, createDomainEventEnvelope, version };
@@ -0,0 +1,3 @@
1
+ import { i as version, r as CONTRACT_VERSION_PROPERTY } from "../immutable.js";
2
+ import { n as createDomainEventEnvelope, t as buildDomainEventContractVersions } from "../domain-event-contracts.js";
3
+ export { CONTRACT_VERSION_PROPERTY, buildDomainEventContractVersions, createDomainEventEnvelope, version };
package/meta.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "schemaVersion": "2",
3
3
  "name": "erp-core",
4
- "version": "1.1.0",
4
+ "version": "1.3.0",
5
5
  "description": "Núcleo arquitetural para ERP em TypeScript — entidades, value objects, erros tipados e policies declaráveis sobre clean architecture.",
6
6
  "compatibility": {
7
7
  "engines": {
@@ -12,7 +12,9 @@
12
12
  "peerDependencies": [
13
13
  {
14
14
  "name": "zod",
15
- "range": ">=3.22.0 <5"
15
+ "range": ">=3.22.0 <5",
16
+ "optional": true,
17
+ "notes": "Only required by the `./policies` subpath (gate/compute engines). The domain/result/errors/exceptions/rulesets/versioning/plugins subpaths are zod-free."
16
18
  }
17
19
  ]
18
20
  },
@@ -51,6 +53,17 @@
51
53
  "exports": [
52
54
  "Entity",
53
55
  "ValueObject",
56
+ "UuidIdentifier",
57
+ "PluginManager",
58
+ "Result",
59
+ "Outcome",
60
+ "RulesetRegistry",
61
+ "DomainException",
62
+ "InvariantViolationException",
63
+ "ValidationException",
64
+ "ValidationField",
65
+ "version",
66
+ "createDomainEventEnvelope",
54
67
  "AppError",
55
68
  "ValidationError",
56
69
  "NotFoundError",
@@ -76,7 +89,9 @@
76
89
  "1.0.9 - Harden erp-core invariants and error serialization",
77
90
  "1.0.10 - Stop the `meta.json` `changelog` from drifting and clarify the `ValueObject` freeze contract.",
78
91
  "1.0.11 - Document the public API surface with TSDoc.",
79
- "1.1.0 - **New: `ResultRepository`, `UseCaseObservability`, and `./application` subpath**"
92
+ "1.1.0 - **New: `ResultRepository`, `UseCaseObservability`, and `./application` subpath**",
93
+ "1.2.0 - Add zod-free subpaths for the stable core primitives and a value-object equality plugin system.",
94
+ "1.3.0 - Add zod-free subpaths for the stable core primitives and a value-object equality plugin system."
80
95
  ],
81
96
  "deprecated": false
82
97
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cullet/erp-core",
3
- "version": "1.1.0",
3
+ "version": "1.3.0",
4
4
  "description": "Núcleo arquitetural para ERP em TypeScript — entidades, value objects, erros tipados e policies declaráveis sobre clean architecture.",
5
5
  "type": "module",
6
6
  "repository": {
@@ -8,36 +8,179 @@
8
8
  "url": "git+https://github.com/fabiano-eduardo/cullet.git",
9
9
  "directory": "packages/erp-core"
10
10
  },
11
- "main": "./dist/index.js",
11
+ "main": "./dist/index.cjs",
12
+ "module": "./dist/index.js",
12
13
  "types": "./dist/index.d.ts",
14
+ "typesVersions": {
15
+ "*": {
16
+ "application": [
17
+ "./dist/application/index.d.ts"
18
+ ],
19
+ "domain": [
20
+ "./dist/domain/index.d.ts"
21
+ ],
22
+ "errors": [
23
+ "./dist/errors/index.d.ts"
24
+ ],
25
+ "exceptions": [
26
+ "./dist/exceptions/index.d.ts"
27
+ ],
28
+ "exceptions/validation-field": [
29
+ "./dist/exceptions/validation-field.d.ts"
30
+ ],
31
+ "plugins": [
32
+ "./dist/plugins/index.d.ts"
33
+ ],
34
+ "policies": [
35
+ "./dist/policies/index.d.ts"
36
+ ],
37
+ "policies/engines": [
38
+ "./dist/policies/engines/index.d.ts"
39
+ ],
40
+ "policies/engines/v1/gate": [
41
+ "./dist/policies/engines/v1/gate/index.d.ts"
42
+ ],
43
+ "result": [
44
+ "./dist/result/index.d.ts"
45
+ ],
46
+ "rulesets": [
47
+ "./dist/rulesets/index.d.ts"
48
+ ],
49
+ "versioning": [
50
+ "./dist/versioning/index.d.ts"
51
+ ]
52
+ }
53
+ },
13
54
  "exports": {
14
55
  ".": {
15
- "types": "./dist/index.d.ts",
16
- "import": "./dist/index.js"
56
+ "import": {
57
+ "types": "./dist/index.d.ts",
58
+ "default": "./dist/index.js"
59
+ },
60
+ "require": {
61
+ "types": "./dist/index.d.cts",
62
+ "default": "./dist/index.cjs"
63
+ }
17
64
  },
18
65
  "./application": {
19
- "types": "./dist/application/index.d.ts",
20
- "import": "./dist/application/index.js"
66
+ "import": {
67
+ "types": "./dist/application/index.d.ts",
68
+ "default": "./dist/application/index.js"
69
+ },
70
+ "require": {
71
+ "types": "./dist/application/index.d.cts",
72
+ "default": "./dist/application/index.cjs"
73
+ }
74
+ },
75
+ "./domain": {
76
+ "import": {
77
+ "types": "./dist/domain/index.d.ts",
78
+ "default": "./dist/domain/index.js"
79
+ },
80
+ "require": {
81
+ "types": "./dist/domain/index.d.cts",
82
+ "default": "./dist/domain/index.cjs"
83
+ }
21
84
  },
22
85
  "./errors": {
23
- "types": "./dist/errors/index.d.ts",
24
- "import": "./dist/errors/index.js"
86
+ "import": {
87
+ "types": "./dist/errors/index.d.ts",
88
+ "default": "./dist/errors/index.js"
89
+ },
90
+ "require": {
91
+ "types": "./dist/errors/index.d.cts",
92
+ "default": "./dist/errors/index.cjs"
93
+ }
94
+ },
95
+ "./exceptions": {
96
+ "import": {
97
+ "types": "./dist/exceptions/index.d.ts",
98
+ "default": "./dist/exceptions/index.js"
99
+ },
100
+ "require": {
101
+ "types": "./dist/exceptions/index.d.cts",
102
+ "default": "./dist/exceptions/index.cjs"
103
+ }
104
+ },
105
+ "./exceptions/validation-field": {
106
+ "import": {
107
+ "types": "./dist/exceptions/validation-field.d.ts",
108
+ "default": "./dist/exceptions/validation-field.js"
109
+ },
110
+ "require": {
111
+ "types": "./dist/exceptions/validation-field.d.cts",
112
+ "default": "./dist/exceptions/validation-field.cjs"
113
+ }
114
+ },
115
+ "./plugins": {
116
+ "import": {
117
+ "types": "./dist/plugins/index.d.ts",
118
+ "default": "./dist/plugins/index.js"
119
+ },
120
+ "require": {
121
+ "types": "./dist/plugins/index.d.cts",
122
+ "default": "./dist/plugins/index.cjs"
123
+ }
25
124
  },
26
125
  "./policies": {
27
- "types": "./dist/policies/index.d.ts",
28
- "import": "./dist/policies/index.js"
126
+ "import": {
127
+ "types": "./dist/policies/index.d.ts",
128
+ "default": "./dist/policies/index.js"
129
+ },
130
+ "require": {
131
+ "types": "./dist/policies/index.d.cts",
132
+ "default": "./dist/policies/index.cjs"
133
+ }
29
134
  },
30
135
  "./policies/engines": {
31
- "types": "./dist/policies/engines/index.d.ts",
32
- "import": "./dist/policies/engines/index.js"
136
+ "import": {
137
+ "types": "./dist/policies/engines/index.d.ts",
138
+ "default": "./dist/policies/engines/index.js"
139
+ },
140
+ "require": {
141
+ "types": "./dist/policies/engines/index.d.cts",
142
+ "default": "./dist/policies/engines/index.cjs"
143
+ }
33
144
  },
34
145
  "./policies/engines/v1/gate": {
35
- "types": "./dist/policies/engines/v1/gate/index.d.ts",
36
- "import": "./dist/policies/engines/v1/gate/index.js"
146
+ "import": {
147
+ "types": "./dist/policies/engines/v1/gate/index.d.ts",
148
+ "default": "./dist/policies/engines/v1/gate/index.js"
149
+ },
150
+ "require": {
151
+ "types": "./dist/policies/engines/v1/gate/index.d.cts",
152
+ "default": "./dist/policies/engines/v1/gate/index.cjs"
153
+ }
37
154
  },
38
- "./exceptions/validation-field": {
39
- "types": "./dist/exceptions/validation-field.d.ts",
40
- "import": "./dist/exceptions/validation-field.js"
155
+ "./result": {
156
+ "import": {
157
+ "types": "./dist/result/index.d.ts",
158
+ "default": "./dist/result/index.js"
159
+ },
160
+ "require": {
161
+ "types": "./dist/result/index.d.cts",
162
+ "default": "./dist/result/index.cjs"
163
+ }
164
+ },
165
+ "./rulesets": {
166
+ "import": {
167
+ "types": "./dist/rulesets/index.d.ts",
168
+ "default": "./dist/rulesets/index.js"
169
+ },
170
+ "require": {
171
+ "types": "./dist/rulesets/index.d.cts",
172
+ "default": "./dist/rulesets/index.cjs"
173
+ }
174
+ },
175
+ "./versioning": {
176
+ "import": {
177
+ "types": "./dist/versioning/index.d.ts",
178
+ "default": "./dist/versioning/index.js"
179
+ },
180
+ "require": {
181
+ "types": "./dist/versioning/index.d.cts",
182
+ "default": "./dist/versioning/index.cjs"
183
+ }
41
184
  },
42
185
  "./package.json": "./package.json"
43
186
  },
@@ -62,6 +205,11 @@
62
205
  "peerDependencies": {
63
206
  "zod": ">=3.22.0 <5"
64
207
  },
208
+ "peerDependenciesMeta": {
209
+ "zod": {
210
+ "optional": true
211
+ }
212
+ },
65
213
  "publishConfig": {
66
214
  "access": "public"
67
215
  },
@@ -0,0 +1,7 @@
1
+ export {
2
+ type CreationRuleset,
3
+ type InvariantRuleset,
4
+ } from "./entity-ruleset.contracts.js";
5
+ export { RulesetRegistry } from "./ruleset-registry.js";
6
+ export { type Ruleset, type RulesetId } from "./ruleset.contracts.js";
7
+ export { type ValueObjectRuleset } from "./value-object-ruleset.contracts.js";
@@ -0,0 +1,72 @@
1
+ import { ValueObject } from "./value-object.js";
2
+
3
+ /**
4
+ * Canonical RFC-4122 UUID (versions 1–5), matched case-insensitively. Declared
5
+ * once here so the dozens of typed identifiers a domain accumulates never have
6
+ * to re-state the pattern.
7
+ */
8
+ const UUID_PATTERN =
9
+ /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
10
+
11
+ /**
12
+ * Base class for UUID-backed identity value objects.
13
+ *
14
+ * A domain typically has many of these — `OrderId`, `CustomerId`, `InvoiceId` —
15
+ * all wrapping the same `string` shape. Two problems follow: the UUID format
16
+ * check gets copy-pasted into every one, and because the wrapped shape is
17
+ * identical, TypeScript's structural typing would happily accept an `OrderId`
18
+ * where a `CustomerId` is expected. `UuidIdentifier` solves both: the format
19
+ * lives here once ({@link isValid}), and the `TBrand` phantom tag makes each
20
+ * subtype nominally distinct so the ids cannot be swapped for one another.
21
+ *
22
+ * It deliberately does not impose a factory. Identifiers vary in how they
23
+ * report an invalid value (their own exception type, their own message), so a
24
+ * concrete id adds a validating `create` that calls {@link isValid} plus a
25
+ * `reconstitute` that trusts an already-persisted value:
26
+ *
27
+ * ```ts
28
+ * class OrderId extends UuidIdentifier<"OrderId"> {
29
+ * private constructor(value: string) { super(value); }
30
+ * static create(raw: string): OrderId {
31
+ * if (!UuidIdentifier.isValid(raw)) throw new InvalidOrderIdError(raw);
32
+ * return new OrderId(raw);
33
+ * }
34
+ * static reconstitute(value: string): OrderId { return new OrderId(value); }
35
+ * }
36
+ * ```
37
+ *
38
+ * @typeParam TBrand - A unique string literal that nominally tags the subtype.
39
+ */
40
+ abstract class UuidIdentifier<
41
+ TBrand extends string = string,
42
+ > extends ValueObject<string, string> {
43
+ /**
44
+ * Phantom brand. Never assigned at runtime (`declare`), it exists only so
45
+ * two identifiers with different brands are not interchangeable at the type
46
+ * level despite sharing the same `string` value.
47
+ */
48
+ protected declare readonly __brand: TBrand;
49
+
50
+ protected constructor(value: string) {
51
+ super(value);
52
+ }
53
+
54
+ public toPrimitive(): string {
55
+ return this.value;
56
+ }
57
+
58
+ /** The wrapped UUID string — convenient for logging and interpolation. */
59
+ public toString(): string {
60
+ return this.value;
61
+ }
62
+
63
+ /**
64
+ * Whether `candidate` is a canonical UUID (versions 1–5). The single source
65
+ * of truth for the format; concrete identifiers call this from `create`.
66
+ */
67
+ public static isValid(candidate: string): boolean {
68
+ return UUID_PATTERN.test(candidate);
69
+ }
70
+ }
71
+
72
+ export { UuidIdentifier };