Skip to content

Commit

Permalink
feat: Serialize messages and fields when converting toJSON
Browse files Browse the repository at this point in the history
* Serialize SimpleMessagesProvider into JSON

* pin @swc/core to v1.3.82

* use deepEqual
  • Loading branch information
TiBianMod committed Oct 16, 2023
1 parent 16bd6e8 commit 72d098d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"@japa/assert": "^2.0.0-1",
"@japa/expect-type": "^2.0.0-0",
"@japa/runner": "^3.0.0-6",
"@swc/core": "^1.3.70",
"@swc/core": "1.3.82",
"@types/node": "^20.4.2",
"benchmark": "^2.1.4",
"c8": "^8.0.1",
Expand Down
7 changes: 7 additions & 0 deletions src/messages_provider/simple_messages_provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,11 @@ export class SimpleMessagesProvider implements MessagesProviderContact {
...args,
})
}

toJSON() {
return {
messages: this.#messages,
fields: this.#fields,
}
}
}
28 changes: 28 additions & 0 deletions tests/unit/simple_messages_provider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,31 @@ test.group('Simple messages provider | interpolation', () => {
)
})
})

test.group('Simple messages provider', () => {
test('serialize to json', ({ assert }) => {
const provider = new SimpleMessagesProvider(
{
required: 'The {{ field }} field is required',
string: 'The value of {{ field }} field must be a string',
email: 'The value is not a valid email address',
},
{
first_name: 'first name',
last_name: 'last name',
}
)

assert.deepEqual(provider.toJSON(), {
messages: {
required: 'The {{ field }} field is required',
string: 'The value of {{ field }} field must be a string',
email: 'The value is not a valid email address',
},
fields: {
first_name: 'first name',
last_name: 'last name',
},
})
})
})

0 comments on commit 72d098d

Please sign in to comment.