Skip to content

Commit

Permalink
fix: resolve issue in decimal128 schema builder's setToStringGetter
Browse files Browse the repository at this point in the history
… not handling undefined input
  • Loading branch information
kiki-kanri committed Dec 15, 2024
1 parent 980a557 commit 10434c6
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/schema-builders/decimal128.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ interface BaseProps {
}

interface ToStringGetterSchema {
get: (value: Types.Decimal128) => string;
get: (value?: Types.Decimal128) => string | undefined;
}

interface ToStringSetterSchema {
Expand Down Expand Up @@ -87,7 +87,7 @@ export function decimal128SchemaBuilder() {
}

if (key === 'setToStringGetter') {
schema.get = (value: Types.Decimal128) => value.toString();
schema.get = (value?: Types.Decimal128) => value?.toString();
return receiver;
}

Expand Down
5 changes: 5 additions & 0 deletions tests/schema-builders/decimal128.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ describe('decimal128SchemaBuilder', () => {
expect(schema.get(new Types.Decimal128('114514.1919810'))).toEqual('114514.1919810');
});

it('should return the undefined representation of Decimal128 when using setToStringGetter', () => {
const schema = decimal128SchemaBuilder().setToStringGetter.nonRequired;
expect(schema.get()).toBeUndefined();
});

it('should set the value correctly with rounding and fixed decimal places', () => {
const schema1 = decimal128SchemaBuilder().setRoundAndToFixedSetter().nonRequired;
expect(schema1.set('114514.1919810')).toEqual('114514.19');
Expand Down

0 comments on commit 10434c6

Please sign in to comment.