Skip to content

Commit 33c39f9

Browse files
committed
Merge pull request #101 from holidayextras/filterFalsyValues
Filtering falsy boolean values
2 parents 1e562c7 + c82c240 commit 33c39f9

File tree

6 files changed

+54
-3
lines changed

6 files changed

+54
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
- 2016-02-16 - v1.3.3
2+
- 2016-02-16 - Filtering falsy boolean values
13
- 2016-02-16 - v1.3.2
24
- 2016-02-16 - Correctly filter by non-string attributes
35
- 2016-02-10 - v1.3.1

example/resources/photos.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ jsonApi.define({
2020
width: jsonApi.Joi.number().min(1).max(10000).precision(0)
2121
.description("The photos width in pixels")
2222
.example(512),
23+
raw: jsonApi.Joi.boolean()
24+
.default(false)
25+
.description("File in RAW format")
26+
.example(false),
2327
photographer: jsonApi.Joi.one("people")
2428
.description("The person who took the photo"),
2529
articles: jsonApi.Joi.belongsToMany({
@@ -35,6 +39,7 @@ jsonApi.define({
3539
url: "http://www.example.com/foobar",
3640
height: 1080,
3741
width: 1920,
42+
raw: true,
3843
photographer: { type: "people", id: "ad3aa89e-9c5b-4ac9-a652-6670f9f27587" }
3944
},
4045
{

lib/postProcessing/filter.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,12 @@ filter._filterKeepObject = function(someObject, filters, attributesConfig) {
9494
var attributeConfig = attributesConfig[filterName];
9595

9696
if (someObject.attributes.hasOwnProperty(filterName) || (filterName === "id")) {
97-
var attributeValue = someObject.attributes[filterName] || "";
97+
var attributeValue = someObject.attributes[filterName];
9898
if (filterName === "id") attributeValue = someObject.id;
9999
var attributeMatches = filter._attributesMatchesOR(attributeValue, attributeConfig, whitelist);
100100
if (!attributeMatches) return false;
101101
} else if (someObject.relationships.hasOwnProperty(filterName)) {
102-
var relationships = someObject.relationships[filterName] || "";
102+
var relationships = someObject.relationships[filterName];
103103
var relationshipMatches = filter._relationshipMatchesOR(relationships, whitelist);
104104
if (!relationshipMatches) return false;
105105
} else {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jsonapi-server",
3-
"version": "1.3.2",
3+
"version": "1.3.3",
44
"description": "A config driven NodeJS framework implementing json:api",
55
"keywords": [
66
"jsonapi",

test/get-resource.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,46 @@ describe("Testing jsonapi-server", function() {
137137
});
138138
});
139139

140+
describe("equality for booleans", function() {
141+
142+
it("matches false", function(done) {
143+
var url = "http://localhost:16006/rest/photos?filter[raw]=false";
144+
helpers.request({
145+
method: "GET",
146+
url: url
147+
}, function(err, res, json) {
148+
assert.equal(err, null);
149+
json = helpers.validateJson(json);
150+
151+
assert.equal(res.statusCode, "200", "Expecting 200 OK");
152+
var photoTypes = json.data.map(function(i) { return i.attributes.raw; });
153+
assert.deepEqual(photoTypes, [ false, false ], "expected matching resources");
154+
155+
done();
156+
});
157+
});
158+
159+
it("matches true", function(done) {
160+
var url = "http://localhost:16006/rest/photos?filter[raw]=true";
161+
helpers.request({
162+
method: "GET",
163+
url: url
164+
}, function(err, res, json) {
165+
assert.equal(err, null);
166+
json = helpers.validateJson(json);
167+
168+
assert.equal(res.statusCode, "200", "Expecting 200 OK");
169+
var photoTypes = json.data.map(function(i) { return i.attributes.raw; });
170+
assert.deepEqual(photoTypes, [ true ], "expected matching resources");
171+
172+
done();
173+
});
174+
});
175+
176+
177+
});
178+
179+
140180
it("less than for strings", function(done) {
141181
var url = "http://localhost:16006/rest/articles?filter[title]=<M";
142182
helpers.request({

test/swaggerValidator.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ swaggerValidator._validateOther = function(model, payload, urlPath, validationPa
119119
if (typeof payload !== "number") {
120120
throw new Error("Swagger Validation: " + urlPath + " Expected number at " + validationPath + ", got " + typeof payload);
121121
}
122+
} else if (model.type === "boolean") {
123+
if (typeof payload !== "boolean") {
124+
throw new Error("Swagger Validation: " + urlPath + " Expected boolean at " + validationPath + ", got " + typeof payload);
125+
}
122126
} else {
123127
throw new Error("Swagger Validation: " + urlPath + " Unknown type " + model.type + " at " + validationPath);
124128
}

0 commit comments

Comments
 (0)