Skip to content

Commit 6c1201c

Browse files
committed
Pseudo private function _convertCase
Validate convertCase option values
1 parent 70a06fd commit 6c1201c

File tree

2 files changed

+7
-33
lines changed

2 files changed

+7
-33
lines changed

lib/JSONAPISerializer.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ module.exports = class JSONAPISerializer {
2222
})).default({}),
2323
topLevelLinks: joi.object().default({}),
2424
topLevelMeta: joi.object().default({}),
25-
convertCase: joi.string(),
25+
convertCase: joi.string().valid('kebab-case', 'snake_case', 'camelCase'),
2626
}).required();
2727

2828
const validated = joi.validate(options, optionsSchema);
@@ -115,7 +115,7 @@ module.exports = class JSONAPISerializer {
115115
let serializedAttributes = _.pick(data, _.difference(Object.keys(data), _.concat([options.id], Object.keys(options.relationships), options.blacklist)));
116116

117117
if (options.convertCase) {
118-
serializedAttributes = this.convertCase(serializedAttributes, options.convertCase);
118+
serializedAttributes = this._convertCase(serializedAttributes, options.convertCase);
119119
}
120120

121121
return serializedAttributes;
@@ -132,7 +132,7 @@ module.exports = class JSONAPISerializer {
132132
data: this.serializeRelationship(rOptions.type, data[relationship], this.schemas[options.relationships[relationship].type][schema], included),
133133
};
134134

135-
relationship = (options.convertCase) ? this.convertCase(relationship, options.convertCase) : relationship;
135+
relationship = (options.convertCase) ? this._convertCase(relationship, options.convertCase) : relationship;
136136

137137
_.set(serializedRelationships, relationship, serializeRelationship);
138138
});
@@ -187,14 +187,14 @@ module.exports = class JSONAPISerializer {
187187
return !_.isEmpty(processedOptions) ? processedOptions : undefined;
188188
}
189189

190-
convertCase(data, convertCaseOptions) {
190+
_convertCase(data, convertCaseOptions) {
191191
let converted;
192192
if (_.isArray(data) || _.isPlainObject(data)) {
193193
converted = _.transform(data, (result, value, key) => {
194194
if (_.isArray(value) || _.isPlainObject(value)) {
195-
result[this.convertCase(key, convertCaseOptions)] = this.convertCase(value, convertCaseOptions);
195+
result[this._convertCase(key, convertCaseOptions)] = this._convertCase(value, convertCaseOptions);
196196
} else {
197-
result[this.convertCase(key, convertCaseOptions)] = value;
197+
result[this._convertCase(key, convertCaseOptions)] = value;
198198
}
199199
});
200200
} else {
@@ -208,9 +208,7 @@ module.exports = class JSONAPISerializer {
208208
case 'camelCase':
209209
converted = _.camelCase(data);
210210
break;
211-
default:
212-
converted = data;
213-
break;
211+
default: // Do nothing
214212
}
215213
}
216214

test/unit/JSONAPISerializer.test.js

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -417,30 +417,6 @@ describe('JSONAPISerializer', function() {
417417
expect(serializedAttributes.address).to.have.property('zipCode');
418418
done();
419419
});
420-
421-
it('should not convert attributes with unsupported format', function(done) {
422-
const Serializer = new JSONAPISerializer();
423-
Serializer.register('articles', {
424-
convertCase: 'unsupported'
425-
});
426-
const data = {
427-
id: '1',
428-
firstName: 'firstName',
429-
lastName: 'lastName',
430-
articles: [{
431-
createdAt: '2016-06-04T06:09:24.864Z'
432-
}],
433-
address: {
434-
zipCode: 123456
435-
}
436-
};
437-
const serializedAttributes = Serializer.serializeAttributes(data, Serializer.schemas.articles.default);
438-
expect(serializedAttributes).to.have.property('firstName');
439-
expect(serializedAttributes).to.have.property('lastName');
440-
expect(serializedAttributes.articles[0]).to.have.property('createdAt');
441-
expect(serializedAttributes.address).to.have.property('zipCode');
442-
done();
443-
});
444420
});
445421

446422
describe('serializeIncluded', function() {

0 commit comments

Comments
 (0)