This repository was archived by the owner on Feb 16, 2021. It is now read-only.
File tree 3 files changed +90
-0
lines changed
3 files changed +90
-0
lines changed Original file line number Diff line number Diff line change @@ -41,3 +41,29 @@ Removes all registered formats.
41
41
``` js
42
42
transform .resetFormats ();
43
43
```
44
+
45
+ ## registerType(typeName: string, type: tComb Supported types): void
46
+
47
+ Registers a new type.
48
+
49
+ ** Example**
50
+
51
+ ``` js
52
+ var Str10 = t .subtype (t .Str , function (s ) {
53
+ return s .length <= 10 ;
54
+ }, ' Str10' );
55
+
56
+ transform .registerType (' string10' , Str10);
57
+
58
+ var TcombType = transform ({
59
+ type: " string10"
60
+ });
61
+ ```
62
+
63
+ ## resetTypes(): void
64
+
65
+ Removes all registered types.
66
+
67
+ ``` js
68
+ transform .resetTypes ();
69
+ ```
Original file line number Diff line number Diff line change @@ -113,6 +113,8 @@ var types = {
113
113
114
114
} ;
115
115
116
+ var registerTypes = { } ;
117
+
116
118
function transform ( s ) {
117
119
t . assert ( t . Object . is ( s ) ) ;
118
120
if ( ! s . hasOwnProperty ( 'type' ) ) {
@@ -127,6 +129,11 @@ function transform(s) {
127
129
return types [ type ] ( s ) ;
128
130
} ) ) ;
129
131
}
132
+
133
+ if ( registerTypes . hasOwnProperty ( type ) ) {
134
+ return registerTypes [ type ] ;
135
+ }
136
+
130
137
t . fail ( '[tcomb-json-schema] Unsupported json schema ' + t . stringify ( s ) ) ;
131
138
}
132
139
@@ -141,4 +148,14 @@ transform.resetFormats = function resetFormats() {
141
148
formats = { } ;
142
149
} ;
143
150
151
+ transform . registerType = function registerType ( typeName , type ) {
152
+ t . assert ( ! registerTypes . hasOwnProperty ( typeName ) , '[tcomb-json-schema] Duplicated type ' + typeName ) ;
153
+ t . assert ( ! SchemaType . is ( typeName ) , '[tcomb-json-schema] Reserved type ' + typeName ) ;
154
+ registerTypes [ typeName ] = type ;
155
+ } ;
156
+
157
+ transform . resetTypes = function resetTypes ( ) {
158
+ registerTypes = { } ;
159
+ } ;
160
+
144
161
module . exports = transform ;
Original file line number Diff line number Diff line change @@ -368,6 +368,53 @@ describe('transform', function () {
368
368
ko ( Type . is ( '' ) )
369
369
} ) ;
370
370
371
+ } ) ;
372
+
373
+ describe ( 'registerType' , function ( ) {
374
+
375
+ var Str10 = t . subtype ( t . Str , function ( s ) {
376
+ return s . length <= 10 ;
377
+ } , 'Str10' ) ;
378
+
379
+ transform . registerType ( 'string10' , Str10 ) ;
380
+
381
+ it ( 'should throw if duplicated types are registered' , function ( ) {
382
+ assert . throws (
383
+ function ( ) {
384
+ transform . registerType ( 'string10' , Str10 ) ;
385
+ } ,
386
+ function ( err ) {
387
+ if ( ( err instanceof Error ) && err . message === '[tcomb] [tcomb-json-schema] Duplicated type string10' ) {
388
+ return true ;
389
+ }
390
+ }
391
+ ) ;
392
+ } ) ;
393
+
394
+ it ( 'should throw if a reserved type is register' , function ( ) {
395
+ assert . throws (
396
+ function ( ) {
397
+ transform . registerType ( 'string' , Str10 ) ;
398
+ } ,
399
+ function ( err ) {
400
+ if ( ( err instanceof Error ) && err . message === '[tcomb] [tcomb-json-schema] Reserved type string' ) {
401
+ return true ;
402
+ }
403
+ }
404
+ ) ;
405
+ } ) ;
406
+
407
+
408
+ it ( 'should handle type property' , function ( ) {
409
+ var Type = transform ( {
410
+ type : "string10"
411
+ } ) ;
412
+ eq ( getKind ( Type ) , 'subtype' ) ;
413
+ ok ( Type . meta . type === Str ) ;
414
+ ok ( Type . is ( 'abcdefghij' ) ) ;
415
+ ko ( Type . is ( 'abcdefghijk' ) )
416
+ } ) ;
417
+
371
418
} ) ;
372
419
373
420
} ) ;
You can’t perform that action at this time.
0 commit comments