Skip to content

Commit

Permalink
formatJson options
Browse files Browse the repository at this point in the history
  • Loading branch information
amit-meshbey committed Apr 10, 2021
1 parent 260b284 commit b8505ee
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 8 deletions.
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
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

0 comments on commit b8505ee

Please sign in to comment.