Skip to content

Commit

Permalink
Add flag to exclude instance properties from enumeration
Browse files Browse the repository at this point in the history
  • Loading branch information
flacnut committed May 9, 2016
1 parent 4d3ad7d commit c80565f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/Instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ function Instance(Model, opts) {
opts.changes.push(key);
}
},
enumerable: true
enumerable: !(prop && !prop.enumerable)
});
};
var addInstanceExtraProperty = function (key) {
Expand Down
7 changes: 6 additions & 1 deletion lib/Property.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,17 @@ exports.normalize = function (opts) {

if (KNOWN_TYPES.indexOf(opts.prop.type) === -1 && !(opts.prop.type in opts.customTypes)) {
throw new ORMError("Unknown property type: " + opts.prop.type, 'NO_SUPPORT');
}
}

if (!opts.prop.hasOwnProperty("required") && opts.settings.get("properties.required")) {
opts.prop.required = true;
}

// Defaults to true. Setting to false hides properties from JSON.stringify(modelInstance).
if (!opts.prop.hasOwnProperty("enumerable") || opts.prop.enumerable === true) {
opts.prop.enumerable = true;
}

// Defaults to true. Rational means floating point here.
if (opts.prop.type == "number" && opts.prop.rational === undefined) {
opts.prop.rational = true;
Expand Down
19 changes: 18 additions & 1 deletion test/integration/instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ describe("Model instance", function() {
name : String,
age : { type: 'integer', required: false },
height : { type: 'integer', required: false },
weight : { type: 'number', required: false },
weight : { type: 'number', required: false, enumerable: true },
secret : { type: 'text', required: false, enumerable: false },
data : { type: 'object', required: false }
}, {
identityCache: false,
Expand Down Expand Up @@ -445,5 +446,21 @@ describe("Model instance", function() {
});
}
});

describe("Enumerable", function () {
it("should not stringify properties marked as not enumerable", function (done) {
Person.create({ name: 'Dilbert', secret: 'dogbert', weight: 100, data: {data: 3} }, function (err, p) {
if (err) return done(err);

var result = JSON.parse(JSON.stringify(p));
should.not.exist(result.secret);
should.exist(result.weight);
should.exist(result.data);
should.exist(result.name);

done();
});
});
});
});
});

0 comments on commit c80565f

Please sign in to comment.