diff --git a/doc/api/model/instance-methods.md b/doc/api/model/instance-methods.md index cc63bebfc..64604f39d 100644 --- a/doc/api/model/instance-methods.md +++ b/doc/api/model/instance-methods.md @@ -747,9 +747,9 @@ There are a couple of requirements for the implementation: ```js class Person extends Model { - $formatJson(json) { + $formatJson(json, options) { // Remember to call the super class's implementation. - json = super.$formatJson(json); + json = super.$formatJson(json, options); // Do your conversion here. return json; } @@ -768,9 +768,10 @@ There are a couple of requirements for the implementation: ##### Arguments -| Argument | Type | Description | -| -------- | ------ | -------------------------------- | -| json | Object | The JSON POJO in internal format | +| Argument | Type | Description | +| -------- | ------ | -------------------------------------------------------- | +| json | Object | The JSON POJO in internal format | +| options | Object | Formatting options from toJSON's options.format property | ##### Return value diff --git a/lib/model/Model.js b/lib/model/Model.js index 336957a9e..4b01ff846 100644 --- a/lib/model/Model.js +++ b/lib/model/Model.js @@ -127,7 +127,7 @@ class Model { return json; } - $formatJson(json) { + $formatJson(json, options) { return json; } diff --git a/lib/model/modelToJson.js b/lib/model/modelToJson.js index 1de996e3d..b87f79d83 100644 --- a/lib/model/modelToJson.js +++ b/lib/model/modelToJson.js @@ -15,10 +15,11 @@ function toJson(model, optIn) { pick: null, omitFromJson: model.$omitFromJson() || null, cloneObjects: modelClass.cloneObjectAttributes, + format: optIn && optIn.format, }; let json = toExternalJsonImpl(model, opt); - json = model.$formatJson(json); + json = model.$formatJson(json, opt.format); return json; } diff --git a/tests/unit/model/Model.js b/tests/unit/model/Model.js index 81157682b..1ec113c49 100644 --- a/tests/unit/model/Model.js +++ b/tests/unit/model/Model.js @@ -975,6 +975,27 @@ describe('Model', () => { expect(calls).to.equal(1); }); + it('should call $formatJson with formatting options', () => { + let calls = 0; + let json = { a: 1 }; + let opt = { c: 2 }; + + Model1.prototype.$formatJson = (jsn, o) => { + ++calls; + expect(jsn).to.eql(json); + expect(o).to.eql(opt); + jsn.b = 2; + return jsn; + }; + + let model = Model1.fromJson(json); + let output = model.$toJson({ format: opt }); + + expect(output.a).to.equal(1); + expect(output.b).to.equal(2); + expect(calls).to.equal(1); + }); + it('should call $toJson for properties of class Model', () => { let Model2 = createModelClass(); diff --git a/typings/objection/index.d.ts b/typings/objection/index.d.ts index dc45ac9a6..faf6f1c29 100644 --- a/typings/objection/index.d.ts +++ b/typings/objection/index.d.ts @@ -1267,6 +1267,7 @@ declare namespace Objection { export interface ToJsonOptions extends CloneOptions { virtuals?: boolean | string[]; + format?: Pojo } export interface ValidatorContext { @@ -1695,7 +1696,7 @@ declare namespace Objection { $formatDatabaseJson(json: Pojo): Pojo; $parseDatabaseJson(json: Pojo): Pojo; - $formatJson(json: Pojo): Pojo; + $formatJson(json: Pojo, opt?: Pojo): Pojo; $parseJson(json: Pojo, opt?: ModelOptions): Pojo; $beforeValidate(jsonSchema: JSONSchema, json: Pojo, opt: ModelOptions): JSONSchema;