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

Commit 43a7811

Browse files
committed
minItems and maxItems have no effect for arrays of objects, fix #18 (thanks @WeweTom)
1 parent f19d76a commit 43a7811

File tree

5 files changed

+65
-12
lines changed

5 files changed

+65
-12
lines changed

.npmignore

-2
This file was deleted.

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.3
15+
16+
- **Bug Fix**
17+
- `minItems` and `maxItems` have no effect for `array`s of objects, fix #18 (thanks @WeweTom)
18+
1419
## v0.2.2
1520

1621
- **Bug Fix**

index.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,14 @@ var types = {
9090
},
9191

9292
array: function (s) {
93+
var type = t.Array;
9394
if (s.hasOwnProperty('items')) {
9495
var items = s.items;
9596
if (t.Object.is(items)) {
96-
return t.list(transform(s.items));
97+
type = t.list(transform(s.items));
98+
} else {
99+
return t.tuple(items.map(transform));
97100
}
98-
return t.tuple(items.map(transform));
99101
}
100102
var predicate;
101103
if (s.hasOwnProperty('minItems')) {
@@ -104,7 +106,7 @@ var types = {
104106
if (s.hasOwnProperty('maxItems')) {
105107
predicate = and(predicate, fcomb.maxLength(s.maxItems));
106108
}
107-
return predicate ? t.subtype(t.Array, predicate) : t.Array;
109+
return predicate ? t.subtype(type, predicate) : type;
108110
},
109111

110112
'null': function () {

package.json

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
{
22
"name": "tcomb-json-schema",
3-
"version": "0.2.2",
3+
"version": "0.2.3",
44
"description": "Transforms a JSON Schema to a tcomb type",
55
"main": "index.js",
6+
"files": [
7+
"index.js",
8+
"util.js"
9+
],
610
"scripts": {
711
"test": "mocha"
812
},
@@ -16,11 +20,9 @@
1620
"url": "https://github.com/gcanti/tcomb-json-schema/issues"
1721
},
1822
"homepage": "https://github.com/gcanti/tcomb-json-schema",
19-
"peerDependencies": {
20-
"tcomb": "^2.2.0"
21-
},
2223
"dependencies": {
23-
"fcomb": "^0.1.0"
24+
"fcomb": "^0.1.0",
25+
"tcomb": "^2.2.0"
2426
},
2527
"devDependencies": {
2628
"eslint": "^1.1.0"

test/test.js

+48-2
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ describe('transform', function () {
286286
eq(Type.meta.predicate(['a', 'b', 'c']), false);
287287
});
288288

289-
it('should handle items as list', function () {
289+
it('should handle list items', function () {
290290
var Type = transform({
291291
type: 'array',
292292
items: {
@@ -297,7 +297,53 @@ describe('transform', function () {
297297
ok(Type.meta.type === Num);
298298
});
299299

300-
it('should handle items as tuple', function () {
300+
it('should handle minItems with list items', function () {
301+
var Type = transform({
302+
"type": "array",
303+
"minItems": 2,
304+
"items": {
305+
"type": "object",
306+
"properties": {
307+
"name": {
308+
"type": "string"
309+
}
310+
},
311+
"required": ["name"]
312+
}
313+
})
314+
eq(getKind(Type), 'subtype');
315+
eq(getKind(Type.meta.type), 'list');
316+
eq(getKind(Type.meta.type.meta.type), 'struct');
317+
eq(Type.meta.predicate([]), false);
318+
eq(Type.meta.predicate([{name: 'name 1'}]), false);
319+
eq(Type.meta.predicate([{name: 'name 1'}, {name: 'name 2'}]), true);
320+
eq(Type.meta.predicate([{name: 'name 1'}, {name: 'name 2'}, {name: 'name 3'}]), true);
321+
})
322+
323+
it('should handle maxItems with list items', function () {
324+
var Type = transform({
325+
"type": "array",
326+
"maxItems": 2,
327+
"items": {
328+
"type": "object",
329+
"properties": {
330+
"name": {
331+
"type": "string"
332+
}
333+
},
334+
"required": ["name"]
335+
}
336+
})
337+
eq(getKind(Type), 'subtype');
338+
eq(getKind(Type.meta.type), 'list');
339+
eq(getKind(Type.meta.type.meta.type), 'struct');
340+
eq(Type.meta.predicate([]), true);
341+
eq(Type.meta.predicate([{name: 'name 1'}]), true);
342+
eq(Type.meta.predicate([{name: 'name 1'}, {name: 'name 2'}]), true);
343+
eq(Type.meta.predicate([{name: 'name 1'}, {name: 'name 2'}, {name: 'name 3'}]), false);
344+
})
345+
346+
it('should handle tuple items', function () {
301347
var Type = transform({
302348
type: 'array',
303349
items: [

0 commit comments

Comments
 (0)