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

Commit 2fa4ee4

Browse files
committed
Merge pull request #10 from gcanti/v0.2
upgrade to tcomb v2.2
2 parents c854202 + fd507dc commit 2fa4ee4

File tree

5 files changed

+40
-27
lines changed

5 files changed

+40
-27
lines changed

.eslintrc

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"env": {
3+
"browser": true,
4+
"node": true
5+
},
6+
"ecmaFeatures": {
7+
"modules": true
8+
},
9+
"rules": {
10+
"new-cap": 0,
11+
"no-shadow": 0,
12+
"quotes": [2, "single"],
13+
"comma-dangle": "always"
14+
}
15+
}

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
1212
**Note**: Gaps between patch versions are faulty/broken releases.
1313

14+
## v0.2.0
15+
16+
- **Breaking Change**
17+
+ upgrade to tcomb v2.2
18+
1419
## v0.1.4
1520

1621
- **Internal**

index.js

+15-23
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,7 @@ var t = require('tcomb');
44
var fcomb = require('fcomb');
55
var util = require('./util');
66

7-
var Str = t.Str;
8-
var Num = t.Num;
9-
var Bool = t.Bool;
10-
var Obj = t.Obj;
11-
var Arr = t.Arr;
12-
var subtype = t.subtype;
13-
var enums = t.enums;
14-
15-
var SchemaType = enums.of('null string number integer boolean object array', 'SchemaType');
7+
var SchemaType = t.enums.of('null string number integer boolean object array', 'SchemaType');
168

179
function and(f, g) {
1810
return f ? fcomb.and(f, g) : g;
@@ -22,7 +14,7 @@ var types = {
2214

2315
string: function (s) {
2416
if (s.hasOwnProperty('enum')) {
25-
return enums.of(s['enum']);
17+
return t.enums.of(s['enum']);
2618
}
2719
var predicate;
2820
if (s.hasOwnProperty('minLength')) {
@@ -35,10 +27,10 @@ var types = {
3527
predicate = and(predicate, fcomb.regexp(new RegExp(s.pattern)));
3628
}
3729
if (s.hasOwnProperty('format')) {
38-
t.assert(formats.hasOwnProperty(s.format), 'missing format %s, use the `registerFormat` API', s.format);
30+
t.assert(formats.hasOwnProperty(s.format), '[tcomb-json-schema] Missing format ' + s.format + ', use the (format, predicate) API');
3931
predicate = and(predicate, formats[s.format]);
4032
}
41-
return predicate ? subtype(Str, predicate) : Str;
33+
return predicate ? t.subtype(t.String, predicate) : t.String;
4234
},
4335

4436
number: function (s) {
@@ -56,7 +48,7 @@ var types = {
5648
if (s.hasOwnProperty('integer') && s.integer) {
5749
predicate = and(predicate, util.isInteger);
5850
}
59-
return predicate ? subtype(Num, predicate) : Num;
51+
return predicate ? t.subtype(t.Number, predicate) : t.Number;
6052
},
6153

6254
integer: function (s) {
@@ -71,11 +63,11 @@ var types = {
7163
and(predicate, fcomb.lt(s.maximum)) :
7264
and(predicate, fcomb.lte(s.maximum));
7365
}
74-
return predicate ? subtype(util.Int, predicate) : util.Int;
66+
return predicate ? t.subtype(util.Int, predicate) : util.Int;
7567
},
7668

7769
boolean: function (s) {
78-
return Bool;
70+
return t.Boolean;
7971
},
8072

8173
object: function (s) {
@@ -91,16 +83,16 @@ var types = {
9183
if (s.properties.hasOwnProperty(k)) {
9284
hasProperties = true;
9385
var type = transform(s.properties[k]);
94-
props[k] = required[k] || type === Bool ? type : t.maybe(type);
86+
props[k] = required[k] || type === t.Boolean ? type : t.maybe(type);
9587
}
9688
}
97-
return hasProperties ? t.struct(props, s.description) : Obj;
89+
return hasProperties ? t.struct(props, s.description) : t.Object;
9890
},
9991

10092
array: function (s) {
10193
if (s.hasOwnProperty('items')) {
10294
var items = s.items;
103-
if (Obj.is(items)) {
95+
if (t.Object.is(items)) {
10496
return t.list(transform(s.items));
10597
}
10698
return t.tuple(items.map(transform));
@@ -112,7 +104,7 @@ var types = {
112104
if (s.hasOwnProperty('maxItems')) {
113105
predicate = and(predicate, fcomb.maxLength(s.maxItems));
114106
}
115-
return predicate ? subtype(Arr, predicate) : Arr;
107+
return predicate ? t.subtype(t.Array, predicate) : t.Array;
116108
},
117109

118110
null: function () {
@@ -122,26 +114,26 @@ var types = {
122114
};
123115

124116
function transform(s) {
125-
t.assert(Obj.is(s));
117+
t.assert(t.Object.is(s));
126118
if (!s.hasOwnProperty('type')) {
127119
return t.Any;
128120
}
129121
var type = s.type;
130122
if (SchemaType.is(type)) {
131123
return types[type](s);
132124
}
133-
if (Arr.is(type)) {
125+
if (t.Array.is(type)) {
134126
return t.union(type.map(function (type) {
135127
return types[type](s);
136128
}));
137129
}
138-
t.fail(t.format('unsupported json schema %j', s));
130+
t.fail('[tcomb-json-schema] Unsupported json schema ' + t.stringify(s));
139131
}
140132

141133
var formats = {};
142134

143135
transform.registerFormat = function registerFormat(format, predicate) {
144-
t.assert(!formats.hasOwnProperty(format), '[tcomb-json-schema] duplicated format %s', format);
136+
t.assert(!formats.hasOwnProperty(format), '[tcomb-json-schema] Duplicated format ' + format);
145137
formats[format] = predicate;
146138
};
147139

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tcomb-json-schema",
3-
"version": "0.1.4",
3+
"version": "0.2.0",
44
"description": "Transforms a JSON Schema to a tcomb type",
55
"main": "index.js",
66
"scripts": {
@@ -17,12 +17,13 @@
1717
},
1818
"homepage": "https://github.com/gcanti/tcomb-json-schema",
1919
"peerDependencies": {
20-
"tcomb": "^1.0.0"
20+
"tcomb": "^2.2.0"
2121
},
2222
"dependencies": {
2323
"fcomb": "^0.1.0"
2424
},
2525
"devDependencies": {
26+
"eslint": "^1.1.0"
2627
},
2728
"tags": [
2829
"tcomb",

test/test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ describe('transform', function () {
333333
transform.registerFormat('email', isEmail);
334334
},
335335
function(err) {
336-
if ( (err instanceof Error) && err.message === '[tcomb-json-schema] duplicated format email') {
336+
if ( (err instanceof Error) && err.message === '[tcomb] [tcomb-json-schema] Duplicated format email') {
337337
return true;
338338
}
339339
}
@@ -349,7 +349,7 @@ describe('transform', function () {
349349
});
350350
},
351351
function(err) {
352-
if ( (err instanceof Error) && err.message === 'missing format unknown, use the `registerFormat` API') {
352+
if ( (err instanceof Error) && err.message === '[tcomb] [tcomb-json-schema] Missing format unknown, use the (format, predicate) API') {
353353
return true;
354354
}
355355
}

0 commit comments

Comments
 (0)