Skip to content

Commit

Permalink
Merge pull request #101 from holidayextras/filterFalsyValues
Browse files Browse the repository at this point in the history
Filtering falsy boolean values
  • Loading branch information
DuncanFenning committed Feb 24, 2016
2 parents 1e562c7 + c82c240 commit 33c39f9
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
- 2016-02-16 - v1.3.3
- 2016-02-16 - Filtering falsy boolean values
- 2016-02-16 - v1.3.2
- 2016-02-16 - Correctly filter by non-string attributes
- 2016-02-10 - v1.3.1
Expand Down
5 changes: 5 additions & 0 deletions example/resources/photos.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ jsonApi.define({
width: jsonApi.Joi.number().min(1).max(10000).precision(0)
.description("The photos width in pixels")
.example(512),
raw: jsonApi.Joi.boolean()
.default(false)
.description("File in RAW format")
.example(false),
photographer: jsonApi.Joi.one("people")
.description("The person who took the photo"),
articles: jsonApi.Joi.belongsToMany({
Expand All @@ -35,6 +39,7 @@ jsonApi.define({
url: "http://www.example.com/foobar",
height: 1080,
width: 1920,
raw: true,
photographer: { type: "people", id: "ad3aa89e-9c5b-4ac9-a652-6670f9f27587" }
},
{
Expand Down
4 changes: 2 additions & 2 deletions lib/postProcessing/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,12 @@ filter._filterKeepObject = function(someObject, filters, attributesConfig) {
var attributeConfig = attributesConfig[filterName];

if (someObject.attributes.hasOwnProperty(filterName) || (filterName === "id")) {
var attributeValue = someObject.attributes[filterName] || "";
var attributeValue = someObject.attributes[filterName];
if (filterName === "id") attributeValue = someObject.id;
var attributeMatches = filter._attributesMatchesOR(attributeValue, attributeConfig, whitelist);
if (!attributeMatches) return false;
} else if (someObject.relationships.hasOwnProperty(filterName)) {
var relationships = someObject.relationships[filterName] || "";
var relationships = someObject.relationships[filterName];
var relationshipMatches = filter._relationshipMatchesOR(relationships, whitelist);
if (!relationshipMatches) return false;
} else {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jsonapi-server",
"version": "1.3.2",
"version": "1.3.3",
"description": "A config driven NodeJS framework implementing json:api",
"keywords": [
"jsonapi",
Expand Down
40 changes: 40 additions & 0 deletions test/get-resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,46 @@ describe("Testing jsonapi-server", function() {
});
});

describe("equality for booleans", function() {

it("matches false", function(done) {
var url = "http://localhost:16006/rest/photos?filter[raw]=false";
helpers.request({
method: "GET",
url: url
}, function(err, res, json) {
assert.equal(err, null);
json = helpers.validateJson(json);

assert.equal(res.statusCode, "200", "Expecting 200 OK");
var photoTypes = json.data.map(function(i) { return i.attributes.raw; });
assert.deepEqual(photoTypes, [ false, false ], "expected matching resources");

done();
});
});

it("matches true", function(done) {
var url = "http://localhost:16006/rest/photos?filter[raw]=true";
helpers.request({
method: "GET",
url: url
}, function(err, res, json) {
assert.equal(err, null);
json = helpers.validateJson(json);

assert.equal(res.statusCode, "200", "Expecting 200 OK");
var photoTypes = json.data.map(function(i) { return i.attributes.raw; });
assert.deepEqual(photoTypes, [ true ], "expected matching resources");

done();
});
});


});


it("less than for strings", function(done) {
var url = "http://localhost:16006/rest/articles?filter[title]=<M";
helpers.request({
Expand Down
4 changes: 4 additions & 0 deletions test/swaggerValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ swaggerValidator._validateOther = function(model, payload, urlPath, validationPa
if (typeof payload !== "number") {
throw new Error("Swagger Validation: " + urlPath + " Expected number at " + validationPath + ", got " + typeof payload);
}
} else if (model.type === "boolean") {
if (typeof payload !== "boolean") {
throw new Error("Swagger Validation: " + urlPath + " Expected boolean at " + validationPath + ", got " + typeof payload);
}
} else {
throw new Error("Swagger Validation: " + urlPath + " Unknown type " + model.type + " at " + validationPath);
}
Expand Down

0 comments on commit 33c39f9

Please sign in to comment.