Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added $formatJson options that are passed to children/relations #2033

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 6 additions & 5 deletions doc/api/model/instance-methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/model/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class Model {
return json;
}

$formatJson(json) {
$formatJson(json, options) {
return json;
}

Expand Down
3 changes: 2 additions & 1 deletion lib/model/modelToJson.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
21 changes: 21 additions & 0 deletions tests/unit/model/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
3 changes: 2 additions & 1 deletion typings/objection/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1267,6 +1267,7 @@ declare namespace Objection {

export interface ToJsonOptions extends CloneOptions {
virtuals?: boolean | string[];
format?: Pojo
}

export interface ValidatorContext {
Expand Down Expand Up @@ -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;
Expand Down