Skip to content

Commit

Permalink
fix: Correctly handle negative numbers, fix #65
Browse files Browse the repository at this point in the history
  • Loading branch information
Embraser01 committed Aug 21, 2024
1 parent 021cff8 commit 4a80fc3
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .yarn/versions/66b0f787.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
releases:
"@typoas/generator": patch

declined:
- "@typoas/cli"
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ exports[`create type from schema should handle primitive boolean enums 1`] = `"t

exports[`create type from schema should handle primitive boolean schema 1`] = `"boolean"`;

exports[`create type from schema should handle primitive negative number enums 1`] = `"-1 | 6"`;

exports[`create type from schema should handle primitive number enums 1`] = `"1 | 6"`;

exports[`create type from schema should handle primitive number schema 1`] = `"number"`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ describe('create type from schema', () => {
expect(getStringFromNode(node)).toMatchSnapshot();
});

it('should handle primitive negative number enums', () => {
const schema: SchemaObject = { type: 'number', enum: [-1, 6] };

const node = createTypeFromSchema(schema, new Context());

expect(getStringFromNode(node)).toMatchSnapshot();
});

it('should handle dates', () => {
const schema: SchemaObject = { type: 'string', format: 'date-time' };

Expand Down
9 changes: 8 additions & 1 deletion packages/typoas-generator/src/generator/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,14 @@ export function createTypeFromSchema(
return e ? factory.createTrue() : factory.createFalse();
}
if (typeof e === 'number') {
return factory.createNumericLiteral(e);
const literal = factory.createNumericLiteral(Math.abs(e));
if (e >= 0) {
return literal;
}
return factory.createPrefixUnaryExpression(
SyntaxKind.MinusToken,
literal,
);
}
return factory.createStringLiteral(e as string, true);
})
Expand Down

0 comments on commit 4a80fc3

Please sign in to comment.