File tree Expand file tree Collapse file tree 3 files changed +13
-18
lines changed
Expand file tree Collapse file tree 3 files changed +13
-18
lines changed Original file line number Diff line number Diff line change @@ -41,8 +41,8 @@ Creating value objects:
4141``` ts
4242import { Uuid } from ' @code-net/identity' ;
4343
44- class UserId extends Uuid . for ( ' User' ) {}
45- class ProductId extends Uuid . for ( ' Product' ) {}
44+ class UserId extends Uuid < ' User' > {}
45+ class ProductId extends Uuid < ' Product' > {}
4646
4747let userId = new UserId ();
4848let productId = new ProductId ();
Original file line number Diff line number Diff line change @@ -15,7 +15,7 @@ export class Uuid<T = unknown> {
1515 constructor ( private readonly id : string = uuid . v4 ( ) ) {
1616 if ( ! uuid . isValid ( id ) ) {
1717 throw new InvalidValue (
18- `${ this . __brand } ID is not valid UUID, given: "${ id } "`
18+ `${ this . constructor . name } is not valid UUID, given: "${ id } "`
1919 ) ;
2020 }
2121 }
@@ -43,17 +43,4 @@ export class Uuid<T = unknown> {
4343 valueOf ( ) {
4444 return this . id ;
4545 }
46-
47- static for < T > ( ) : new ( id ?: string ) => Uuid < T > {
48- return class UuidFor extends Uuid < T > {
49- constructor ( id : string = uuid . v4 ( ) ) {
50- super ( id ) ;
51- if ( ! uuid . isValid ( id ) ) {
52- throw new InvalidValue (
53- `${ this . __brand } is not valid UUID, given: "${ id } "`
54- ) ;
55- }
56- }
57- } ;
58- }
5946}
Original file line number Diff line number Diff line change 11import { Uuid } from '../src' ;
22
33it ( 'should fail typechecking' , ( ) => {
4- class UserId extends Uuid . for < 'User' > ( ) { }
5- class ProductId extends Uuid . for < 'Product' > ( ) { }
4+ class UserId extends Uuid < 'User' > { }
5+ class ProductId extends Uuid < 'Product' > { }
66
77 let userId = new UserId ( ) ;
88 const productId = new ProductId ( ) ;
@@ -11,3 +11,11 @@ it('should fail typechecking', () => {
1111 userId = productId ;
1212 expect ( userId ) . toBeDefined ( ) ;
1313} ) ;
14+
15+ it ( 'will throw InvalidValue for invalid UUID' , ( ) => {
16+ class MyId extends Uuid < 'My' > { }
17+
18+ expect ( ( ) => {
19+ new MyId ( 'invalid-uuid' ) ;
20+ } ) . toThrow ( `MyId is not valid UUID, given: "invalid-uuid"` ) ;
21+ } ) ;
You can’t perform that action at this time.
0 commit comments