@tsed/schema 8.0.0 → 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,57 @@
|
|
|
1
|
+
import {getJsonSchema} from "../../utils/getJsonSchema.js";
|
|
2
|
+
import {ErrorMsg} from "./errorMsg.js";
|
|
3
|
+
|
|
4
|
+
describe("@ErrorMsg", () => {
|
|
5
|
+
it("should declare error message", () => {
|
|
6
|
+
// WHEN
|
|
7
|
+
class Model {
|
|
8
|
+
@ErrorMsg({property: "foo should be a string"})
|
|
9
|
+
property: number;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// THEN
|
|
13
|
+
const schema = getJsonSchema(Model, {customKeys: true});
|
|
14
|
+
|
|
15
|
+
expect(schema).toEqual({
|
|
16
|
+
type: "object",
|
|
17
|
+
properties: {
|
|
18
|
+
property: {
|
|
19
|
+
type: "number"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
errorMessage: {
|
|
23
|
+
property: "foo should be a string"
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it("should declare multiple error messages", () => {
|
|
29
|
+
// WHEN
|
|
30
|
+
class Model {
|
|
31
|
+
@ErrorMsg({foo: "foo should be a string"})
|
|
32
|
+
foo: string;
|
|
33
|
+
|
|
34
|
+
@ErrorMsg({bar: "bar should be a number"})
|
|
35
|
+
bar: number;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// THEN
|
|
39
|
+
const schema = getJsonSchema(Model, {customKeys: true});
|
|
40
|
+
|
|
41
|
+
expect(schema).toEqual({
|
|
42
|
+
type: "object",
|
|
43
|
+
properties: {
|
|
44
|
+
foo: {
|
|
45
|
+
type: "string"
|
|
46
|
+
},
|
|
47
|
+
bar: {
|
|
48
|
+
type: "number"
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
errorMessage: {
|
|
52
|
+
foo: "foo should be a string",
|
|
53
|
+
bar: "bar should be a number"
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import {JsonEntityFn} from "./jsonEntityFn.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Add an error message to the property.
|
|
5
|
+
* @decorator
|
|
6
|
+
* @ajv
|
|
7
|
+
*/
|
|
8
|
+
export function ErrorMsg(obj: Record<string, string>) {
|
|
9
|
+
return JsonEntityFn((store) => {
|
|
10
|
+
// since errorMessage is a custom key, it is prefixed with a # to avoid conflict with JSON Schema keywords
|
|
11
|
+
const errorMessage = store.parentSchema.get("#errorMessage") || {};
|
|
12
|
+
store.parentSchema.customKey("errorMessage", {...errorMessage, ...obj});
|
|
13
|
+
});
|
|
14
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import {descriptorOf, useDecorators} from "@tsed/core";
|
|
2
|
+
|
|
3
|
+
import {Description, getSpec, In, Name, OperationPath, Path, Pattern, SpecTypes} from "../../index.js";
|
|
4
|
+
import {getJsonSchema} from "../../utils/getJsonSchema.js";
|
|
5
|
+
import {Example} from "./example.js";
|
|
6
|
+
|
|
7
|
+
function ObjectID(name?: string) {
|
|
8
|
+
return useDecorators(name && Name(name), Pattern(/^[0-9a-fA-F]{24}$/), Description("An ObjectID"), Example("5ce7ad3028890bd71749d477"));
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
describe("@Example", () => {
|
|
12
|
+
it("should declare description on class", () => {
|
|
13
|
+
// WHEN
|
|
14
|
+
@Example({id: "id"})
|
|
15
|
+
class Model {}
|
|
16
|
+
|
|
17
|
+
// THEN
|
|
18
|
+
expect(getJsonSchema(Model)).toEqual({
|
|
19
|
+
examples: [{id: "id"}],
|
|
20
|
+
type: "object"
|
|
21
|
+
});
|
|
22
|
+
});
|
|
23
|
+
it("should declare example on params", () => {
|
|
24
|
+
// WHEN
|
|
25
|
+
|
|
26
|
+
@Path("/")
|
|
27
|
+
class MyController {
|
|
28
|
+
@OperationPath("GET", "/:id")
|
|
29
|
+
getMethod(
|
|
30
|
+
@In("path")
|
|
31
|
+
@Name("id")
|
|
32
|
+
@ObjectID()
|
|
33
|
+
id: string
|
|
34
|
+
) {
|
|
35
|
+
return id;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// THEN
|
|
40
|
+
expect(getSpec(MyController)).toEqual({
|
|
41
|
+
paths: {
|
|
42
|
+
"/{id}": {
|
|
43
|
+
get: {
|
|
44
|
+
operationId: "myControllerGetMethod",
|
|
45
|
+
parameters: [
|
|
46
|
+
{
|
|
47
|
+
description: "An ObjectID",
|
|
48
|
+
in: "path",
|
|
49
|
+
name: "id",
|
|
50
|
+
required: true,
|
|
51
|
+
schema: {
|
|
52
|
+
example: "5ce7ad3028890bd71749d477",
|
|
53
|
+
pattern: "^[0-9a-fA-F]{24}$",
|
|
54
|
+
type: "string"
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
],
|
|
58
|
+
responses: {
|
|
59
|
+
"200": {
|
|
60
|
+
description: "Success"
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
tags: ["MyController"]
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
},
|
|
67
|
+
tags: [
|
|
68
|
+
{
|
|
69
|
+
name: "MyController"
|
|
70
|
+
}
|
|
71
|
+
]
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
it("should declare description on property", () => {
|
|
75
|
+
// WHEN
|
|
76
|
+
|
|
77
|
+
class Model {
|
|
78
|
+
@Example("Examples")
|
|
79
|
+
method: string;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// THEN
|
|
83
|
+
expect(getJsonSchema(Model)).toEqual({
|
|
84
|
+
properties: {
|
|
85
|
+
method: {
|
|
86
|
+
examples: ["Examples"],
|
|
87
|
+
type: "string"
|
|
88
|
+
}
|
|
89
|
+
},
|
|
90
|
+
type: "object"
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
});
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import {JsonEntityStore} from "../../domain/JsonEntityStore.js";
|
|
2
|
+
import {JsonEntityFn} from "./jsonEntityFn.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Add an example metadata on the decorated element.
|
|
6
|
+
*
|
|
7
|
+
* @decorator
|
|
8
|
+
* @swagger
|
|
9
|
+
* @schema
|
|
10
|
+
* @input
|
|
11
|
+
* @methodDecorator
|
|
12
|
+
* @classDecorator
|
|
13
|
+
*/
|
|
14
|
+
export function Example(...examples: any[]): Function {
|
|
15
|
+
return JsonEntityFn((store: JsonEntityStore) => {
|
|
16
|
+
store.schema.examples(examples);
|
|
17
|
+
});
|
|
18
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import {getSpec, In, OperationPath, Path, Property, SpecTypes} from "../../index.js";
|
|
2
|
+
import {Examples} from "./examples.js";
|
|
3
|
+
|
|
4
|
+
class MyModel {
|
|
5
|
+
@Property()
|
|
6
|
+
id: string;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
describe("@Examples", () => {
|
|
10
|
+
it("should declare example on body params", () => {
|
|
11
|
+
// WHEN
|
|
12
|
+
@Path("/")
|
|
13
|
+
class MyController {
|
|
14
|
+
@OperationPath("POST", "/:id")
|
|
15
|
+
getMethod(
|
|
16
|
+
@In("body")
|
|
17
|
+
@Examples({
|
|
18
|
+
Example1: {
|
|
19
|
+
description: "Example with hello1",
|
|
20
|
+
value: {test: "hello1"}
|
|
21
|
+
},
|
|
22
|
+
Example2: {
|
|
23
|
+
description: "Example with hello2",
|
|
24
|
+
value: {test: "hello2"}
|
|
25
|
+
}
|
|
26
|
+
})
|
|
27
|
+
model: MyModel
|
|
28
|
+
) {
|
|
29
|
+
return model;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// THEN
|
|
34
|
+
expect(getSpec(MyController, {specType: SpecTypes.OPENAPI})).toEqual({
|
|
35
|
+
components: {
|
|
36
|
+
schemas: {
|
|
37
|
+
MyModel: {
|
|
38
|
+
properties: {
|
|
39
|
+
id: {
|
|
40
|
+
type: "string"
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
type: "object"
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
paths: {
|
|
48
|
+
"/{id}": {
|
|
49
|
+
post: {
|
|
50
|
+
operationId: "myControllerGetMethod",
|
|
51
|
+
parameters: [
|
|
52
|
+
{
|
|
53
|
+
in: "path",
|
|
54
|
+
name: "id",
|
|
55
|
+
required: true,
|
|
56
|
+
schema: {
|
|
57
|
+
type: "string"
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
],
|
|
61
|
+
requestBody: {
|
|
62
|
+
content: {
|
|
63
|
+
"application/json": {
|
|
64
|
+
examples: {
|
|
65
|
+
Example1: {
|
|
66
|
+
description: "Example with hello1",
|
|
67
|
+
value: {
|
|
68
|
+
test: "hello1"
|
|
69
|
+
}
|
|
70
|
+
},
|
|
71
|
+
Example2: {
|
|
72
|
+
description: "Example with hello2",
|
|
73
|
+
value: {
|
|
74
|
+
test: "hello2"
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
},
|
|
78
|
+
schema: {
|
|
79
|
+
$ref: "#/components/schemas/MyModel"
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
},
|
|
83
|
+
required: false
|
|
84
|
+
},
|
|
85
|
+
responses: {
|
|
86
|
+
"200": {
|
|
87
|
+
description: "Success"
|
|
88
|
+
}
|
|
89
|
+
},
|
|
90
|
+
tags: ["MyController"]
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
},
|
|
94
|
+
tags: [
|
|
95
|
+
{
|
|
96
|
+
name: "MyController"
|
|
97
|
+
}
|
|
98
|
+
]
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
});
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import {OpenSpecHash, OpenSpecRef, OS3Example} from "@tsed/openspec";
|
|
2
|
+
|
|
3
|
+
import {JsonParameterStore} from "../../domain/JsonParameterStore.js";
|
|
4
|
+
import {JsonEntityFn} from "./jsonEntityFn.js";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Add a examples metadata on the decorated element.
|
|
8
|
+
*
|
|
9
|
+
* @decorator
|
|
10
|
+
* @swagger
|
|
11
|
+
* @schema
|
|
12
|
+
* @input
|
|
13
|
+
* @methodDecorator
|
|
14
|
+
* @classDecorator
|
|
15
|
+
*/
|
|
16
|
+
export function Examples(examples: OpenSpecHash<OS3Example | OpenSpecRef>): ParameterDecorator {
|
|
17
|
+
return JsonEntityFn((store: JsonParameterStore) => {
|
|
18
|
+
store.parameter!.examples(examples);
|
|
19
|
+
});
|
|
20
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import "../../index.js";
|
|
2
|
+
|
|
3
|
+
import {JsonEntityStore} from "../../domain/JsonEntityStore.js";
|
|
4
|
+
import {ExclusiveMaximum} from "./exclusiveMaximum.js";
|
|
5
|
+
|
|
6
|
+
describe("@ExclusiveMaximum", () => {
|
|
7
|
+
it("should declare exclusiveMaximum value", () => {
|
|
8
|
+
// WHEN
|
|
9
|
+
class Model {
|
|
10
|
+
@ExclusiveMaximum(0, true)
|
|
11
|
+
num: number;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// THEN
|
|
15
|
+
const classSchema = JsonEntityStore.from(Model);
|
|
16
|
+
|
|
17
|
+
expect(classSchema.schema.toJSON()).toEqual({
|
|
18
|
+
properties: {
|
|
19
|
+
num: {
|
|
20
|
+
exclusiveMaximum: 0,
|
|
21
|
+
type: "number"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
type: "object"
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
|
+
it("should declare exclusiveMaximum value (default value)", () => {
|
|
28
|
+
// WHEN
|
|
29
|
+
class Model {
|
|
30
|
+
@ExclusiveMaximum(0)
|
|
31
|
+
num: number;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// THEN
|
|
35
|
+
const classSchema = JsonEntityStore.from(Model);
|
|
36
|
+
|
|
37
|
+
expect(classSchema.schema.toJSON()).toEqual({
|
|
38
|
+
properties: {
|
|
39
|
+
num: {
|
|
40
|
+
exclusiveMaximum: 0,
|
|
41
|
+
type: "number"
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
type: "object"
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
});
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import {withErrorMsg} from "../../utils/withErrorMsg.js";
|
|
2
|
+
import {Maximum} from "./maximum.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The value of `exclusiveMaximum` MUST be number, representing an exclusive upper limit for a numeric instance.
|
|
6
|
+
*
|
|
7
|
+
* If the instance is a number, then the instance is valid only if it has a value strictly less than (not equal to) `exclusiveMaximum`.
|
|
8
|
+
*
|
|
9
|
+
* ::: warning
|
|
10
|
+
* For v6 user, use @@ExclusiveMaximum@@ from @tsed/schema instead of @tsed/platform-http.
|
|
11
|
+
* :::
|
|
12
|
+
*
|
|
13
|
+
* ## Example
|
|
14
|
+
* ### With primitive type
|
|
15
|
+
*
|
|
16
|
+
* ```typescript
|
|
17
|
+
* class Model {
|
|
18
|
+
* @ExclusiveMaximum(10)
|
|
19
|
+
* property: number;
|
|
20
|
+
* }
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* Will produce:
|
|
24
|
+
*
|
|
25
|
+
* ```json
|
|
26
|
+
* {
|
|
27
|
+
* "type": "object",
|
|
28
|
+
* "properties": {
|
|
29
|
+
* "property": {
|
|
30
|
+
* "type": "number",
|
|
31
|
+
* "exclusiveMaximum": 10
|
|
32
|
+
* }
|
|
33
|
+
* }
|
|
34
|
+
* }
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
* ### With array type
|
|
38
|
+
*
|
|
39
|
+
* ```typescript
|
|
40
|
+
* class Model {
|
|
41
|
+
* @ExclusiveMaximum(10)
|
|
42
|
+
* @CollectionOf(Number)
|
|
43
|
+
* property: number[];
|
|
44
|
+
* }
|
|
45
|
+
* ```
|
|
46
|
+
*
|
|
47
|
+
* Will produce:
|
|
48
|
+
*
|
|
49
|
+
* ```json
|
|
50
|
+
* {
|
|
51
|
+
* "type": "object",
|
|
52
|
+
* "properties": {
|
|
53
|
+
* "property": {
|
|
54
|
+
* "type": "array",
|
|
55
|
+
* "items": {
|
|
56
|
+
* "type": "number",
|
|
57
|
+
* "exclusiveMaximum": 10
|
|
58
|
+
* }
|
|
59
|
+
* }
|
|
60
|
+
* }
|
|
61
|
+
* }
|
|
62
|
+
* ```
|
|
63
|
+
*
|
|
64
|
+
* @param maximum
|
|
65
|
+
* @param {boolean} exclusiveMaximum
|
|
66
|
+
* @decorator
|
|
67
|
+
* @validation
|
|
68
|
+
* @swagger
|
|
69
|
+
* @schema
|
|
70
|
+
* @input
|
|
71
|
+
* @ajv-errors
|
|
72
|
+
*/
|
|
73
|
+
export const ExclusiveMaximum = withErrorMsg("exclusiveMaximum", (maximum: number, exclusiveMaximum: boolean = true) => {
|
|
74
|
+
return Maximum(maximum, exclusiveMaximum);
|
|
75
|
+
});
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import "../../index.js";
|
|
2
|
+
|
|
3
|
+
import {JsonEntityStore} from "../../domain/JsonEntityStore.js";
|
|
4
|
+
import {ExclusiveMinimum} from "./exclusiveMinimum.js";
|
|
5
|
+
|
|
6
|
+
describe("@ExclusiveMinimum", () => {
|
|
7
|
+
it("should declare exclusiveMinimum value", () => {
|
|
8
|
+
// WHEN
|
|
9
|
+
class Model {
|
|
10
|
+
@ExclusiveMinimum(0, true)
|
|
11
|
+
num: number;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// THEN
|
|
15
|
+
const classSchema = JsonEntityStore.from(Model);
|
|
16
|
+
|
|
17
|
+
expect(classSchema.schema.toJSON()).toEqual({
|
|
18
|
+
properties: {
|
|
19
|
+
num: {
|
|
20
|
+
exclusiveMinimum: 0,
|
|
21
|
+
type: "number"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
type: "object"
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
|
+
it("should declare exclusiveMinimum value (default value)", () => {
|
|
28
|
+
// WHEN
|
|
29
|
+
class Model {
|
|
30
|
+
@ExclusiveMinimum(0)
|
|
31
|
+
num: number;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// THEN
|
|
35
|
+
const classSchema = JsonEntityStore.from(Model);
|
|
36
|
+
|
|
37
|
+
expect(classSchema.schema.toJSON()).toEqual({
|
|
38
|
+
properties: {
|
|
39
|
+
num: {
|
|
40
|
+
exclusiveMinimum: 0,
|
|
41
|
+
type: "number"
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
type: "object"
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
});
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import {withErrorMsg} from "../../utils/withErrorMsg.js";
|
|
2
|
+
import {Minimum} from "./minimum.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The value of `exclusiveMinimum` MUST be number, representing an exclusive upper limit for a numeric instance.
|
|
6
|
+
*
|
|
7
|
+
* If the instance is a number, then the instance is valid only if it has a value strictly greater than (not equal to) `exclusiveMinimum`.
|
|
8
|
+
*
|
|
9
|
+
* ::: warning
|
|
10
|
+
* For v6 user, use @@ExclusiveMinimum@@ from @tsed/schema instead of @tsed/platform-http.
|
|
11
|
+
* :::
|
|
12
|
+
*
|
|
13
|
+
* ## Example
|
|
14
|
+
* ### With primitive type
|
|
15
|
+
*
|
|
16
|
+
* ```typescript
|
|
17
|
+
* class Model {
|
|
18
|
+
* @ExclusiveMinimum(10)
|
|
19
|
+
* property: number;
|
|
20
|
+
* }
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* Will produce:
|
|
24
|
+
*
|
|
25
|
+
* ```json
|
|
26
|
+
* {
|
|
27
|
+
* "type": "object",
|
|
28
|
+
* "properties": {
|
|
29
|
+
* "property": {
|
|
30
|
+
* "type": "number",
|
|
31
|
+
* "exclusiveMinimum": 10
|
|
32
|
+
* }
|
|
33
|
+
* }
|
|
34
|
+
* }
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
* ### With array type
|
|
38
|
+
*
|
|
39
|
+
* ```typescript
|
|
40
|
+
* class Model {
|
|
41
|
+
* @ExclusiveMinimum(10)
|
|
42
|
+
* @CollectionOf(Number)
|
|
43
|
+
* property: number[];
|
|
44
|
+
* }
|
|
45
|
+
* ```
|
|
46
|
+
*
|
|
47
|
+
* Will produce:
|
|
48
|
+
*
|
|
49
|
+
* ```json
|
|
50
|
+
* {
|
|
51
|
+
* "type": "object",
|
|
52
|
+
* "properties": {
|
|
53
|
+
* "property": {
|
|
54
|
+
* "type": "array",
|
|
55
|
+
* "items": {
|
|
56
|
+
* "type": "number",
|
|
57
|
+
* "exclusiveMinimum": 10
|
|
58
|
+
* }
|
|
59
|
+
* }
|
|
60
|
+
* }
|
|
61
|
+
* }
|
|
62
|
+
* ```
|
|
63
|
+
*
|
|
64
|
+
* @param minimum
|
|
65
|
+
* @param {boolean} exclusiveMinimum
|
|
66
|
+
* @decorator
|
|
67
|
+
* @validation
|
|
68
|
+
* @swagger
|
|
69
|
+
* @schema
|
|
70
|
+
* @input
|
|
71
|
+
* @ajv-errors
|
|
72
|
+
*/
|
|
73
|
+
export const ExclusiveMinimum = withErrorMsg("exclusiveMinimum", (minimum: number, exclusiveMinimum: boolean = true) => {
|
|
74
|
+
return Minimum(minimum, exclusiveMinimum);
|
|
75
|
+
});
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import {getJsonSchema} from "../../utils/getJsonSchema.js";
|
|
2
|
+
import {DateFormat, DateTime, Email, Format, TimeFormat, Uri, Url} from "./format.js";
|
|
3
|
+
|
|
4
|
+
describe("@Format", () => {
|
|
5
|
+
it("should declare prop", () => {
|
|
6
|
+
// WHEN
|
|
7
|
+
class Model {
|
|
8
|
+
@Format("email")
|
|
9
|
+
email: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// THEN
|
|
13
|
+
expect(getJsonSchema(Model)).toEqual({
|
|
14
|
+
properties: {
|
|
15
|
+
email: {
|
|
16
|
+
format: "email",
|
|
17
|
+
type: "string"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
type: "object"
|
|
21
|
+
});
|
|
22
|
+
});
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
describe("@Email", () => {
|
|
26
|
+
it("should declare prop", () => {
|
|
27
|
+
// WHEN
|
|
28
|
+
class Model {
|
|
29
|
+
@Email()
|
|
30
|
+
email: string;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// THEN
|
|
34
|
+
expect(getJsonSchema(Model)).toEqual({
|
|
35
|
+
properties: {
|
|
36
|
+
email: {
|
|
37
|
+
format: "email",
|
|
38
|
+
type: "string"
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
type: "object"
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
describe("@DateFormat", () => {
|
|
47
|
+
it("should declare prop", () => {
|
|
48
|
+
// WHEN
|
|
49
|
+
class Model {
|
|
50
|
+
@DateFormat()
|
|
51
|
+
prop: string;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// THEN
|
|
55
|
+
expect(getJsonSchema(Model)).toEqual({
|
|
56
|
+
properties: {
|
|
57
|
+
prop: {
|
|
58
|
+
format: "date",
|
|
59
|
+
type: "string"
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
type: "object"
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
describe("@TimeFormat", () => {
|
|
68
|
+
it("should declare prop", () => {
|
|
69
|
+
// WHEN
|
|
70
|
+
class Model {
|
|
71
|
+
@TimeFormat()
|
|
72
|
+
prop: string;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// THEN
|
|
76
|
+
expect(getJsonSchema(Model)).toEqual({
|
|
77
|
+
properties: {
|
|
78
|
+
prop: {
|
|
79
|
+
format: "time",
|
|
80
|
+
type: "string"
|
|
81
|
+
}
|
|
82
|
+
},
|
|
83
|
+
type: "object"
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
describe("@DateTime", () => {
|
|
89
|
+
it("should declare prop", () => {
|
|
90
|
+
// WHEN
|
|
91
|
+
class Model {
|
|
92
|
+
@DateTime()
|
|
93
|
+
prop: string;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// THEN
|
|
97
|
+
expect(getJsonSchema(Model)).toEqual({
|
|
98
|
+
properties: {
|
|
99
|
+
prop: {
|
|
100
|
+
format: "date-time",
|
|
101
|
+
type: "string"
|
|
102
|
+
}
|
|
103
|
+
},
|
|
104
|
+
type: "object"
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
describe("@Uri", () => {
|
|
110
|
+
it("should declare prop", () => {
|
|
111
|
+
// WHEN
|
|
112
|
+
class Model {
|
|
113
|
+
@Uri()
|
|
114
|
+
prop: string;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// THEN
|
|
118
|
+
expect(getJsonSchema(Model)).toEqual({
|
|
119
|
+
properties: {
|
|
120
|
+
prop: {
|
|
121
|
+
format: "uri",
|
|
122
|
+
type: "string"
|
|
123
|
+
}
|
|
124
|
+
},
|
|
125
|
+
type: "object"
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
describe("@Url", () => {
|
|
131
|
+
it("should declare prop", () => {
|
|
132
|
+
// WHEN
|
|
133
|
+
class Model {
|
|
134
|
+
@Url()
|
|
135
|
+
prop: string;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// THEN
|
|
139
|
+
expect(getJsonSchema(Model)).toEqual({
|
|
140
|
+
properties: {
|
|
141
|
+
prop: {
|
|
142
|
+
format: "url",
|
|
143
|
+
type: "string"
|
|
144
|
+
}
|
|
145
|
+
},
|
|
146
|
+
type: "object"
|
|
147
|
+
});
|
|
148
|
+
});
|
|
149
|
+
});
|