Skip to content

Commit

Permalink
fix(schema): fix nullable decorator and mixed type usage
Browse files Browse the repository at this point in the history
Closes: #2735
  • Loading branch information
Romakita committed Jul 10, 2024
1 parent 528386a commit ce892ee
Show file tree
Hide file tree
Showing 35 changed files with 1,516 additions and 280 deletions.
File renamed without changes
188 changes: 149 additions & 39 deletions docs/docs/model.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,131 @@ json type or when you use a mixed TypeScript types.
</Tab>
</Tabs>

You can also use @@Any@@ decorator to allow all types:

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

```typescript
import {Any} from "@tsed/schema";

export class Model {
@Any()
prop: any;
}
```

</Tab>
<Tab label="Json schema">

```json
{
"properties": {
"prop": {
"anyOf": [
{
"type": "null"
},
{
"type": "integer",
"multipleOf": 1
},
{
"type": "number"
},
{
"type": "string"
},
{
"type": "boolean"
},
{
"type": "array"
},
{
"type": "object"
}
]
}
},
"type": "object"
}
```

</Tab>
<Tab label="OS3">

```json
{
"properties": {
"prop": {
"nullable": true,
"anyOf": [
{
"type": "integer",
"multipleOf": 1
},
{
"type": "number"
},
{
"type": "string"
},
{
"type": "boolean"
},
{
"type": "array"
},
{
"type": "object"
}
]
}
},
"type": "object"
}
```

</Tab>
</Tabs>

Since v7.75.0, when you use @@Any@@ decorator combined with other decorators like @@MinLength@@, @@Minimum@@, etc. metadata will be automatically assigned to the right
type. For example, if you add a @@Minimum@@ decorator, it will be assigned to the number type.

```ts
import {Any} from "@tsed/schema";

class Model {
@Any(String, Number)
@Minimum(0)
@MaxLength(100)
prop: string | number;
}
```

Produce a json-schema as follows:

```json
{
"properties": {
"prop": {
"allOf": [
{
"type": "string",
"maxLength": 100
},
{
"type": "number",
"minimum": 0
}
]
}
},
"type": "object"
}
```

## Nullable

The @@Nullable@@ decorator is used allow a null value on a field while preserving the original Typescript type.
Expand Down Expand Up @@ -189,62 +314,50 @@ class NullableModel {
`returnsCoercedValue` will become true by default in the next major version of Ts.ED.
:::

## Any

The @@Any@@ decorator is used to allow any types:
## Nullable and mixed types <Badge text="7.75.0+"/>

<Tabs class="-code">
<Tab label="Model">
The @@Nullable@@ decorator can be used with Tuple types:

```typescript
import {Any} from "@tsed/schema";
```ts
import {Nullable} from "@tsed/schema";

export class Model {
@Any()
prop: any;
class Model {
@Nullable(String, Number)
prop: string | number | null;
}
```

</Tab>
<Tab label="Json schema">
Since v7.75.0, when you use @@Nullable@@ decorator combined with other decorators like @@MinLength@@, @@Minimum@@, etc. metadata will be automatically assigned to the right
type. For example, if you add a @@Minimum@@ decorator, it will be assigned to the number type.

```json
{
"properties": {
"prop": {
"type": ["integer", "number", "string", "boolean", "array", "object", "null"]
}
},
"type": "object"
```ts
import {Nullable} from "@tsed/schema";

class Model {
@Nullable(String, Number)
@Minimum(0)
@MaxLength(100)
prop: string | number | null;
}
```

</Tab>
<Tab label="OS3">
Produce a json-schema as follows:

```json
{
"properties": {
"prop": {
"nullable": true,
"oneOf": [
"anyOf": [
{
"type": "integer"
"type": "null"
},
{
"type": "number"
"type": "string",
"maxLength": 100
},
{
"type": "string"
},
{
"type": "boolean"
},
{
"type": "array"
},
{
"type": "object"
"type": "number",
"minimum": 0
}
]
}
Expand All @@ -253,9 +366,6 @@ export class Model {
}
```

</Tab>
</Tabs>

## Regular expressions

The @@Pattern@@ decorator is used to restrict a string to a particular regular expression. The regular expression syntax
Expand Down
47 changes: 43 additions & 4 deletions docs/docs/snippets/model/any-types.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,53 @@
{
"definitions": {},
"properties": {
"prop1": {
"type": ["integer", "number", "string", "boolean", "array", "object", "null"]
"allOf": [
{
"type": "integer",
"minLength": 1
},
{
"type": "number"
},
{
"type": "string"
},
{
"type": "boolean"
},
{
"type": "array"
},
{
"type": "object"
},
{
"type": "null"
}
]
},
"prop2": {
"type": ["string", "number", "boolean"]
"allOf": [
{
"type": "number"
},
{
"type": "string"
},
{
"type": "boolean"
}
]
},
"prop3": {
"type": ["string", "null"]
"allOf": [
{
"type": "string"
},
{
"type": "null"
}
]
}
},
"type": "object"
Expand Down
2 changes: 1 addition & 1 deletion packages/orm/mongoose/src/decorators/virtualRef.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ describe("@VirtualRef()", () => {
type: "number"
},
members: {
oneOf: [
anyOf: [
{
$ref: "#/components/schemas/TestPerson"
}
Expand Down
3 changes: 1 addition & 2 deletions packages/orm/mongoose/test/enums.integration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ describe("Enums integration", () => {
ResponseTimeThreshold: {
properties: {
status: {
$ref: "#/definitions/ComponentStatuses",
minLength: 1
$ref: "#/definitions/ComponentStatuses"
}
},
required: ["status"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ Object {
"application/json": Object {
"schema": Object {
"items": Object {
"nullable": true,
"oneOf": Array [
"anyOf": Array [
Object {
"multipleOf": 1,
"type": "integer",
},
Object {
Expand All @@ -50,6 +50,7 @@ Object {
"type": "object",
},
],
"nullable": true,
},
"type": "array",
},
Expand All @@ -76,9 +77,9 @@ Object {
"application/json": Object {
"schema": Object {
"items": Object {
"nullable": true,
"oneOf": Array [
"anyOf": Array [
Object {
"multipleOf": 1,
"type": "integer",
},
Object {
Expand All @@ -97,6 +98,7 @@ Object {
"type": "object",
},
],
"nullable": true,
},
"type": "array",
},
Expand Down Expand Up @@ -200,9 +202,9 @@ Object {
"content": Object {
"application/json": Object {
"schema": Object {
"nullable": true,
"oneOf": Array [
"anyOf": Array [
Object {
"multipleOf": 1,
"type": "integer",
},
Object {
Expand All @@ -221,6 +223,7 @@ Object {
"type": "object",
},
],
"nullable": true,
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Object {
"ActionPartial": Object {
"properties": Object {
"event": Object {
"minLength": 1,
"type": "string",
},
"type": Object {
Expand Down Expand Up @@ -71,6 +72,7 @@ Object {
"CustomActionPartial": Object {
"properties": Object {
"event": Object {
"minLength": 1,
"type": "string",
},
"meta": Object {
Expand Down Expand Up @@ -112,6 +114,7 @@ Object {
"type": "string",
},
"url": Object {
"minLength": 1,
"type": "string",
},
"value": Object {
Expand Down Expand Up @@ -203,7 +206,6 @@ Object {
"discriminator": Object {
"propertyName": "type",
},
"nullable": true,
"oneOf": Array [
Object {
"$ref": "#/components/schemas/PageView",
Expand Down
Loading

0 comments on commit ce892ee

Please sign in to comment.