@tsed/schema 8.0.1 → 8.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +11 -10
- package/src/components/async-api/channelsMapper.ts +81 -0
- package/src/components/async-api/generate.ts +24 -0
- package/src/components/async-api/messageMapper.ts +62 -0
- package/src/components/async-api/payloadMapper.ts +61 -0
- package/src/components/async-api/responseMapper.ts +70 -0
- package/src/components/default/anyMapper.spec.ts +7 -0
- package/src/components/default/anyMapper.ts +33 -0
- package/src/components/default/classMapper.ts +55 -0
- package/src/components/default/discriminatorMappingMapper.ts +36 -0
- package/src/components/default/enumsMapper.spec.ts +64 -0
- package/src/components/default/enumsMapper.ts +17 -0
- package/src/components/default/genericsMapper.ts +70 -0
- package/src/components/default/inheritedClassMapper.ts +24 -0
- package/src/components/default/itemMapper.ts +8 -0
- package/src/components/default/lazyRefMapper.ts +21 -0
- package/src/components/default/mapMapper.ts +25 -0
- package/src/components/default/nullableMapper.ts +55 -0
- package/src/components/default/objectMapper.ts +36 -0
- package/src/components/default/ofMapper.ts +16 -0
- package/src/components/default/propertiesMapper.ts +25 -0
- package/src/components/default/requiredMapper.ts +69 -0
- package/src/components/default/schemaMapper.ts +151 -0
- package/src/components/index.ts +36 -0
- package/src/components/open-spec/discriminatorMappingMapper.ts +7 -0
- package/src/components/open-spec/enumsMapper.ts +20 -0
- package/src/components/open-spec/generate.ts +29 -0
- package/src/components/open-spec/nullableMapper.ts +51 -0
- package/src/components/open-spec/operationInFilesMapper.ts +32 -0
- package/src/components/open-spec/operationInParameterMapper.ts +56 -0
- package/src/components/open-spec/operationInParametersMapper.ts +9 -0
- package/src/components/open-spec/operationInQueryMapper.ts +74 -0
- package/src/components/open-spec/operationMapper.ts +65 -0
- package/src/components/open-spec/operationMediaMapper.ts +11 -0
- package/src/components/open-spec/operationRequestBodyMapper.ts +82 -0
- package/src/components/open-spec/operationResponseMapper.ts +26 -0
- package/src/components/open-spec/pathsMapper.ts +132 -0
- package/src/constants/OperationVerbs.ts +29 -0
- package/src/constants/httpStatusMessages.ts +21 -0
- package/src/constants/jsonSchemaProperties.ts +17 -0
- package/src/decorators/class/children.ts +22 -0
- package/src/decorators/class/discriminatorValue.ts +53 -0
- package/src/decorators/collections/collectionContains.ts +25 -0
- package/src/decorators/collections/collectionOf.spec.ts +327 -0
- package/src/decorators/collections/collectionOf.ts +182 -0
- package/src/decorators/collections/maxItems.spec.ts +40 -0
- package/src/decorators/collections/maxItems.ts +59 -0
- package/src/decorators/collections/maxProperties.spec.ts +109 -0
- package/src/decorators/collections/maxProperties.ts +82 -0
- package/src/decorators/collections/minItems.spec.ts +40 -0
- package/src/decorators/collections/minItems.ts +63 -0
- package/src/decorators/collections/minProperties.spec.ts +109 -0
- package/src/decorators/collections/minProperties.ts +81 -0
- package/src/decorators/collections/uniqueItems.spec.ts +28 -0
- package/src/decorators/collections/uniqueItems.ts +59 -0
- package/src/decorators/common/__snapshots__/groups.spec.ts.snap +321 -0
- package/src/decorators/common/additionalProperties.spec.ts +133 -0
- package/src/decorators/common/additionalProperties.ts +17 -0
- package/src/decorators/common/allOf.spec.ts +276 -0
- package/src/decorators/common/allOf.ts +19 -0
- package/src/decorators/common/allow.spec.ts +130 -0
- package/src/decorators/common/allow.ts +50 -0
- package/src/decorators/common/any.spec.ts +109 -0
- package/src/decorators/common/any.ts +45 -0
- package/src/decorators/common/anyOf.spec.ts +270 -0
- package/src/decorators/common/anyOf.ts +23 -0
- package/src/decorators/common/const.spec.ts +98 -0
- package/src/decorators/common/const.ts +68 -0
- package/src/decorators/common/customKey.spec.ts +105 -0
- package/src/decorators/common/customKey.ts +39 -0
- package/src/decorators/common/default.spec.ts +25 -0
- package/src/decorators/common/default.ts +47 -0
- package/src/decorators/common/defaultMsg.spec.ts +22 -0
- package/src/decorators/common/defaultMsg.ts +38 -0
- package/src/decorators/common/description.spec.ts +166 -0
- package/src/decorators/common/description.ts +77 -0
- package/src/decorators/common/discriminatorKey.ts +14 -0
- package/src/decorators/common/enum.spec.ts +305 -0
- package/src/decorators/common/enum.ts +111 -0
- package/src/decorators/common/errorMsg.spec.ts +57 -0
- package/src/decorators/common/errorMsg.ts +14 -0
- package/src/decorators/common/example.spec.ts +93 -0
- package/src/decorators/common/example.ts +18 -0
- package/src/decorators/common/examples.spec.ts +101 -0
- package/src/decorators/common/examples.ts +20 -0
- package/src/decorators/common/exclusiveMaximum.spec.ts +47 -0
- package/src/decorators/common/exclusiveMaximum.ts +75 -0
- package/src/decorators/common/exclusiveMinimum.spec.ts +47 -0
- package/src/decorators/common/exclusiveMinimum.ts +75 -0
- package/src/decorators/common/format.spec.ts +149 -0
- package/src/decorators/common/format.ts +486 -0
- package/src/decorators/common/forwardGroups.spec.ts +270 -0
- package/src/decorators/common/forwardGroups.ts +16 -0
- package/src/decorators/common/groups.spec.ts +968 -0
- package/src/decorators/common/groups.ts +66 -0
- package/src/decorators/common/hidden.spec.ts +52 -0
- package/src/decorators/common/hidden.ts +36 -0
- package/src/decorators/common/ignore.spec.ts +88 -0
- package/src/decorators/common/ignore.ts +66 -0
- package/src/decorators/common/integer.spec.ts +110 -0
- package/src/decorators/common/integer.ts +69 -0
- package/src/decorators/common/jsonEntityFn.spec.ts +16 -0
- package/src/decorators/common/jsonEntityFn.ts +33 -0
- package/src/decorators/common/maxLength.spec.ts +61 -0
- package/src/decorators/common/maxLength.ts +88 -0
- package/src/decorators/common/maximum.spec.ts +73 -0
- package/src/decorators/common/maximum.ts +146 -0
- package/src/decorators/common/minLength.spec.ts +186 -0
- package/src/decorators/common/minLength.ts +89 -0
- package/src/decorators/common/minimum.spec.ts +74 -0
- package/src/decorators/common/minimum.ts +145 -0
- package/src/decorators/common/multipleOf.spec.ts +33 -0
- package/src/decorators/common/multipleOf.ts +80 -0
- package/src/decorators/common/name.spec.ts +56 -0
- package/src/decorators/common/name.ts +50 -0
- package/src/decorators/common/nullable.spec.ts +818 -0
- package/src/decorators/common/nullable.ts +32 -0
- package/src/decorators/common/oneOf.spec.ts +301 -0
- package/src/decorators/common/oneOf.ts +24 -0
- package/src/decorators/common/optional.spec.ts +43 -0
- package/src/decorators/common/optional.ts +26 -0
- package/src/decorators/common/pattern.spec.ts +27 -0
- package/src/decorators/common/pattern.ts +102 -0
- package/src/decorators/common/property.ts +24 -0
- package/src/decorators/common/readOnly.spec.ts +135 -0
- package/src/decorators/common/readOnly.ts +17 -0
- package/src/decorators/common/recordOf.spec.ts +41 -0
- package/src/decorators/common/recordOf.ts +59 -0
- package/src/decorators/common/required.spec.ts +188 -0
- package/src/decorators/common/required.ts +50 -0
- package/src/decorators/common/requiredGroups.spec.ts +127 -0
- package/src/decorators/common/requiredGroups.ts +23 -0
- package/src/decorators/common/schema.spec.ts +23 -0
- package/src/decorators/common/schema.ts +64 -0
- package/src/decorators/common/title.spec.ts +20 -0
- package/src/decorators/common/title.ts +45 -0
- package/src/decorators/common/typeError.spec.ts +22 -0
- package/src/decorators/common/typeError.ts +15 -0
- package/src/decorators/common/writeOnly.spec.ts +105 -0
- package/src/decorators/common/writeOnly.ts +17 -0
- package/src/decorators/config/jsonEntityComponent.ts +16 -0
- package/src/decorators/generics/genericOf.spec.ts +246 -0
- package/src/decorators/generics/genericOf.ts +112 -0
- package/src/decorators/generics/generics.ts +22 -0
- package/src/decorators/operations/acceptMime.spec.ts +39 -0
- package/src/decorators/operations/acceptMime.ts +25 -0
- package/src/decorators/operations/consumes.spec.ts +89 -0
- package/src/decorators/operations/consumes.ts +37 -0
- package/src/decorators/operations/contentType.spec.ts +40 -0
- package/src/decorators/operations/contentType.ts +24 -0
- package/src/decorators/operations/deprecated.spec.ts +223 -0
- package/src/decorators/operations/deprecated.ts +49 -0
- package/src/decorators/operations/header.spec.ts +214 -0
- package/src/decorators/operations/header.ts +62 -0
- package/src/decorators/operations/in.spec.ts +321 -0
- package/src/decorators/operations/in.ts +141 -0
- package/src/decorators/operations/inFile.spec.ts +209 -0
- package/src/decorators/operations/inFile.ts +55 -0
- package/src/decorators/operations/location.spec.ts +51 -0
- package/src/decorators/operations/location.ts +26 -0
- package/src/decorators/operations/operation.ts +119 -0
- package/src/decorators/operations/operationId.spec.ts +43 -0
- package/src/decorators/operations/operationId.ts +22 -0
- package/src/decorators/operations/operationPath.spec.ts +42 -0
- package/src/decorators/operations/operationPath.ts +33 -0
- package/src/decorators/operations/partial.spec.ts +249 -0
- package/src/decorators/operations/partial.ts +14 -0
- package/src/decorators/operations/path.spec.ts +47 -0
- package/src/decorators/operations/path.ts +27 -0
- package/src/decorators/operations/produces.spec.ts +89 -0
- package/src/decorators/operations/produces.ts +44 -0
- package/src/decorators/operations/publish.spec.ts +26 -0
- package/src/decorators/operations/publish.ts +6 -0
- package/src/decorators/operations/redirect.spec.ts +146 -0
- package/src/decorators/operations/redirect.ts +82 -0
- package/src/decorators/operations/returns.spec.ts +1393 -0
- package/src/decorators/operations/returns.ts +662 -0
- package/src/decorators/operations/route.spec.ts +211 -0
- package/src/decorators/operations/route.ts +154 -0
- package/src/decorators/operations/security.spec.ts +162 -0
- package/src/decorators/operations/security.ts +83 -0
- package/src/decorators/operations/status.spec.ts +582 -0
- package/src/decorators/operations/status.ts +130 -0
- package/src/decorators/operations/subscribe.spec.ts +32 -0
- package/src/decorators/operations/subscribe.ts +6 -0
- package/src/decorators/operations/summary.spec.ts +45 -0
- package/src/decorators/operations/summary.ts +32 -0
- package/src/decorators/operations/tags.spec.ts +113 -0
- package/src/decorators/operations/tags.ts +53 -0
- package/src/decorators/operations/view.spec.ts +16 -0
- package/src/decorators/operations/view.ts +27 -0
- package/src/domain/DecoratorContext.ts +116 -0
- package/src/domain/JsonAliasMap.ts +19 -0
- package/src/domain/JsonClassStore.ts +35 -0
- package/src/domain/JsonDiscriminator.spec.ts +65 -0
- package/src/domain/JsonDiscriminator.ts +52 -0
- package/src/domain/JsonEntityStore.spec.ts +277 -0
- package/src/domain/JsonEntityStore.ts +284 -0
- package/src/domain/JsonFormatTypes.ts +17 -0
- package/src/domain/JsonLazyRef.ts +21 -0
- package/src/domain/JsonMap.spec.ts +15 -0
- package/src/domain/JsonMap.ts +33 -0
- package/src/domain/JsonMedia.ts +24 -0
- package/src/domain/JsonMethodStore.spec.ts +301 -0
- package/src/domain/JsonMethodStore.ts +212 -0
- package/src/domain/JsonOperation.spec.ts +126 -0
- package/src/domain/JsonOperation.ts +219 -0
- package/src/domain/JsonOperationPathsMap.ts +16 -0
- package/src/domain/JsonOperationRoute.spec.ts +109 -0
- package/src/domain/JsonOperationRoute.ts +75 -0
- package/src/domain/JsonParameter.ts +70 -0
- package/src/domain/JsonParameterStore.spec.ts +212 -0
- package/src/domain/JsonParameterStore.ts +138 -0
- package/src/domain/JsonParameterTypes.spec.ts +12 -0
- package/src/domain/JsonParameterTypes.ts +20 -0
- package/src/domain/JsonPropertyStore.spec.ts +177 -0
- package/src/domain/JsonPropertyStore.ts +92 -0
- package/src/domain/JsonRequestBody.ts +47 -0
- package/src/domain/JsonResponse.spec.ts +33 -0
- package/src/domain/JsonResponse.ts +64 -0
- package/src/domain/JsonSchema.spec.ts +1470 -0
- package/src/domain/JsonSchema.ts +1049 -0
- package/src/domain/SpecTypes.ts +6 -0
- package/src/hooks/alterIgnore.ts +25 -0
- package/src/hooks/alterOneOf.ts +67 -0
- package/src/hooks/alterRequiredGroups.ts +12 -0
- package/src/index.ts +185 -0
- package/src/interfaces/IgnoreCallback.ts +5 -0
- package/src/interfaces/JsonHookContext.ts +18 -0
- package/src/interfaces/JsonOpenSpec.ts +7 -0
- package/src/interfaces/JsonSchemaOptions.ts +35 -0
- package/src/registries/JsonSchemaMapperContainer.ts +52 -0
- package/src/registries/enumRegistries.ts +3 -0
- package/src/utils/__snapshots__/generateSpec.spec.ts.snap +109 -0
- package/src/utils/buildPath.ts +8 -0
- package/src/utils/concatParameters.ts +12 -0
- package/src/utils/concatPath.spec.ts +23 -0
- package/src/utils/concatPath.ts +24 -0
- package/src/utils/defineStatusModel.ts +11 -0
- package/src/utils/from.spec.ts +243 -0
- package/src/utils/from.ts +281 -0
- package/src/utils/generateSpec.spec.ts +1262 -0
- package/src/utils/generateSpec.ts +54 -0
- package/src/utils/generics.ts +83 -0
- package/src/utils/getComputedType.spec.ts +45 -0
- package/src/utils/getComputedType.ts +39 -0
- package/src/utils/getInheritedStores.spec.ts +47 -0
- package/src/utils/getInheritedStores.ts +23 -0
- package/src/utils/getJsonEntityStore.ts +36 -0
- package/src/utils/getJsonPathParameters.spec.ts +126 -0
- package/src/utils/getJsonPathParameters.ts +97 -0
- package/src/utils/getJsonSchema.spec.ts +695 -0
- package/src/utils/getJsonSchema.ts +69 -0
- package/src/utils/getJsonType.spec.ts +28 -0
- package/src/utils/getJsonType.ts +46 -0
- package/src/utils/getOperationsRoutes.spec.ts +284 -0
- package/src/utils/getOperationsRoutes.ts +56 -0
- package/src/utils/getOperationsStore.spec.ts +47 -0
- package/src/utils/getOperationsStores.ts +37 -0
- package/src/utils/getPropertiesStores.spec.ts +138 -0
- package/src/utils/getPropertiesStores.ts +66 -0
- package/src/utils/getSpec.spec.ts +610 -0
- package/src/utils/getSpec.ts +117 -0
- package/src/utils/getSpecType.spec.ts +16 -0
- package/src/utils/getSpecType.ts +19 -0
- package/src/utils/isSuccessStatus.spec.ts +22 -0
- package/src/utils/isSuccessStatus.ts +2 -0
- package/src/utils/mapHeaders.spec.ts +57 -0
- package/src/utils/mapHeaders.ts +33 -0
- package/src/utils/mapOpenSpec.ts +44 -0
- package/src/utils/mapOpenSpec2.ts +50 -0
- package/src/utils/mapOpenSpec3.ts +25 -0
- package/src/utils/mapOpenSpecInfo.ts +19 -0
- package/src/utils/mapOperationOptions.ts +34 -0
- package/src/utils/matchGroups.spec.ts +85 -0
- package/src/utils/matchGroups.ts +64 -0
- package/src/utils/mergeSpec.spec.ts +533 -0
- package/src/utils/mergeSpec.ts +54 -0
- package/src/utils/operationIdFormatter.spec.ts +11 -0
- package/src/utils/operationIdFormatter.ts +49 -0
- package/src/utils/ref.ts +65 -0
- package/src/utils/removeHiddenOperation.ts +5 -0
- package/src/utils/serializeEnumValues.ts +27 -0
- package/src/utils/somethingOf.ts +11 -0
- package/src/utils/toJsonMapCollection.spec.ts +18 -0
- package/src/utils/toJsonMapCollection.ts +12 -0
- package/src/utils/toJsonRegex.ts +7 -0
- package/src/utils/transformToOS2.spec.ts +177 -0
- package/src/utils/transformToOS2.ts +607 -0
- package/src/utils/withErrorMsg.spec.ts +16 -0
- package/src/utils/withErrorMsg.ts +56 -0
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
import "../index.js";
|
|
2
|
+
|
|
3
|
+
import {CollectionOf} from "../decorators/collections/collectionOf.js";
|
|
4
|
+
import {Property} from "../decorators/common/property.js";
|
|
5
|
+
import {
|
|
6
|
+
allOf,
|
|
7
|
+
any,
|
|
8
|
+
anyOf,
|
|
9
|
+
array,
|
|
10
|
+
boolean,
|
|
11
|
+
date,
|
|
12
|
+
datetime,
|
|
13
|
+
email,
|
|
14
|
+
from,
|
|
15
|
+
integer,
|
|
16
|
+
lazyRef,
|
|
17
|
+
map,
|
|
18
|
+
number,
|
|
19
|
+
object,
|
|
20
|
+
set,
|
|
21
|
+
string,
|
|
22
|
+
time,
|
|
23
|
+
uri,
|
|
24
|
+
url
|
|
25
|
+
} from "./from.js";
|
|
26
|
+
|
|
27
|
+
describe("from", () => {
|
|
28
|
+
it("should declare a model", () => {
|
|
29
|
+
expect(from().toJSON()).toEqual({type: "object"});
|
|
30
|
+
expect(string().toJSON()).toEqual({type: "string"});
|
|
31
|
+
expect(number().toJSON()).toEqual({type: "number"});
|
|
32
|
+
expect(integer().toJSON()).toEqual({
|
|
33
|
+
multipleOf: 1,
|
|
34
|
+
type: "integer"
|
|
35
|
+
});
|
|
36
|
+
expect(boolean().toJSON()).toEqual({type: "boolean"});
|
|
37
|
+
expect(date().toJSON()).toEqual({
|
|
38
|
+
format: "date",
|
|
39
|
+
type: "string"
|
|
40
|
+
});
|
|
41
|
+
expect(datetime().toJSON()).toEqual({
|
|
42
|
+
format: "date-time",
|
|
43
|
+
type: "string"
|
|
44
|
+
});
|
|
45
|
+
expect(time().toJSON()).toEqual({
|
|
46
|
+
format: "time",
|
|
47
|
+
type: "string"
|
|
48
|
+
});
|
|
49
|
+
expect(email().toJSON()).toEqual({
|
|
50
|
+
format: "email",
|
|
51
|
+
type: "string"
|
|
52
|
+
});
|
|
53
|
+
expect(email().toJSON()).toEqual({
|
|
54
|
+
format: "email",
|
|
55
|
+
type: "string"
|
|
56
|
+
});
|
|
57
|
+
expect(uri().toJSON()).toEqual({
|
|
58
|
+
format: "uri",
|
|
59
|
+
type: "string"
|
|
60
|
+
});
|
|
61
|
+
expect(url().toJSON()).toEqual({
|
|
62
|
+
format: "url",
|
|
63
|
+
type: "string"
|
|
64
|
+
});
|
|
65
|
+
expect(set().toJSON()).toEqual({
|
|
66
|
+
type: "array",
|
|
67
|
+
uniqueItems: true
|
|
68
|
+
});
|
|
69
|
+
expect(map().toJSON()).toEqual({
|
|
70
|
+
additionalProperties: true,
|
|
71
|
+
type: "object"
|
|
72
|
+
});
|
|
73
|
+
expect(array().toJSON()).toEqual({type: "array"});
|
|
74
|
+
expect(any().toJSON()).toEqual({
|
|
75
|
+
anyOf: [
|
|
76
|
+
{
|
|
77
|
+
type: "null"
|
|
78
|
+
},
|
|
79
|
+
{
|
|
80
|
+
multipleOf: 1,
|
|
81
|
+
type: "integer"
|
|
82
|
+
},
|
|
83
|
+
{
|
|
84
|
+
type: "number"
|
|
85
|
+
},
|
|
86
|
+
{
|
|
87
|
+
type: "string"
|
|
88
|
+
},
|
|
89
|
+
{
|
|
90
|
+
type: "boolean"
|
|
91
|
+
},
|
|
92
|
+
{
|
|
93
|
+
type: "array"
|
|
94
|
+
},
|
|
95
|
+
{
|
|
96
|
+
type: "object"
|
|
97
|
+
}
|
|
98
|
+
]
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
expect(anyOf(string(), number()).toJSON()).toEqual({
|
|
102
|
+
anyOf: [{type: "string"}, {type: "number"}]
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
expect(allOf(string(), number()).toJSON()).toEqual({
|
|
106
|
+
allOf: [{type: "string"}, {type: "number"}]
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
it("should create ref when use a label", () => {
|
|
110
|
+
const ErrorSchema = object({
|
|
111
|
+
name: string().required().description("The error name"),
|
|
112
|
+
message: string().required().description("An error message")
|
|
113
|
+
})
|
|
114
|
+
.label("GenericError")
|
|
115
|
+
.unknown();
|
|
116
|
+
|
|
117
|
+
const ExceptionSchema = object()
|
|
118
|
+
.allOf([ErrorSchema])
|
|
119
|
+
.properties({
|
|
120
|
+
name: string().required().description("The error name"),
|
|
121
|
+
message: string().required().description("An error message"),
|
|
122
|
+
status: number().required().description("The status code of the exception"),
|
|
123
|
+
errors: array().items(ErrorSchema).description("A list of related errors"),
|
|
124
|
+
stack: string().description("The stack trace (only in development mode)")
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
expect(ExceptionSchema.toJSON()).toEqual({
|
|
128
|
+
allOf: [
|
|
129
|
+
{
|
|
130
|
+
$ref: "#/definitions/GenericError"
|
|
131
|
+
}
|
|
132
|
+
],
|
|
133
|
+
definitions: {
|
|
134
|
+
GenericError: {
|
|
135
|
+
additionalProperties: true,
|
|
136
|
+
properties: {
|
|
137
|
+
message: {
|
|
138
|
+
description: "An error message",
|
|
139
|
+
minLength: 1,
|
|
140
|
+
type: "string"
|
|
141
|
+
},
|
|
142
|
+
name: {
|
|
143
|
+
description: "The error name",
|
|
144
|
+
minLength: 1,
|
|
145
|
+
type: "string"
|
|
146
|
+
}
|
|
147
|
+
},
|
|
148
|
+
required: ["name", "message"],
|
|
149
|
+
type: "object"
|
|
150
|
+
}
|
|
151
|
+
},
|
|
152
|
+
properties: {
|
|
153
|
+
errors: {
|
|
154
|
+
description: "A list of related errors",
|
|
155
|
+
items: {
|
|
156
|
+
$ref: "#/definitions/GenericError"
|
|
157
|
+
},
|
|
158
|
+
type: "array"
|
|
159
|
+
},
|
|
160
|
+
message: {
|
|
161
|
+
description: "An error message",
|
|
162
|
+
minLength: 1,
|
|
163
|
+
type: "string"
|
|
164
|
+
},
|
|
165
|
+
name: {
|
|
166
|
+
description: "The error name",
|
|
167
|
+
minLength: 1,
|
|
168
|
+
type: "string"
|
|
169
|
+
},
|
|
170
|
+
stack: {
|
|
171
|
+
description: "The stack trace (only in development mode)",
|
|
172
|
+
type: "string"
|
|
173
|
+
},
|
|
174
|
+
status: {
|
|
175
|
+
description: "The status code of the exception",
|
|
176
|
+
type: "number"
|
|
177
|
+
}
|
|
178
|
+
},
|
|
179
|
+
required: ["name", "message", "status"],
|
|
180
|
+
type: "object"
|
|
181
|
+
});
|
|
182
|
+
});
|
|
183
|
+
it("should declare a lazyRef", () => {
|
|
184
|
+
const schema1 = object({
|
|
185
|
+
owners: array().items(lazyRef(() => Owner))
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
class Post {
|
|
189
|
+
@Property()
|
|
190
|
+
id: string;
|
|
191
|
+
|
|
192
|
+
@Property(() => Owner)
|
|
193
|
+
owner: typeof Owner;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
class Owner {
|
|
197
|
+
@Property()
|
|
198
|
+
id: string;
|
|
199
|
+
|
|
200
|
+
@CollectionOf(() => Post)
|
|
201
|
+
posts: Post[];
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
expect(schema1.toJSON()).toEqual({
|
|
205
|
+
definitions: {
|
|
206
|
+
Owner: {
|
|
207
|
+
properties: {
|
|
208
|
+
id: {
|
|
209
|
+
type: "string"
|
|
210
|
+
},
|
|
211
|
+
posts: {
|
|
212
|
+
items: {
|
|
213
|
+
$ref: "#/definitions/Post"
|
|
214
|
+
},
|
|
215
|
+
type: "array"
|
|
216
|
+
}
|
|
217
|
+
},
|
|
218
|
+
type: "object"
|
|
219
|
+
},
|
|
220
|
+
Post: {
|
|
221
|
+
properties: {
|
|
222
|
+
id: {
|
|
223
|
+
type: "string"
|
|
224
|
+
},
|
|
225
|
+
owner: {
|
|
226
|
+
$ref: "#/definitions/Owner"
|
|
227
|
+
}
|
|
228
|
+
},
|
|
229
|
+
type: "object"
|
|
230
|
+
}
|
|
231
|
+
},
|
|
232
|
+
properties: {
|
|
233
|
+
owners: {
|
|
234
|
+
items: {
|
|
235
|
+
$ref: "#/definitions/Owner"
|
|
236
|
+
},
|
|
237
|
+
type: "array"
|
|
238
|
+
}
|
|
239
|
+
},
|
|
240
|
+
type: "object"
|
|
241
|
+
});
|
|
242
|
+
});
|
|
243
|
+
});
|
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
import {isClass, Type} from "@tsed/core";
|
|
2
|
+
|
|
3
|
+
import {JsonEntityStore} from "../domain/JsonEntityStore.js";
|
|
4
|
+
import {JsonFormatTypes} from "../domain/JsonFormatTypes.js";
|
|
5
|
+
import {JsonLazyRef} from "../domain/JsonLazyRef.js";
|
|
6
|
+
import {JsonSchema} from "../domain/JsonSchema.js";
|
|
7
|
+
import {enumsRegistry} from "../registries/enumRegistries.js";
|
|
8
|
+
import {getJsonEntityStore} from "./getJsonEntityStore.js";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Create a new model from the given type.
|
|
12
|
+
*
|
|
13
|
+
* ```typescript
|
|
14
|
+
* from(String)
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* * See @@JsonSchema@@ to discover available methods.
|
|
18
|
+
*
|
|
19
|
+
* @schemaFunctional
|
|
20
|
+
*/
|
|
21
|
+
export function from(type: Type<any> = Object) {
|
|
22
|
+
if (isClass(type)) {
|
|
23
|
+
const {schema} = getJsonEntityStore(type);
|
|
24
|
+
|
|
25
|
+
schema.properties = function properties(obj: {[key: string]: JsonSchema}) {
|
|
26
|
+
Object.entries(obj).forEach(([propertyKey, propSchema]) => {
|
|
27
|
+
getJsonEntityStore(type.prototype, propertyKey).schema.assign(propSchema);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
return this;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
return schema;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return JsonSchema.from({type});
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Declare a new number model.
|
|
41
|
+
*
|
|
42
|
+
* * See @@JsonSchema@@ to discover available methods.
|
|
43
|
+
*
|
|
44
|
+
* @schemaFunctional
|
|
45
|
+
*/
|
|
46
|
+
export function number() {
|
|
47
|
+
return from(Number);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Declare a new integer model.
|
|
52
|
+
*
|
|
53
|
+
* * See @@JsonSchema@@ to discover available methods.
|
|
54
|
+
*
|
|
55
|
+
* @schemaFunctional
|
|
56
|
+
*/
|
|
57
|
+
export function integer() {
|
|
58
|
+
return from(Number).integer();
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Declare a new string model.
|
|
63
|
+
*
|
|
64
|
+
* * See @@JsonSchema@@ to discover available methods.
|
|
65
|
+
*
|
|
66
|
+
* @schemaFunctional
|
|
67
|
+
*/
|
|
68
|
+
export function string() {
|
|
69
|
+
return from(String);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Declare a new boolean model.
|
|
74
|
+
*
|
|
75
|
+
* * See @@JsonSchema@@ to discover available methods.
|
|
76
|
+
*
|
|
77
|
+
* @schemaFunctional
|
|
78
|
+
*/
|
|
79
|
+
export function boolean() {
|
|
80
|
+
return from(Boolean);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Declare a new string model with `format: date`.
|
|
85
|
+
*
|
|
86
|
+
* * See @@JsonSchema@@ to discover available methods.
|
|
87
|
+
*
|
|
88
|
+
* @schemaFunctional
|
|
89
|
+
*/
|
|
90
|
+
export function date() {
|
|
91
|
+
return from(Date).format(JsonFormatTypes.DATE);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Declare a new string model with `format: datetime`.
|
|
96
|
+
*
|
|
97
|
+
* * See @@JsonSchema@@ to discover available methods.
|
|
98
|
+
*
|
|
99
|
+
* @schemaFunctional
|
|
100
|
+
*/
|
|
101
|
+
export function datetime() {
|
|
102
|
+
return from(Date).format(JsonFormatTypes.DATE_TIME);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Declare a new string model with `format: time`.
|
|
107
|
+
*
|
|
108
|
+
* * See @@JsonSchema@@ to discover available methods.
|
|
109
|
+
*
|
|
110
|
+
* @schemaFunctional
|
|
111
|
+
*/
|
|
112
|
+
export function time() {
|
|
113
|
+
return from(Date).format(JsonFormatTypes.TIME);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Declare a new string model with `format: email`.
|
|
118
|
+
*
|
|
119
|
+
* * See @@JsonSchema@@ to discover available methods.
|
|
120
|
+
*
|
|
121
|
+
* @schemaFunctional
|
|
122
|
+
*/
|
|
123
|
+
export function email() {
|
|
124
|
+
return from(String).format(JsonFormatTypes.EMAIL);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Declare a new string model with `format: uri`.
|
|
129
|
+
*
|
|
130
|
+
* * See @@JsonSchema@@ to discover available methods.
|
|
131
|
+
*
|
|
132
|
+
* @schemaFunctional
|
|
133
|
+
*/
|
|
134
|
+
export function uri() {
|
|
135
|
+
return from(String).format(JsonFormatTypes.URI);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Declare a new string model with `format: url`.
|
|
140
|
+
*
|
|
141
|
+
* * See @@JsonSchema@@ to discover available methods.
|
|
142
|
+
*
|
|
143
|
+
* @schemaFunctional
|
|
144
|
+
*/
|
|
145
|
+
export function url() {
|
|
146
|
+
return from(String).format(JsonFormatTypes.URL);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Declare a new object model with `additionalProperties: true`.
|
|
151
|
+
*
|
|
152
|
+
* ```json
|
|
153
|
+
* {
|
|
154
|
+
* "type": "array"
|
|
155
|
+
* }
|
|
156
|
+
* ```
|
|
157
|
+
*
|
|
158
|
+
* * See @@JsonSchema@@ to discover available methods.
|
|
159
|
+
*
|
|
160
|
+
* @schemaFunctional
|
|
161
|
+
*/
|
|
162
|
+
export function array() {
|
|
163
|
+
return from(Array);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Declare a new object model with `additionalProperties: true`.
|
|
168
|
+
*
|
|
169
|
+
* ```json
|
|
170
|
+
* {
|
|
171
|
+
* "type": "object",
|
|
172
|
+
* "additionalProperties": true
|
|
173
|
+
* }
|
|
174
|
+
* ```
|
|
175
|
+
*
|
|
176
|
+
* * See @@JsonSchema@@ to discover available methods.
|
|
177
|
+
*
|
|
178
|
+
* @schemaFunctional
|
|
179
|
+
*/
|
|
180
|
+
export function map() {
|
|
181
|
+
return from(Map).unknown(true);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Declare a new array model with `uniqueItems: true`.
|
|
186
|
+
*
|
|
187
|
+
* ```json
|
|
188
|
+
* {
|
|
189
|
+
* "type": "array",
|
|
190
|
+
* "uniqueItems": true
|
|
191
|
+
* }
|
|
192
|
+
* ```
|
|
193
|
+
*
|
|
194
|
+
* * See @@JsonSchema@@ to discover available methods.
|
|
195
|
+
*
|
|
196
|
+
* @schemaFunctional
|
|
197
|
+
*/
|
|
198
|
+
export function set() {
|
|
199
|
+
return from(Array).uniqueItems(true);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Declare a new object model.
|
|
204
|
+
*
|
|
205
|
+
* * See @@JsonSchema@@ to discover available methods.
|
|
206
|
+
*
|
|
207
|
+
* @schemaFunctional
|
|
208
|
+
*/
|
|
209
|
+
export function object(properties: {[key: string]: JsonSchema} = {}) {
|
|
210
|
+
return from(Object).properties(properties);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Declare a model with any type (By default: `integer`, `number`, `string`, `boolean`, `array`, `object`, `null`)
|
|
215
|
+
*
|
|
216
|
+
* * See @@JsonSchema@@ to discover available methods.
|
|
217
|
+
*
|
|
218
|
+
* @schemaFunctional
|
|
219
|
+
*/
|
|
220
|
+
export function any(...types: any[]) {
|
|
221
|
+
return from().any(...types);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* See https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.27
|
|
226
|
+
*
|
|
227
|
+
* * See @@JsonSchema@@ to discover available methods.
|
|
228
|
+
*
|
|
229
|
+
* @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.27
|
|
230
|
+
* @schemaFunctional
|
|
231
|
+
*/
|
|
232
|
+
export function anyOf(...anyOf: any[]) {
|
|
233
|
+
return from().anyOf(anyOf);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* See https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.28
|
|
238
|
+
*
|
|
239
|
+
* * See @@JsonSchema@@ to discover available methods.
|
|
240
|
+
*
|
|
241
|
+
* @schemaFunctional
|
|
242
|
+
* @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.28
|
|
243
|
+
*/
|
|
244
|
+
export function oneOf(...oneOf: any[]) {
|
|
245
|
+
return from().oneOf(oneOf);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* See https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.26
|
|
250
|
+
*
|
|
251
|
+
* * See @@JsonSchema@@ to discover available methods.
|
|
252
|
+
*
|
|
253
|
+
* @see https://tools.ietf.org/html/draft-wright-json-schema-validation-01#section-6.26
|
|
254
|
+
* @schemaFunctional
|
|
255
|
+
*/
|
|
256
|
+
export function allOf(...allOf: any[]) {
|
|
257
|
+
return from().allOf(allOf);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Declare a sub schema which will be resolved later. Use this function when you have a circular reference between two schemes.
|
|
262
|
+
*
|
|
263
|
+
* @schemaFunctional
|
|
264
|
+
*/
|
|
265
|
+
export function lazyRef(cb: () => Type<any>) {
|
|
266
|
+
try {
|
|
267
|
+
// solve issue with a self referenced model
|
|
268
|
+
if (cb()) {
|
|
269
|
+
// type is already accessible
|
|
270
|
+
return JsonEntityStore.from(cb()).schema as any;
|
|
271
|
+
}
|
|
272
|
+
} catch (er) {}
|
|
273
|
+
|
|
274
|
+
return new JsonLazyRef(cb);
|
|
275
|
+
}
|
|
276
|
+
export function enums(e: Record<string, any>) {
|
|
277
|
+
const schema = string().enum(e);
|
|
278
|
+
enumsRegistry.set(e, schema);
|
|
279
|
+
|
|
280
|
+
return schema;
|
|
281
|
+
}
|