Enhance fieldsToArray to consider strict mode

This commit is contained in:
Raymond Feng 2015-05-19 22:32:03 -07:00
parent 9705819972
commit 966bb56c31
4 changed files with 74 additions and 43 deletions

View File

@ -947,7 +947,7 @@ DataAccessObject._normalize = function (filter) {
// normalize fields as array of included property names
if (filter.fields) {
filter.fields = fieldsToArray(filter.fields,
Object.keys(this.definition.properties));
Object.keys(this.definition.properties), this.settings.strict);
}
filter = removeUndefined(filter);

View File

@ -181,8 +181,9 @@ ModelBaseClass.prototype._initProperties = function (data, options) {
} else if (ctor.relations[p]) {
var relationType = ctor.relations[p].type;
var modelTo;
if (!properties[p]) {
var modelTo = ctor.relations[p].modelTo || ModelBaseClass;
modelTo = ctor.relations[p].modelTo || ModelBaseClass;
var multiple = ctor.relations[p].multiple;
var typeName = multiple ? 'Array' : modelTo.modelName;
var propType = multiple ? [modelTo] : modelTo;
@ -196,7 +197,8 @@ ModelBaseClass.prototype._initProperties = function (data, options) {
self.__data[ctor.relations[p].keyFrom] = propVal[ctor.relations[p].keyTo];
if (ctor.relations[p].options.embedsProperties) {
var fields = fieldsToArray(ctor.relations[p].properties, modelTo.definition.properties);
var fields = fieldsToArray(ctor.relations[p].properties,
modelTo.definition.properties, modelTo.setting.strict);
if (!~fields.indexOf(ctor.relations[p].keyTo)) {
fields.push(ctor.relations[p].keyTo);
}

View File

@ -213,46 +213,61 @@ function mergeQuery(base, update, spec) {
return base;
}
function fieldsToArray(fields, properties) {
/**
* Normalize fields to an array of included properties
* @param {String|String[]|Object} fields Fields filter
* @param {String[]} properties Property names
* @param {Boolean} excludeUnknown To exclude fields that are unknown properties
* @returns {String[]} An array of included property names
*/
function fieldsToArray(fields, properties, excludeUnknown) {
if (!fields) return;
// include all properties by default
var result = properties;
var i, n;
if (typeof fields === 'string') {
return [fields];
}
if (Array.isArray(fields) && fields.length > 0) {
result = [fields];
} else if (Array.isArray(fields) && fields.length > 0) {
// No empty array, including all the fields
return fields;
}
if ('object' === typeof fields) {
result = fields;
} else if ('object' === typeof fields) {
// { field1: boolean, field2: boolean ... }
var included = [];
var excluded = [];
var keys = Object.keys(fields);
if (!keys.length) return;
keys.forEach(function (k) {
for (i = 0, n = keys.length; i < n; i++) {
var k = keys[i];
if (fields[k]) {
included.push(k);
} else if ((k in fields) && !fields[k]) {
excluded.push(k);
}
});
}
if (included.length > 0) {
result = included;
} else if (excluded.length > 0) {
excluded.forEach(function (e) {
var index = result.indexOf(e);
for (i = 0, n = excluded.length; i < n; i++) {
var index = result.indexOf(excluded[i]);
result.splice(index, 1);
});
}
}
}
return result;
var fieldArray = [];
if (excludeUnknown) {
for (i = 0, n = result.length; i < n; i++) {
if (properties.indexOf(result[i]) !== -1) {
fieldArray.push(result[i]);
}
}
} else {
fieldArray = result;
}
return fieldArray;
}
function selectFields(fields) {

View File

@ -7,17 +7,18 @@ var mergeIncludes = utils.mergeIncludes;
var sortObjectsByIds = utils.sortObjectsByIds;
describe('util.fieldsToArray', function () {
it('Turn objects and strings into an array of fields to include when finding models', function () {
function sample(fields) {
function sample(fields, excludeUnknown) {
var properties = ['foo', 'bar', 'bat', 'baz'];
return {
expect: function (arr) {
should.deepEqual(fieldsToArray(fields, properties), arr);
}
should.deepEqual(fieldsToArray(fields, properties, excludeUnknown), arr);
}
};
}
it('Turn objects and strings into an array of fields' +
' to include when finding models', function () {
sample(false).expect(undefined);
sample(null).expect(undefined);
sample({}).expect(undefined);
@ -28,6 +29,19 @@ describe('util.fieldsToArray', function () {
sample({'bat': 0}).expect(['foo', 'bar', 'baz']);
sample({'bat': false}).expect(['foo', 'bar', 'baz']);
});
it('should exclude unknown properties', function () {
sample(false, true).expect(undefined);
sample(null, true).expect(undefined);
sample({}, true).expect(undefined);
sample('foo', true).expect(['foo']);
sample(['foo', 'unknown'], true).expect(['foo']);
sample({'foo': 1, unknown: 1}, true).expect(['foo']);
sample({'bat': true, unknown: true}, true).expect(['bat']);
sample({'bat': 0}, true).expect(['foo', 'bar', 'baz']);
sample({'bat': false}, true).expect(['foo', 'bar', 'baz']);
});
});
describe('util.removeUndefined', function () {