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

Commit

Permalink
minItems and maxItems have no effect for arrays of objects, fix #…
Browse files Browse the repository at this point in the history
…18 (thanks @WeweTom)
  • Loading branch information
gcanti committed Dec 20, 2015
1 parent f19d76a commit 43a7811
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 12 deletions.
2 changes: 0 additions & 2 deletions .npmignore

This file was deleted.

5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
**Note**: Gaps between patch versions are faulty/broken releases.

## v0.2.3

- **Bug Fix**
- `minItems` and `maxItems` have no effect for `array`s of objects, fix #18 (thanks @WeweTom)

## v0.2.2

- **Bug Fix**
Expand Down
8 changes: 5 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,14 @@ var types = {
},

array: function (s) {
var type = t.Array;
if (s.hasOwnProperty('items')) {
var items = s.items;
if (t.Object.is(items)) {
return t.list(transform(s.items));
type = t.list(transform(s.items));
} else {
return t.tuple(items.map(transform));
}
return t.tuple(items.map(transform));
}
var predicate;
if (s.hasOwnProperty('minItems')) {
Expand All @@ -104,7 +106,7 @@ var types = {
if (s.hasOwnProperty('maxItems')) {
predicate = and(predicate, fcomb.maxLength(s.maxItems));
}
return predicate ? t.subtype(t.Array, predicate) : t.Array;
return predicate ? t.subtype(type, predicate) : type;
},

'null': function () {
Expand Down
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
{
"name": "tcomb-json-schema",
"version": "0.2.2",
"version": "0.2.3",
"description": "Transforms a JSON Schema to a tcomb type",
"main": "index.js",
"files": [
"index.js",
"util.js"
],
"scripts": {
"test": "mocha"
},
Expand All @@ -16,11 +20,9 @@
"url": "https://github.com/gcanti/tcomb-json-schema/issues"
},
"homepage": "https://github.com/gcanti/tcomb-json-schema",
"peerDependencies": {
"tcomb": "^2.2.0"
},
"dependencies": {
"fcomb": "^0.1.0"
"fcomb": "^0.1.0",
"tcomb": "^2.2.0"
},
"devDependencies": {
"eslint": "^1.1.0"
Expand Down
50 changes: 48 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ describe('transform', function () {
eq(Type.meta.predicate(['a', 'b', 'c']), false);
});

it('should handle items as list', function () {
it('should handle list items', function () {
var Type = transform({
type: 'array',
items: {
Expand All @@ -297,7 +297,53 @@ describe('transform', function () {
ok(Type.meta.type === Num);
});

it('should handle items as tuple', function () {
it('should handle minItems with list items', function () {
var Type = transform({
"type": "array",
"minItems": 2,
"items": {
"type": "object",
"properties": {
"name": {
"type": "string"
}
},
"required": ["name"]
}
})
eq(getKind(Type), 'subtype');
eq(getKind(Type.meta.type), 'list');
eq(getKind(Type.meta.type.meta.type), 'struct');
eq(Type.meta.predicate([]), false);
eq(Type.meta.predicate([{name: 'name 1'}]), false);
eq(Type.meta.predicate([{name: 'name 1'}, {name: 'name 2'}]), true);
eq(Type.meta.predicate([{name: 'name 1'}, {name: 'name 2'}, {name: 'name 3'}]), true);
})

it('should handle maxItems with list items', function () {
var Type = transform({
"type": "array",
"maxItems": 2,
"items": {
"type": "object",
"properties": {
"name": {
"type": "string"
}
},
"required": ["name"]
}
})
eq(getKind(Type), 'subtype');
eq(getKind(Type.meta.type), 'list');
eq(getKind(Type.meta.type.meta.type), 'struct');
eq(Type.meta.predicate([]), true);
eq(Type.meta.predicate([{name: 'name 1'}]), true);
eq(Type.meta.predicate([{name: 'name 1'}, {name: 'name 2'}]), true);
eq(Type.meta.predicate([{name: 'name 1'}, {name: 'name 2'}, {name: 'name 3'}]), false);
})

it('should handle tuple items', function () {
var Type = transform({
type: 'array',
items: [
Expand Down

0 comments on commit 43a7811

Please sign in to comment.