-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from dynamiccast/to-json-support
Support sails's model.toJSON()
- Loading branch information
Showing
5 changed files
with
110 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/** | ||
* JsonController | ||
* | ||
* @description :: Server-side logic for managing jsons | ||
* @help :: See http://sailsjs.org/#!/documentation/concepts/Controllers | ||
*/ | ||
|
||
module.exports = { | ||
|
||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* Json.js | ||
* | ||
* @description :: TODO: You might write a short summary of how this model works and what it represents here. | ||
* @docs :: http://sailsjs.org/documentation/concepts/models-and-orm/models | ||
*/ | ||
|
||
module.exports = { | ||
|
||
attributes: { | ||
variable: { | ||
type: 'string' | ||
}, | ||
invisible: { | ||
type: 'string' | ||
}, | ||
|
||
toJSON: function() { | ||
|
||
var obj = this.toObject(); | ||
delete obj.invisible; | ||
|
||
return obj; | ||
} | ||
}, | ||
|
||
autoCreatedAt: false, | ||
autoUpdatedAt: false | ||
}; |
40 changes: 40 additions & 0 deletions
40
tests/dummy/test/integration/controllers/JsonController.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
var request = require('supertest'); | ||
var JSONAPIValidator = require('jsonapi-validator').Validator; | ||
|
||
validateJSONapi = function(res) { | ||
var validator = new JSONAPIValidator(); | ||
|
||
validator.validate(res.body); | ||
}; | ||
|
||
describe('toJSON compatibility', function() { | ||
|
||
it('Should return record without the hidden field', function(done) { | ||
|
||
let toCreate = { | ||
'data': { | ||
'attributes': { | ||
variable: "test", | ||
invisible: "should not be displayed" | ||
}, | ||
'type': 'jsons' | ||
} | ||
}; | ||
|
||
request(sails.hooks.http.app) | ||
.post('/jsons') | ||
.send(toCreate) | ||
.expect(201) | ||
.expect(validateJSONapi) | ||
.expect({ | ||
data: { | ||
id: '1', | ||
type: 'jsons', | ||
attributes: { | ||
variable: 'test' | ||
} | ||
} | ||
}) | ||
.end(done); | ||
}); | ||
}); |