@tsed/schema 8.0.1 → 8.0.3
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/readme.md +6 -6
- 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,277 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Allow,
|
|
3
|
+
In,
|
|
4
|
+
JsonEntityStore,
|
|
5
|
+
JsonMethodStore,
|
|
6
|
+
JsonOperation,
|
|
7
|
+
JsonParameter,
|
|
8
|
+
JsonParameterStore,
|
|
9
|
+
JsonPropertyStore,
|
|
10
|
+
Property,
|
|
11
|
+
Required
|
|
12
|
+
} from "../index.js";
|
|
13
|
+
|
|
14
|
+
describe("JsonEntityStore", () => {
|
|
15
|
+
it("should create JsonEntityStore", () => {
|
|
16
|
+
class Model {
|
|
17
|
+
@Property()
|
|
18
|
+
id: string;
|
|
19
|
+
|
|
20
|
+
method(@In("path") param: string) {}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// CLASS
|
|
24
|
+
const storeClass = JsonEntityStore.from(Model);
|
|
25
|
+
|
|
26
|
+
expect(storeClass).toBeInstanceOf(JsonEntityStore);
|
|
27
|
+
expect(storeClass.decoratorType).toBe("class");
|
|
28
|
+
expect(storeClass.propertyName).toBe("undefined");
|
|
29
|
+
expect(storeClass.propertyKey).toBeUndefined();
|
|
30
|
+
expect(storeClass.index).toBeUndefined();
|
|
31
|
+
expect(storeClass.parent).toBe(storeClass);
|
|
32
|
+
expect(storeClass.type).toEqual(Model);
|
|
33
|
+
expect(storeClass.target).toEqual(Model);
|
|
34
|
+
expect(storeClass.token).toEqual(Model);
|
|
35
|
+
expect(storeClass.isCollection).toBe(false);
|
|
36
|
+
expect(storeClass.isDate).toBe(false);
|
|
37
|
+
expect(storeClass.isPrimitive).toBe(false);
|
|
38
|
+
expect(storeClass.isObject).toBe(false);
|
|
39
|
+
expect(storeClass.isClass).toBe(true);
|
|
40
|
+
expect(storeClass.isDiscriminatorChild).toBe(false);
|
|
41
|
+
expect(storeClass.getBestType()).toBe(Model);
|
|
42
|
+
|
|
43
|
+
// PROPERTY
|
|
44
|
+
const storeProp = JsonEntityStore.from(Model).children.get("id") as JsonPropertyStore;
|
|
45
|
+
expect(storeProp).toBeInstanceOf(JsonEntityStore);
|
|
46
|
+
expect(storeProp.decoratorType).toBe("property");
|
|
47
|
+
expect(storeProp.propertyKey).toBe("id");
|
|
48
|
+
expect(storeProp.propertyName).toBe("id");
|
|
49
|
+
expect(storeProp.index).toBeUndefined();
|
|
50
|
+
expect(storeProp.nestedGenerics).toEqual([]);
|
|
51
|
+
expect(storeProp.parent).toEqual(storeClass);
|
|
52
|
+
expect(storeProp.type).toEqual(String);
|
|
53
|
+
expect(storeProp.target).toEqual(Model);
|
|
54
|
+
expect(storeProp.token).toEqual(Model);
|
|
55
|
+
expect(storeProp.isCollection).toBe(false);
|
|
56
|
+
expect(storeProp.isDate).toBe(false);
|
|
57
|
+
expect(storeProp.isPrimitive).toBe(true);
|
|
58
|
+
expect(storeProp.isObject).toBe(false);
|
|
59
|
+
expect(storeProp.isClass).toBe(false);
|
|
60
|
+
expect(storeProp.isGetterOnly()).toBeFalsy();
|
|
61
|
+
|
|
62
|
+
// METHOD
|
|
63
|
+
const storeMethod = JsonEntityStore.from(Model).children.get("method") as JsonMethodStore;
|
|
64
|
+
expect(storeMethod).toBeInstanceOf(JsonEntityStore);
|
|
65
|
+
expect(storeMethod.propertyKey).toBe("method");
|
|
66
|
+
expect(storeMethod.propertyName).toBe("method");
|
|
67
|
+
expect(storeMethod.decoratorType).toBe("method");
|
|
68
|
+
expect(storeMethod.index).toBeUndefined();
|
|
69
|
+
expect(storeMethod.operation).toBeInstanceOf(JsonOperation);
|
|
70
|
+
expect(storeMethod.nestedGenerics).toEqual([]);
|
|
71
|
+
expect(storeMethod.parent).toEqual(storeClass);
|
|
72
|
+
expect(storeMethod.target).toEqual(Model);
|
|
73
|
+
expect(storeMethod.type).toEqual(Object);
|
|
74
|
+
expect(storeMethod.target).toEqual(Model);
|
|
75
|
+
expect(storeMethod.token).toEqual(Model);
|
|
76
|
+
expect(storeMethod.isCollection).toBe(false);
|
|
77
|
+
expect(storeMethod.isDate).toBe(false);
|
|
78
|
+
expect(storeMethod.isPrimitive).toBe(false);
|
|
79
|
+
expect(storeMethod.isObject).toBe(false);
|
|
80
|
+
expect(storeMethod.isClass).toBe(false);
|
|
81
|
+
|
|
82
|
+
// PARAMETERS
|
|
83
|
+
const storeParam = JsonEntityStore.from(Model).children.get("method")?.children.get(0) as JsonParameterStore;
|
|
84
|
+
expect(storeParam).toBeInstanceOf(JsonEntityStore);
|
|
85
|
+
expect(storeParam.propertyKey).toBe("method");
|
|
86
|
+
expect(storeParam.propertyName).toBe("method");
|
|
87
|
+
expect(storeParam.index).toBe(0);
|
|
88
|
+
expect(storeParam.decoratorType).toBe("parameter");
|
|
89
|
+
expect(storeParam.parameter).toBeInstanceOf(JsonParameter);
|
|
90
|
+
expect(storeParam.nestedGenerics).toEqual([]);
|
|
91
|
+
expect(storeParam.parent).toEqual(storeMethod);
|
|
92
|
+
expect(storeParam.type).toEqual(String);
|
|
93
|
+
expect(storeParam.target).toEqual(Model);
|
|
94
|
+
expect(storeParam.token).toEqual(Model);
|
|
95
|
+
expect(storeParam.isCollection).toBe(false);
|
|
96
|
+
expect(storeParam.isDate).toBe(false);
|
|
97
|
+
expect(storeParam.isPrimitive).toBe(true);
|
|
98
|
+
expect(storeParam.isObject).toBe(false);
|
|
99
|
+
expect(storeParam.isClass).toBe(false);
|
|
100
|
+
expect(storeParam.isGetterOnly()).toBe(false);
|
|
101
|
+
expect(storeParam.toString()).toBe("Model:method:0");
|
|
102
|
+
});
|
|
103
|
+
it("should manage enum from babel", () => {
|
|
104
|
+
enum MyEnum {
|
|
105
|
+
test = "test"
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
class Model {
|
|
109
|
+
test: MyEnum;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
Reflect.defineMetadata("design:type", MyEnum, Model.prototype, "test");
|
|
113
|
+
|
|
114
|
+
// CLASS
|
|
115
|
+
Property()(Model.prototype, "test");
|
|
116
|
+
const store = JsonEntityStore.from(Model, "test");
|
|
117
|
+
|
|
118
|
+
expect(store.type).toEqual(String);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
describe("isGetterOnly()", () => {
|
|
122
|
+
it("should create JsonEntityStore on getter", () => {
|
|
123
|
+
class Model {
|
|
124
|
+
@Property()
|
|
125
|
+
get id() {
|
|
126
|
+
return "id";
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// CLASS
|
|
131
|
+
// PROPERTY
|
|
132
|
+
const storeProp = JsonEntityStore.from(Model).children.get("id") as JsonPropertyStore;
|
|
133
|
+
expect(storeProp.isGetterOnly()).toBeTruthy();
|
|
134
|
+
});
|
|
135
|
+
it("should create JsonEntityStore on getter/setter", () => {
|
|
136
|
+
class Model {
|
|
137
|
+
@Property()
|
|
138
|
+
get id() {
|
|
139
|
+
return "id";
|
|
140
|
+
}
|
|
141
|
+
set id(id: string) {}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// CLASS
|
|
145
|
+
// PROPERTY
|
|
146
|
+
const storeProp = JsonEntityStore.from(Model).children.get("id") as JsonPropertyStore;
|
|
147
|
+
expect(storeProp.isGetterOnly()).toBeFalsy();
|
|
148
|
+
});
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
describe("required() and allowRequiredValues", () => {
|
|
152
|
+
it("should return the required value", () => {
|
|
153
|
+
class Test {
|
|
154
|
+
@Required(true)
|
|
155
|
+
@Allow(null, "")
|
|
156
|
+
test: string;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const propertyMetadata = JsonEntityStore.get(Test, "test");
|
|
160
|
+
propertyMetadata.required = true;
|
|
161
|
+
propertyMetadata.type = Test;
|
|
162
|
+
|
|
163
|
+
expect(propertyMetadata.required).toEqual(true);
|
|
164
|
+
|
|
165
|
+
expect(propertyMetadata.collectionType).toEqual(undefined);
|
|
166
|
+
expect(propertyMetadata.type).toEqual(Test);
|
|
167
|
+
expect(propertyMetadata.isCollection).toEqual(false);
|
|
168
|
+
});
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
describe("isRequired", () => {
|
|
172
|
+
describe("when property is required", () => {
|
|
173
|
+
class Test {
|
|
174
|
+
@Required(true)
|
|
175
|
+
test: string;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
let propertyMetadata: JsonEntityStore;
|
|
179
|
+
|
|
180
|
+
beforeAll(() => {
|
|
181
|
+
propertyMetadata = JsonEntityStore.get(Test, "test");
|
|
182
|
+
propertyMetadata.required = true;
|
|
183
|
+
});
|
|
184
|
+
it("should return false (value 0)", () => {
|
|
185
|
+
expect(propertyMetadata.isRequired(0)).toEqual(false);
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
it("should return true (value '')", () => {
|
|
189
|
+
expect(propertyMetadata.isRequired("")).toEqual(true);
|
|
190
|
+
});
|
|
191
|
+
it("should return true (value null)", () => {
|
|
192
|
+
expect(propertyMetadata.isRequired(null)).toEqual(true);
|
|
193
|
+
});
|
|
194
|
+
it("should return true (value undefined)", () => {
|
|
195
|
+
expect(propertyMetadata.isRequired(undefined)).toEqual(true);
|
|
196
|
+
});
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
describe("when property is required and have allowed values", () => {
|
|
200
|
+
it("should validate the required values", () => {
|
|
201
|
+
class Test {
|
|
202
|
+
@Required()
|
|
203
|
+
@Allow(null)
|
|
204
|
+
test: string;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
let propertyMetadata: JsonEntityStore;
|
|
208
|
+
propertyMetadata = JsonEntityStore.get(Test, "test");
|
|
209
|
+
|
|
210
|
+
expect(propertyMetadata.allowedRequiredValues).toEqual([null]);
|
|
211
|
+
expect(propertyMetadata.isRequired(0)).toEqual(false);
|
|
212
|
+
expect(propertyMetadata.isRequired("")).toEqual(true);
|
|
213
|
+
expect(propertyMetadata.isRequired(null)).toEqual(false);
|
|
214
|
+
expect(propertyMetadata.isRequired(undefined)).toEqual(true);
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
it("should validate the required values (2)", () => {
|
|
218
|
+
class Test {
|
|
219
|
+
@Allow("")
|
|
220
|
+
test: string;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
let propertyMetadata: JsonEntityStore;
|
|
224
|
+
propertyMetadata = JsonEntityStore.get(Test, "test");
|
|
225
|
+
|
|
226
|
+
expect(propertyMetadata.allowedRequiredValues).toEqual([""]);
|
|
227
|
+
expect(propertyMetadata.isRequired(0)).toEqual(false);
|
|
228
|
+
expect(propertyMetadata.isRequired("")).toEqual(false);
|
|
229
|
+
expect(propertyMetadata.isRequired(null)).toEqual(true);
|
|
230
|
+
expect(propertyMetadata.isRequired(undefined)).toEqual(true);
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
it("should validate the required values (3)", () => {
|
|
234
|
+
class Test {
|
|
235
|
+
@Allow("")
|
|
236
|
+
test: string;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
let propertyMetadata: JsonEntityStore;
|
|
240
|
+
propertyMetadata = JsonEntityStore.get(Test, "test");
|
|
241
|
+
|
|
242
|
+
expect(propertyMetadata.allowedRequiredValues).toEqual([""]);
|
|
243
|
+
expect(propertyMetadata.isRequired(0)).toEqual(false);
|
|
244
|
+
expect(propertyMetadata.isRequired("")).toEqual(false);
|
|
245
|
+
expect(propertyMetadata.isRequired(null)).toEqual(true);
|
|
246
|
+
expect(propertyMetadata.isRequired(undefined)).toEqual(true);
|
|
247
|
+
});
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
describe("when property is not required", () => {
|
|
251
|
+
it("should validate values", () => {
|
|
252
|
+
class Test {
|
|
253
|
+
@Required(false)
|
|
254
|
+
test: string;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
const propertyMetadata = JsonEntityStore.get(Test, "test");
|
|
258
|
+
propertyMetadata.required = false;
|
|
259
|
+
expect(propertyMetadata.isRequired(0)).toEqual(false);
|
|
260
|
+
expect(propertyMetadata.isRequired("")).toEqual(false);
|
|
261
|
+
expect(propertyMetadata.isRequired(null)).toEqual(false);
|
|
262
|
+
expect(propertyMetadata.isRequired(undefined)).toEqual(false);
|
|
263
|
+
});
|
|
264
|
+
});
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
describe("get()", () => {
|
|
268
|
+
class Test {
|
|
269
|
+
test: string;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
it("should return the propertyMetadata", () => {
|
|
273
|
+
const propertyMetadata = JsonEntityStore.get(Test, "test");
|
|
274
|
+
expect(propertyMetadata).toBeInstanceOf(JsonEntityStore);
|
|
275
|
+
});
|
|
276
|
+
});
|
|
277
|
+
});
|
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ancestorsOf,
|
|
3
|
+
classOf,
|
|
4
|
+
decoratorTypeOf,
|
|
5
|
+
DecoratorTypes,
|
|
6
|
+
descriptorOf,
|
|
7
|
+
isArrayOrArrayClass,
|
|
8
|
+
isArrowFn,
|
|
9
|
+
isClass,
|
|
10
|
+
isClassObject,
|
|
11
|
+
isCollection,
|
|
12
|
+
isDate,
|
|
13
|
+
isObject,
|
|
14
|
+
isPlainObject,
|
|
15
|
+
isPrimitiveOrPrimitiveClass,
|
|
16
|
+
nameOf,
|
|
17
|
+
prototypeOf,
|
|
18
|
+
Store,
|
|
19
|
+
Type
|
|
20
|
+
} from "@tsed/core";
|
|
21
|
+
|
|
22
|
+
import type {JsonClassStore} from "./JsonClassStore.js";
|
|
23
|
+
import type {JsonMethodStore} from "./JsonMethodStore.js";
|
|
24
|
+
import type {JsonParameterStore} from "./JsonParameterStore.js";
|
|
25
|
+
import type {JsonPropertyStore} from "./JsonPropertyStore.js";
|
|
26
|
+
import type {JsonSchema} from "./JsonSchema.js";
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* @ignore
|
|
30
|
+
*/
|
|
31
|
+
export const JsonEntitiesContainer = new Map<DecoratorTypes, Type<JsonEntityStore>>();
|
|
32
|
+
|
|
33
|
+
export interface JsonEntityStoreOptions {
|
|
34
|
+
decoratorType: DecoratorTypes;
|
|
35
|
+
target: Type<any>;
|
|
36
|
+
propertyKey?: string | symbol;
|
|
37
|
+
index?: number;
|
|
38
|
+
descriptor?: any;
|
|
39
|
+
type?: Type<any>;
|
|
40
|
+
collectionType?: Type<any>;
|
|
41
|
+
beforeMiddlewares?: Function[];
|
|
42
|
+
middlewares?: Function[];
|
|
43
|
+
afterMiddlewares?: Function[];
|
|
44
|
+
|
|
45
|
+
[key: string]: any;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export abstract class JsonEntityStore implements JsonEntityStoreOptions {
|
|
49
|
+
/**
|
|
50
|
+
* Original property key decorated by the decorator
|
|
51
|
+
*/
|
|
52
|
+
readonly propertyKey: string | symbol;
|
|
53
|
+
/**
|
|
54
|
+
* Alias of the property
|
|
55
|
+
*/
|
|
56
|
+
readonly propertyName: string;
|
|
57
|
+
/**
|
|
58
|
+
* Parameter index
|
|
59
|
+
*/
|
|
60
|
+
readonly index: number;
|
|
61
|
+
/**
|
|
62
|
+
* Method's descriptor
|
|
63
|
+
*/
|
|
64
|
+
readonly descriptor: PropertyDescriptor;
|
|
65
|
+
/**
|
|
66
|
+
* Decorator type used to declare the JsonSchemaStore.
|
|
67
|
+
*/
|
|
68
|
+
readonly decoratorType: DecoratorTypes;
|
|
69
|
+
/**
|
|
70
|
+
* Type of the collection (Array, Map, Set, etc...)
|
|
71
|
+
*/
|
|
72
|
+
public collectionType: Type<any>;
|
|
73
|
+
public token: Type<any>;
|
|
74
|
+
readonly store: Store;
|
|
75
|
+
readonly isStore = true;
|
|
76
|
+
readonly parent: JsonEntityStore;
|
|
77
|
+
readonly target: Type<any>;
|
|
78
|
+
/**
|
|
79
|
+
*
|
|
80
|
+
*/
|
|
81
|
+
protected _type: Type<any>;
|
|
82
|
+
/**
|
|
83
|
+
* Ref to JsonSchema
|
|
84
|
+
*/
|
|
85
|
+
protected _schema: JsonSchema;
|
|
86
|
+
|
|
87
|
+
[key: string]: any;
|
|
88
|
+
|
|
89
|
+
constructor(options: JsonEntityStoreOptions) {
|
|
90
|
+
const {target, propertyKey, descriptor, index, decoratorType} = options;
|
|
91
|
+
this.target = target;
|
|
92
|
+
this.propertyKey = propertyKey!;
|
|
93
|
+
this.propertyName = String(propertyKey);
|
|
94
|
+
this.descriptor = descriptor;
|
|
95
|
+
this.index = index!;
|
|
96
|
+
this.decoratorType = decoratorType;
|
|
97
|
+
this.token = target;
|
|
98
|
+
this.store = options.store;
|
|
99
|
+
this.parent = this;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Return the class name of the entity.
|
|
104
|
+
* @returns {string}
|
|
105
|
+
*/
|
|
106
|
+
get targetName(): string {
|
|
107
|
+
return nameOf(this.token);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
get isCollection(): boolean {
|
|
111
|
+
return !!this.collectionType;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
get isArray() {
|
|
115
|
+
return isArrayOrArrayClass(this.collectionType);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
get discriminatorAncestor(): JsonEntityStore | undefined {
|
|
119
|
+
const ancestors = ancestorsOf(this.target);
|
|
120
|
+
const ancestor = ancestors.find((ancestor) => JsonEntityStore.from(ancestor).schema.isDiscriminator);
|
|
121
|
+
return ancestor && JsonEntityStore.from(ancestor);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
get isPrimitive() {
|
|
125
|
+
return isPrimitiveOrPrimitiveClass(this._type);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
get isDate() {
|
|
129
|
+
return isDate(this.computedType);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
get isObject() {
|
|
133
|
+
return isObject(this.computedType);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
get isClass() {
|
|
137
|
+
return isClass(this.computedType);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Return the JsonSchema
|
|
142
|
+
*/
|
|
143
|
+
get schema(): JsonSchema {
|
|
144
|
+
return this._schema;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
get nestedGenerics(): Type<any>[][] {
|
|
148
|
+
return this.schema.nestedGenerics;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
set nestedGenerics(nestedGenerics: Type<any>[][]) {
|
|
152
|
+
this.schema.nestedGenerics = nestedGenerics;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
get type(): Type<any> | any {
|
|
156
|
+
return this._type;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Get original type without transformation
|
|
161
|
+
* @param value
|
|
162
|
+
*/
|
|
163
|
+
set type(value: Type<any> | any) {
|
|
164
|
+
this._type = value;
|
|
165
|
+
this.build();
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Return the itemSchema computed type. if the type is a function used for recursive model, the function will be called to
|
|
170
|
+
* get the right type.
|
|
171
|
+
*/
|
|
172
|
+
get computedType() {
|
|
173
|
+
return this.itemSchema.getComputedType();
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
get itemSchema(): JsonSchema {
|
|
177
|
+
return this.isCollection ? this.schema.itemSchema() : this.schema;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
get parentSchema(): JsonSchema {
|
|
181
|
+
return this.parent.schema;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
get isDiscriminatorChild() {
|
|
185
|
+
return this.schema.isDiscriminator && this.discriminatorAncestor?.schema.discriminator().base !== this.target;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
static from<T extends JsonClassStore = JsonClassStore>(target: Type<any>): T;
|
|
189
|
+
|
|
190
|
+
static from<T extends JsonPropertyStore = JsonPropertyStore>(target: Type<any> | any, propertyKey: string | symbol): T;
|
|
191
|
+
|
|
192
|
+
static from<T extends JsonParameterStore = JsonParameterStore>(target: Type<any> | any, propertyKey: string | symbol, index: number): T;
|
|
193
|
+
|
|
194
|
+
static from<T extends JsonMethodStore = JsonMethodStore>(
|
|
195
|
+
target: Type<any> | any,
|
|
196
|
+
propertyKey: string | symbol,
|
|
197
|
+
descriptor: PropertyDescriptor
|
|
198
|
+
): T;
|
|
199
|
+
|
|
200
|
+
static from<T extends JsonEntityStore = JsonEntityStore>(...args: any[]): T;
|
|
201
|
+
|
|
202
|
+
static from<T extends JsonEntityStore = JsonEntityStore>(...args: any[]): T {
|
|
203
|
+
if (args[0].isStore) {
|
|
204
|
+
return args[0] as T;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
const target = args[0];
|
|
208
|
+
|
|
209
|
+
if (args.length > 1) {
|
|
210
|
+
args[0] = prototypeOf(args[0]);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const store = Store.from(...args);
|
|
214
|
+
|
|
215
|
+
if (!store.has("JsonEntityStore")) {
|
|
216
|
+
const decoratorType = decoratorTypeOf(args);
|
|
217
|
+
const entityStore = JsonEntitiesContainer.get(decoratorType)!;
|
|
218
|
+
|
|
219
|
+
const jsonSchemaStore = new entityStore({
|
|
220
|
+
store,
|
|
221
|
+
decoratorType,
|
|
222
|
+
target: classOf(target),
|
|
223
|
+
propertyKey: args[1],
|
|
224
|
+
index: typeof args[2] === "number" ? args[2] : undefined,
|
|
225
|
+
descriptor: typeof args[2] === "object" ? args[2] : undefined
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
jsonSchemaStore.build();
|
|
229
|
+
|
|
230
|
+
store.set("JsonEntityStore", jsonSchemaStore);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
return store.get<T>("JsonEntityStore")!;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
static fromMethod<T extends JsonMethodStore = JsonMethodStore>(target: any, propertyKey: string | symbol) {
|
|
237
|
+
return this.from<T>(target, propertyKey, descriptorOf(target, propertyKey));
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
static get(target: Type<any>, propertyKey: string | symbol, descriptor?: any) {
|
|
241
|
+
return JsonEntityStore.from(prototypeOf(target), propertyKey, descriptor);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
isGetterOnly() {
|
|
245
|
+
return isObject(this.descriptor) && !this.descriptor.value && this.descriptor.get && !this.descriptor.set;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
get<T = any>(key: string, defaultValue?: any) {
|
|
249
|
+
return this.store.get<T>(key, defaultValue);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
set(key: string, value?: any) {
|
|
253
|
+
return this.store.set(key, value);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
toString() {
|
|
257
|
+
return [this.targetName, this.propertyName, this.index].filter((o) => o !== undefined).join(":");
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
protected abstract build(): void;
|
|
261
|
+
|
|
262
|
+
protected buildType(type: any) {
|
|
263
|
+
if (isCollection(type)) {
|
|
264
|
+
this.collectionType = type;
|
|
265
|
+
} else {
|
|
266
|
+
this._type = type;
|
|
267
|
+
|
|
268
|
+
// issue #1534: Enum metadata stored as plain object instead of String (see: https://github.com/tsedio/tsed/issues/1534)
|
|
269
|
+
if (this._type && isPlainObject(this._type)) {
|
|
270
|
+
this._type = String;
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
getBestType() {
|
|
276
|
+
return this.itemSchema.hasDiscriminator
|
|
277
|
+
? this.itemSchema.discriminator().base
|
|
278
|
+
: isClassObject(this.type)
|
|
279
|
+
? this.itemSchema.getTarget()
|
|
280
|
+
: isArrowFn(this.type)
|
|
281
|
+
? this.type()
|
|
282
|
+
: this.type;
|
|
283
|
+
}
|
|
284
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export enum JsonFormatTypes {
|
|
2
|
+
DATE_TIME = "date-time",
|
|
3
|
+
DATE = "date",
|
|
4
|
+
TIME = "time",
|
|
5
|
+
EMAIL = "email",
|
|
6
|
+
HOSTNAME = "hostname",
|
|
7
|
+
IPV4 = "ipv4",
|
|
8
|
+
IPV6 = "ipv6",
|
|
9
|
+
URI = "uri",
|
|
10
|
+
URL = "url",
|
|
11
|
+
URI_REF = "uri-reference",
|
|
12
|
+
URI_TEMPLATE = "uri-template",
|
|
13
|
+
JSON_POINTER = "json-pointer",
|
|
14
|
+
RELATIVE_JSON_POINTER = "relative-json-pointer",
|
|
15
|
+
UUID = "uuid",
|
|
16
|
+
REGEX = "regex"
|
|
17
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import {nameOf, Type} from "@tsed/core";
|
|
2
|
+
|
|
3
|
+
import {getJsonEntityStore} from "../utils/getJsonEntityStore.js";
|
|
4
|
+
|
|
5
|
+
export class JsonLazyRef {
|
|
6
|
+
readonly isLazyRef = true;
|
|
7
|
+
|
|
8
|
+
constructor(readonly getType: () => Type<any>) {}
|
|
9
|
+
|
|
10
|
+
get target() {
|
|
11
|
+
return this.getType();
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
get schema() {
|
|
15
|
+
return getJsonEntityStore(this.getType()).schema;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
get name() {
|
|
19
|
+
return nameOf(this.getType());
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import "../index.js";
|
|
2
|
+
|
|
3
|
+
import {JsonMap} from "./JsonMap.js";
|
|
4
|
+
|
|
5
|
+
describe("JsonMap", () => {
|
|
6
|
+
it("should serialize a JsonMap", () => {
|
|
7
|
+
const map = new JsonMap({
|
|
8
|
+
id: "id"
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
expect(map.toJSON()).toEqual({
|
|
12
|
+
id: "id"
|
|
13
|
+
});
|
|
14
|
+
});
|
|
15
|
+
});
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import {isFunction} from "@tsed/core";
|
|
2
|
+
|
|
3
|
+
import {JsonSchemaOptions} from "../interfaces/JsonSchemaOptions.js";
|
|
4
|
+
import {execMapper} from "../registries/JsonSchemaMapperContainer.js";
|
|
5
|
+
|
|
6
|
+
export class JsonMap<T> extends Map<string, any> {
|
|
7
|
+
[key: string]: any;
|
|
8
|
+
|
|
9
|
+
$kind: string = "map";
|
|
10
|
+
readonly $isJsonDocument = true;
|
|
11
|
+
|
|
12
|
+
constructor(obj: Partial<T> = {}) {
|
|
13
|
+
super();
|
|
14
|
+
|
|
15
|
+
this.assign(obj);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
assign(obj: Partial<T> & any = {}) {
|
|
19
|
+
Object.entries(obj).forEach(([key, value]) => {
|
|
20
|
+
if (isFunction(this[key])) {
|
|
21
|
+
this[key](value);
|
|
22
|
+
} else {
|
|
23
|
+
this.set(key, value);
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
return this;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
toJSON(options?: JsonSchemaOptions) {
|
|
31
|
+
return execMapper("map", [this], options);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import {OS3MediaType} from "@tsed/openspec";
|
|
2
|
+
|
|
3
|
+
import {JsonMap} from "./JsonMap.js";
|
|
4
|
+
import {JsonSchema} from "./JsonSchema.js";
|
|
5
|
+
|
|
6
|
+
export class JsonMedia extends JsonMap<OS3MediaType<JsonSchema>> {
|
|
7
|
+
$kind: string = "operationMedia";
|
|
8
|
+
|
|
9
|
+
groups: string[] = [];
|
|
10
|
+
groupsName: string;
|
|
11
|
+
allowedGroups?: Set<string>;
|
|
12
|
+
|
|
13
|
+
schema(schema: JsonSchema) {
|
|
14
|
+
this.set("schema", schema);
|
|
15
|
+
|
|
16
|
+
return this;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
examples(examples: any) {
|
|
20
|
+
this.set("examples", examples);
|
|
21
|
+
|
|
22
|
+
return this;
|
|
23
|
+
}
|
|
24
|
+
}
|