Fix Model.prototype.inspect
Return the raw object data when running on Node v0.11.14+ That way all `inspect` options are always preserved. When running on older version: - Honour the depth argument passed to the custom `inspect` function. - Disable color output, becase there is now way how to detect whether colors were enabled or disabled by the top-level caller.
This commit is contained in:
parent
9b759c95ac
commit
e4fc38788f
24
lib/model.js
24
lib/model.js
|
@ -438,8 +438,28 @@ ModelBaseClass.prototype.reset = function () {
|
|||
}
|
||||
};
|
||||
|
||||
ModelBaseClass.prototype.inspect = function () {
|
||||
return util.inspect(this.__data, false, 4, true);
|
||||
// Node v0.11+ allows custom inspect functions to return an object
|
||||
// instead of string. That way options like `showHidden` and `colors`
|
||||
// can be preserved.
|
||||
var versionParts = process.versions.node
|
||||
.split(/\./g).map(function(v) { return +v; });
|
||||
|
||||
var INSPECT_SUPPORTS_OBJECT_RETVAL =
|
||||
versionParts[0] > 0 ||
|
||||
versionParts[1] > 11 ||
|
||||
(versionParts[0] === 11 && versionParts[1] >= 14);
|
||||
|
||||
ModelBaseClass.prototype.inspect = function (depth) {
|
||||
if (INSPECT_SUPPORTS_OBJECT_RETVAL)
|
||||
return this.__data;
|
||||
|
||||
// Workaround for older versions
|
||||
// See also https://github.com/joyent/node/commit/66280de133
|
||||
return util.inspect(this.__data, {
|
||||
showHidden: false,
|
||||
depth: depth,
|
||||
colors: false
|
||||
});
|
||||
};
|
||||
|
||||
ModelBaseClass.mixin = function (anotherClass, options) {
|
||||
|
|
|
@ -486,6 +486,14 @@ describe('validations', function () {
|
|||
.should.equal('`prop` is invalid (value: [ { a: [Object] } ]).');
|
||||
});
|
||||
|
||||
it('should exclude colors from Model values', function() {
|
||||
var obj = new User();
|
||||
obj.email = 'test@example.com';
|
||||
var err = givenValidationError('user', obj, 'is invalid');
|
||||
getErrorDetails(err).should.equal(
|
||||
'`user` is invalid (value: { email: \'test@example.com\' }).');
|
||||
});
|
||||
|
||||
function givenValidationError(propertyName, propertyValue, errorMessage) {
|
||||
var jsonVal = {};
|
||||
jsonVal[propertyName] = propertyValue;
|
||||
|
|
Loading…
Reference in New Issue