Skip to content

Commit

Permalink
feat(schema): Add Generic date support and improve documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Romakita committed Apr 24, 2021
1 parent 44c34d1 commit edbd2e0
Show file tree
Hide file tree
Showing 12 changed files with 860 additions and 150 deletions.
36 changes: 36 additions & 0 deletions docs/docs/model.md
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,42 @@ class MyController {
</Tab>
</Tabs>

### Generics with Types

<Tabs class="-code">
<Tab label="String">

<<< @/docs/snippets/model/generics-string.ts

</Tab>
<Tab label="Date">

<<< @/docs/snippets/model/generics-date.ts

</Tab>
<Tab label="Date">

<<< @/docs/snippets/model/generics-enum.ts

</Tab>
</Tabs>


### Generics with Functional API

<Tabs class="-code">
<Tab label="String">

<<< @/docs/snippets/model/generics-string-api.ts

</Tab>
<Tab label="Date">

<<< @/docs/snippets/model/generics-date-api.ts

</Tab>
</Tabs>

## Pagination

The following advanced example will show you how you can combine the different Ts.ED features to describe Pagination.
Expand Down
30 changes: 30 additions & 0 deletions docs/docs/snippets/model/generic-date-api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import {date, GenericOf, Generics, getJsonSchema, Property, string} from "@tsed/schema";

@Generics("T")
class UserProperty<T> {
@Property("T")
value: T;
}

class Adjustment {
@GenericOf(date().format("date-time"))
adjustment: UserProperty<Date>;
}

console.log(getJsonSchema(Adjustment));
/* OUTPUT:
{
"properties": {
"adjustment": {
"properties": {
"value": {
"type": "string",
"format": "date-time"
}
},
"type": "object"
}
},
"type": "object"
}
*/
30 changes: 30 additions & 0 deletions docs/docs/snippets/model/generic-date.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import {GenericOf, Generics, getJsonSchema, Property} from "@tsed/schema";

@Generics("T")
class UserProperty<T> {
@Property("T")
value: T;
}

class Adjustment {
@GenericOf(Date)
adjustment: UserProperty<Date>;
}

console.log(getJsonSchema(Adjustment))
/* OUTPUT:
{
"properties": {
"adjustment": {
"properties": {
"value": {
"format": "date-time",
"type": "string"
}
},
"type": "object"
}
},
"type": "object"
}
*/
38 changes: 38 additions & 0 deletions docs/docs/snippets/model/generic-enum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import {GenericOf, Generics, getJsonSchema, Property} from "@tsed/schema";

enum AdjustmentType {
PRICE = "price",
DELAY = "delay",
}

@Generics("T")
class UserProperty<T> {
@Property("T")
value: T;
}

class Adjustment {
@GenericOf(AdjustmentType)
adjustment: UserProperty<AdjustmentType>;
}

console.log(getJsonSchema(Adjustment));
/* OUTPUT:
{
"properties": {
"adjustment": {
"properties": {
"value": {
"enum": [
"price"
"delay"
],
"type": "string"
}
},
"type": "object"
}
},
"type": "object"
}
*/
30 changes: 30 additions & 0 deletions docs/docs/snippets/model/generic-string-api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import {GenericOf, Generics, getJsonSchema, Property, string} from "@tsed/schema";

@Generics("T")
class UserProperty<T> {
@Property("T")
value: T;
}

class Adjustment {
@GenericOf(string().pattern(/[a-z]/))
adjustment: UserProperty<string>;
}

console.log(getJsonSchema(Adjustment))
/* OUTPUT:
{
"properties": {
"adjustment": {
"properties": {
"value": {
"type": "string",
"pattern": "[a-z]"
}
},
"type": "object"
}
},
"type": "object"
}
*/
29 changes: 29 additions & 0 deletions docs/docs/snippets/model/generic-string.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {GenericOf, Generics, getJsonSchema, Property} from "@tsed/schema";

@Generics("T")
class UserProperty<T> {
@Property("T")
value: T;
}

class Adjustment {
@GenericOf(String)
adjustment: UserProperty<string>;
}

console.log(getJsonSchema(Adjustment))
/* OUTPUT:
{
"properties": {
"adjustment": {
"properties": {
"value": {
"type": "string"
}
},
"type": "object"
}
},
"type": "object"
}
*/
5 changes: 5 additions & 0 deletions packages/json-mapper/src/utils/deserialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ export function plainObjectToClass<T = any>(src: any, options: JsonDeserializerO
const genericLabels = propStore.parent.schema.genericLabels || [];

next.type = genericTypes[genericLabels.indexOf(propStore.schema.genericType)] || Object;

if (next.type instanceof JsonSchema) {
next.type = next.type.getTarget();
}

next.nestedGenerics = nestedGenerics;
}

Expand Down
Loading

0 comments on commit edbd2e0

Please sign in to comment.