Skip to content
This repository was archived by the owner on Feb 16, 2021. It is now read-only.

Commit c2edfb8

Browse files
committed
Merge pull request #14 from damienleroux/master
registerType & resetTypes new functions
2 parents 2fa4ee4 + db3df9a commit c2edfb8

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

README.md

+26
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,29 @@ Removes all registered formats.
4141
```js
4242
transform.resetFormats();
4343
```
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+
```

index.js

+17
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ var types = {
113113

114114
};
115115

116+
var registerTypes = {};
117+
116118
function transform(s) {
117119
t.assert(t.Object.is(s));
118120
if (!s.hasOwnProperty('type')) {
@@ -127,6 +129,11 @@ function transform(s) {
127129
return types[type](s);
128130
}));
129131
}
132+
133+
if (registerTypes.hasOwnProperty(type)) {
134+
return registerTypes[type];
135+
}
136+
130137
t.fail('[tcomb-json-schema] Unsupported json schema ' + t.stringify(s));
131138
}
132139

@@ -141,4 +148,14 @@ transform.resetFormats = function resetFormats() {
141148
formats = {};
142149
};
143150

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+
144161
module.exports = transform;

test/test.js

+47
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,53 @@ describe('transform', function () {
368368
ko(Type.is(''))
369369
});
370370

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+
371418
});
372419

373420
});

0 commit comments

Comments
 (0)