@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,73 @@
|
|
|
1
|
+
import "../../index.js";
|
|
2
|
+
|
|
3
|
+
import {JsonEntityStore} from "../../domain/JsonEntityStore.js";
|
|
4
|
+
import {CollectionOf} from "../collections/collectionOf.js";
|
|
5
|
+
import {Max} from "./maximum.js";
|
|
6
|
+
|
|
7
|
+
describe("@Maximum", () => {
|
|
8
|
+
it("should declare maximum value", () => {
|
|
9
|
+
// WHEN
|
|
10
|
+
class Model {
|
|
11
|
+
@Max(0)
|
|
12
|
+
num: number;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// THEN
|
|
16
|
+
const classSchema = JsonEntityStore.from(Model);
|
|
17
|
+
|
|
18
|
+
expect(classSchema.schema.toJSON()).toEqual({
|
|
19
|
+
properties: {
|
|
20
|
+
num: {
|
|
21
|
+
maximum: 0,
|
|
22
|
+
type: "number"
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
type: "object"
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("should declare exclusive maximum value", () => {
|
|
30
|
+
// WHEN
|
|
31
|
+
class Model {
|
|
32
|
+
@Max(0, true)
|
|
33
|
+
num: number;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// THEN
|
|
37
|
+
const classSchema = JsonEntityStore.from(Model);
|
|
38
|
+
|
|
39
|
+
expect(classSchema.schema.toJSON()).toEqual({
|
|
40
|
+
properties: {
|
|
41
|
+
num: {
|
|
42
|
+
exclusiveMaximum: 0,
|
|
43
|
+
type: "number"
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
type: "object"
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
it("should declare maximum value (collection)", () => {
|
|
50
|
+
// WHEN
|
|
51
|
+
class Model {
|
|
52
|
+
@Max(0)
|
|
53
|
+
@CollectionOf(Number)
|
|
54
|
+
num: number[];
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// THEN
|
|
58
|
+
const classSchema = JsonEntityStore.from(Model);
|
|
59
|
+
|
|
60
|
+
expect(classSchema.schema.toJSON()).toEqual({
|
|
61
|
+
properties: {
|
|
62
|
+
num: {
|
|
63
|
+
type: "array",
|
|
64
|
+
items: {
|
|
65
|
+
maximum: 0,
|
|
66
|
+
type: "number"
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
},
|
|
70
|
+
type: "object"
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
});
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import {withErrorMsg} from "../../utils/withErrorMsg.js";
|
|
2
|
+
import {JsonEntityFn} from "./jsonEntityFn.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The value of `maximum` MUST be a number, representing an inclusive upper limit for a numeric instance.
|
|
6
|
+
*
|
|
7
|
+
* If the instance is a number, then this keyword validates only if the instance is less than or exactly equal to `maximum`.
|
|
8
|
+
*
|
|
9
|
+
* ::: warning
|
|
10
|
+
* For v6 user, use @@Maximum@@ from @tsed/schema instead of @tsed/platform-http.
|
|
11
|
+
* :::
|
|
12
|
+
*
|
|
13
|
+
* ## Example
|
|
14
|
+
* ### With primitive type
|
|
15
|
+
*
|
|
16
|
+
* ```typescript
|
|
17
|
+
* class Model {
|
|
18
|
+
* @Maximum(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
|
+
* "maximum": 10
|
|
32
|
+
* }
|
|
33
|
+
* }
|
|
34
|
+
* }
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
* ### With array type
|
|
38
|
+
*
|
|
39
|
+
* ```typescript
|
|
40
|
+
* class Model {
|
|
41
|
+
* @Maximum(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
|
+
* "maximum": 10
|
|
58
|
+
* }
|
|
59
|
+
* }
|
|
60
|
+
* }
|
|
61
|
+
* }
|
|
62
|
+
* ```
|
|
63
|
+
*
|
|
64
|
+
* @param {number} maximum The maximum value allowed
|
|
65
|
+
* @param {boolean} exclusive Same effect as ExclusiveMaximum decorator.
|
|
66
|
+
* @decorator
|
|
67
|
+
* @validation
|
|
68
|
+
* @swagger
|
|
69
|
+
* @schema
|
|
70
|
+
* @input
|
|
71
|
+
* @ajv-errors
|
|
72
|
+
*/
|
|
73
|
+
export const Maximum = withErrorMsg("maximum", (maximum: number, exclusive: boolean = false) => {
|
|
74
|
+
return JsonEntityFn((store) => {
|
|
75
|
+
exclusive ? store.itemSchema.exclusiveMaximum(maximum) : store.itemSchema.maximum(maximum);
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
/**
|
|
79
|
+
* The value of `maximum` MUST be a number, representing an inclusive upper limit for a numeric instance.
|
|
80
|
+
*
|
|
81
|
+
* If the instance is a number, then this keyword validates only if the instance is less than or exactly equal to `maximum`.
|
|
82
|
+
*
|
|
83
|
+
* ::: warning
|
|
84
|
+
* For v6 user, use @@Maximum@@ from @tsed/schema instead of @tsed/platform-http.
|
|
85
|
+
* :::
|
|
86
|
+
*
|
|
87
|
+
* ## Example
|
|
88
|
+
* ### With primitive type
|
|
89
|
+
*
|
|
90
|
+
* ```typescript
|
|
91
|
+
* class Model {
|
|
92
|
+
* @Max(10)
|
|
93
|
+
* property: number;
|
|
94
|
+
* }
|
|
95
|
+
* ```
|
|
96
|
+
*
|
|
97
|
+
* Will produce:
|
|
98
|
+
*
|
|
99
|
+
* ```json
|
|
100
|
+
* {
|
|
101
|
+
* "type": "object",
|
|
102
|
+
* "properties": {
|
|
103
|
+
* "property": {
|
|
104
|
+
* "type": "number",
|
|
105
|
+
* "maximum": 10
|
|
106
|
+
* }
|
|
107
|
+
* }
|
|
108
|
+
* }
|
|
109
|
+
* ```
|
|
110
|
+
*
|
|
111
|
+
* ### With array type
|
|
112
|
+
*
|
|
113
|
+
* ```typescript
|
|
114
|
+
* class Model {
|
|
115
|
+
* @Max(10)
|
|
116
|
+
* @CollectionOf(Number)
|
|
117
|
+
* property: number[];
|
|
118
|
+
* }
|
|
119
|
+
* ```
|
|
120
|
+
*
|
|
121
|
+
* Will produce:
|
|
122
|
+
*
|
|
123
|
+
* ```json
|
|
124
|
+
* {
|
|
125
|
+
* "type": "object",
|
|
126
|
+
* "properties": {
|
|
127
|
+
* "property": {
|
|
128
|
+
* "type": "array",
|
|
129
|
+
* "items": {
|
|
130
|
+
* "type": "number",
|
|
131
|
+
* "maximum": 10
|
|
132
|
+
* }
|
|
133
|
+
* }
|
|
134
|
+
* }
|
|
135
|
+
* }
|
|
136
|
+
* ```
|
|
137
|
+
*
|
|
138
|
+
* @param {number} maximum The maximum value allowed
|
|
139
|
+
* @param {boolean} exclusive Same effect as ExclusiveMaximum decorator.
|
|
140
|
+
* @decorator
|
|
141
|
+
* @validation
|
|
142
|
+
* @swagger
|
|
143
|
+
* @schema
|
|
144
|
+
* @input
|
|
145
|
+
*/
|
|
146
|
+
export const Max = Maximum;
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import "../../index.js";
|
|
2
|
+
|
|
3
|
+
import {validateModel} from "../../../test/helpers/validateModel.js";
|
|
4
|
+
import {JsonEntityStore} from "../../domain/JsonEntityStore.js";
|
|
5
|
+
import {getJsonSchema} from "../../utils/getJsonSchema.js";
|
|
6
|
+
import {CollectionOf} from "../collections/collectionOf.js";
|
|
7
|
+
import {MinLength} from "./minLength.js";
|
|
8
|
+
|
|
9
|
+
describe("@MinLength", () => {
|
|
10
|
+
it("should declare minimum value", () => {
|
|
11
|
+
// WHEN
|
|
12
|
+
class Model {
|
|
13
|
+
@MinLength(0)
|
|
14
|
+
word: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// THEN
|
|
18
|
+
const classSchema = JsonEntityStore.from(Model);
|
|
19
|
+
|
|
20
|
+
expect(classSchema.schema.toJSON()).toEqual({
|
|
21
|
+
properties: {
|
|
22
|
+
word: {
|
|
23
|
+
minLength: 0,
|
|
24
|
+
type: "string"
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
type: "object"
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
it("should throw error", () => {
|
|
31
|
+
// WHEN
|
|
32
|
+
let actualError: any;
|
|
33
|
+
try {
|
|
34
|
+
MinLength(-1);
|
|
35
|
+
} catch (er) {
|
|
36
|
+
actualError = er;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// THEN
|
|
40
|
+
expect(actualError.message).toBe("The value of minLength MUST be a non-negative integer.");
|
|
41
|
+
});
|
|
42
|
+
it("should declare minimum value on type (collection)", () => {
|
|
43
|
+
// WHEN
|
|
44
|
+
class Model {
|
|
45
|
+
@MinLength(0)
|
|
46
|
+
@CollectionOf(String)
|
|
47
|
+
words: string[];
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// THEN
|
|
51
|
+
const classSchema = JsonEntityStore.from(Model);
|
|
52
|
+
|
|
53
|
+
expect(classSchema.schema.toJSON()).toEqual({
|
|
54
|
+
properties: {
|
|
55
|
+
words: {
|
|
56
|
+
type: "array",
|
|
57
|
+
items: {
|
|
58
|
+
minLength: 0,
|
|
59
|
+
type: "string"
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
type: "object"
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
it("should declare minimum value on collection (collection)", () => {
|
|
67
|
+
// WHEN
|
|
68
|
+
class Model {
|
|
69
|
+
@MinLength(0)
|
|
70
|
+
@(CollectionOf(String).MinItems(0).MaxItems(10))
|
|
71
|
+
words: string[];
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// THEN
|
|
75
|
+
const classSchema = JsonEntityStore.from(Model);
|
|
76
|
+
|
|
77
|
+
expect(classSchema.schema.toJSON()).toEqual({
|
|
78
|
+
properties: {
|
|
79
|
+
words: {
|
|
80
|
+
type: "array",
|
|
81
|
+
items: {
|
|
82
|
+
minLength: 0,
|
|
83
|
+
type: "string"
|
|
84
|
+
},
|
|
85
|
+
maxItems: 10,
|
|
86
|
+
minItems: 0
|
|
87
|
+
}
|
|
88
|
+
},
|
|
89
|
+
type: "object"
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
it("should declare minLength field with custom error message", () => {
|
|
93
|
+
// WHEN
|
|
94
|
+
class Model {
|
|
95
|
+
@(MinLength(2).Error("Require at least 2 characters"))
|
|
96
|
+
@(CollectionOf(String).MinItems(0).MaxItems(10))
|
|
97
|
+
words: string[];
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// THEN
|
|
101
|
+
const schema = getJsonSchema(Model, {customKeys: true});
|
|
102
|
+
|
|
103
|
+
expect(schema).toEqual({
|
|
104
|
+
properties: {
|
|
105
|
+
words: {
|
|
106
|
+
items: {
|
|
107
|
+
minLength: 2,
|
|
108
|
+
type: "string",
|
|
109
|
+
errorMessage: {
|
|
110
|
+
minLength: "Require at least 2 characters"
|
|
111
|
+
}
|
|
112
|
+
},
|
|
113
|
+
maxItems: 10,
|
|
114
|
+
minItems: 0,
|
|
115
|
+
type: "array"
|
|
116
|
+
}
|
|
117
|
+
},
|
|
118
|
+
type: "object"
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
const result = validateModel(
|
|
122
|
+
{
|
|
123
|
+
words: ["a"]
|
|
124
|
+
},
|
|
125
|
+
Model
|
|
126
|
+
);
|
|
127
|
+
|
|
128
|
+
expect(result).toEqual([
|
|
129
|
+
{
|
|
130
|
+
instancePath: "/words/0",
|
|
131
|
+
keyword: "errorMessage",
|
|
132
|
+
message: "Require at least 2 characters",
|
|
133
|
+
params: {
|
|
134
|
+
errors: [
|
|
135
|
+
{
|
|
136
|
+
emUsed: true,
|
|
137
|
+
instancePath: "/words/0",
|
|
138
|
+
keyword: "minLength",
|
|
139
|
+
message: "must NOT have fewer than 2 characters",
|
|
140
|
+
params: {
|
|
141
|
+
limit: 2
|
|
142
|
+
},
|
|
143
|
+
schemaPath: "#/properties/words/items/minLength"
|
|
144
|
+
}
|
|
145
|
+
]
|
|
146
|
+
},
|
|
147
|
+
schemaPath: "#/properties/words/items/errorMessage"
|
|
148
|
+
}
|
|
149
|
+
]);
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it("should throw error with custom error message", () => {
|
|
153
|
+
// WHEN
|
|
154
|
+
class Model {
|
|
155
|
+
@(MinLength(10).Error("Title must be at least 10 characters long"))
|
|
156
|
+
word: string;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// THEN
|
|
160
|
+
|
|
161
|
+
const result = validateModel({word: "test"}, Model);
|
|
162
|
+
|
|
163
|
+
expect(result).toEqual([
|
|
164
|
+
{
|
|
165
|
+
instancePath: "/word",
|
|
166
|
+
keyword: "errorMessage",
|
|
167
|
+
message: "Title must be at least 10 characters long",
|
|
168
|
+
params: {
|
|
169
|
+
errors: [
|
|
170
|
+
{
|
|
171
|
+
emUsed: true,
|
|
172
|
+
instancePath: "/word",
|
|
173
|
+
keyword: "minLength",
|
|
174
|
+
message: "must NOT have fewer than 10 characters",
|
|
175
|
+
params: {
|
|
176
|
+
limit: 10
|
|
177
|
+
},
|
|
178
|
+
schemaPath: "#/properties/word/minLength"
|
|
179
|
+
}
|
|
180
|
+
]
|
|
181
|
+
},
|
|
182
|
+
schemaPath: "#/properties/word/errorMessage"
|
|
183
|
+
}
|
|
184
|
+
]);
|
|
185
|
+
});
|
|
186
|
+
});
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import {withErrorMsg} from "../../utils/withErrorMsg.js";
|
|
2
|
+
import {JsonEntityFn} from "./jsonEntityFn.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
*
|
|
6
|
+
* A string instance is valid against this keyword if its length is greater than, or equal to, the value of this keyword.
|
|
7
|
+
*
|
|
8
|
+
* The length of a string instance is defined as the number of its characters as defined by [RFC 7159](http://json-schema.org/latest/json-schema-validation.html#RFC7159).
|
|
9
|
+
*
|
|
10
|
+
* ::: warning
|
|
11
|
+
* The value of minLength MUST be a non-negative integer.
|
|
12
|
+
* :::
|
|
13
|
+
*
|
|
14
|
+
* ::: tip
|
|
15
|
+
* Omitting this keyword has the same behavior as a value of 0.
|
|
16
|
+
* :::
|
|
17
|
+
*
|
|
18
|
+
* ::: warning
|
|
19
|
+
* For v6 user, use @@MinLength@@ from @tsed/schema instead of @tsed/platform-http.
|
|
20
|
+
* :::
|
|
21
|
+
*
|
|
22
|
+
* ## Example
|
|
23
|
+
* ### With primitive type
|
|
24
|
+
*
|
|
25
|
+
* ```typescript
|
|
26
|
+
* class Model {
|
|
27
|
+
* @MinLength(10)
|
|
28
|
+
* property: number;
|
|
29
|
+
* }
|
|
30
|
+
* ```
|
|
31
|
+
*
|
|
32
|
+
* Will produce:
|
|
33
|
+
*
|
|
34
|
+
* ```json
|
|
35
|
+
* {
|
|
36
|
+
* "type": "object",
|
|
37
|
+
* "properties": {
|
|
38
|
+
* "property": {
|
|
39
|
+
* "type": "string",
|
|
40
|
+
* "maxLength": 10
|
|
41
|
+
* }
|
|
42
|
+
* }
|
|
43
|
+
* }
|
|
44
|
+
* ```
|
|
45
|
+
*
|
|
46
|
+
* ### With array type
|
|
47
|
+
*
|
|
48
|
+
* ```typescript
|
|
49
|
+
* class Model {
|
|
50
|
+
* @MinLength(10)
|
|
51
|
+
* @CollectionOf(String)
|
|
52
|
+
* property: string[];
|
|
53
|
+
* }
|
|
54
|
+
* ```
|
|
55
|
+
*
|
|
56
|
+
* Will produce:
|
|
57
|
+
*
|
|
58
|
+
* ```json
|
|
59
|
+
* {
|
|
60
|
+
* "type": "object",
|
|
61
|
+
* "properties": {
|
|
62
|
+
* "property": {
|
|
63
|
+
* "type": "array",
|
|
64
|
+
* "items": {
|
|
65
|
+
* "type": "string",
|
|
66
|
+
* "minLength": 10
|
|
67
|
+
* }
|
|
68
|
+
* }
|
|
69
|
+
* }
|
|
70
|
+
* }
|
|
71
|
+
* ```
|
|
72
|
+
*
|
|
73
|
+
* @param {number} minLength
|
|
74
|
+
* @decorator
|
|
75
|
+
* @validation
|
|
76
|
+
* @swagger
|
|
77
|
+
* @schema
|
|
78
|
+
* @input
|
|
79
|
+
* @ajv-errors
|
|
80
|
+
*/
|
|
81
|
+
export const MinLength = withErrorMsg("minLength", (minLength: number) => {
|
|
82
|
+
if (minLength < 0) {
|
|
83
|
+
throw new Error("The value of minLength MUST be a non-negative integer.");
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return JsonEntityFn((store) => {
|
|
87
|
+
store.itemSchema.minLength(minLength);
|
|
88
|
+
});
|
|
89
|
+
});
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import "../../index.js";
|
|
2
|
+
|
|
3
|
+
import {JsonEntityStore} from "../../domain/JsonEntityStore.js";
|
|
4
|
+
import {CollectionOf} from "../collections/collectionOf.js";
|
|
5
|
+
import {Min} from "./minimum.js";
|
|
6
|
+
|
|
7
|
+
describe("@Minimum", () => {
|
|
8
|
+
it("should declare minimum value", () => {
|
|
9
|
+
// WHEN
|
|
10
|
+
class Model {
|
|
11
|
+
@Min(0)
|
|
12
|
+
num: number;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// THEN
|
|
16
|
+
const classSchema = JsonEntityStore.from(Model);
|
|
17
|
+
|
|
18
|
+
expect(classSchema.schema.toJSON()).toEqual({
|
|
19
|
+
properties: {
|
|
20
|
+
num: {
|
|
21
|
+
minimum: 0,
|
|
22
|
+
type: "number"
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
type: "object"
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("should declare exclusive minimum value", () => {
|
|
30
|
+
// WHEN
|
|
31
|
+
class Model {
|
|
32
|
+
@Min(0, true)
|
|
33
|
+
num: number;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// THEN
|
|
37
|
+
const classSchema = JsonEntityStore.from(Model);
|
|
38
|
+
|
|
39
|
+
expect(classSchema.schema.toJSON()).toEqual({
|
|
40
|
+
properties: {
|
|
41
|
+
num: {
|
|
42
|
+
exclusiveMinimum: 0,
|
|
43
|
+
type: "number"
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
type: "object"
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it("should declare minimum value (collection)", () => {
|
|
51
|
+
// WHEN
|
|
52
|
+
class Model {
|
|
53
|
+
@Min(0)
|
|
54
|
+
@CollectionOf(Number)
|
|
55
|
+
num: number[];
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// THEN
|
|
59
|
+
const classSchema = JsonEntityStore.from(Model);
|
|
60
|
+
|
|
61
|
+
expect(classSchema.schema.toJSON()).toEqual({
|
|
62
|
+
properties: {
|
|
63
|
+
num: {
|
|
64
|
+
type: "array",
|
|
65
|
+
items: {
|
|
66
|
+
minimum: 0,
|
|
67
|
+
type: "number"
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
},
|
|
71
|
+
type: "object"
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
});
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import {withErrorMsg} from "../../utils/withErrorMsg.js";
|
|
2
|
+
import {JsonEntityFn} from "./jsonEntityFn.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The value of `minimum` MUST be a number, representing an inclusive upper limit for a numeric instance.
|
|
6
|
+
*
|
|
7
|
+
* If the instance is a number, then this keyword validates only if the instance is greater than or exactly equal to `minimum`.
|
|
8
|
+
*
|
|
9
|
+
* ::: warning
|
|
10
|
+
* This decorator will be removed in v7.
|
|
11
|
+
* For v6 user, use @@Minimum@@ from @tsed/schema instead of @tsed/platform-http.
|
|
12
|
+
* :::
|
|
13
|
+
*
|
|
14
|
+
* ## Example
|
|
15
|
+
* ### With primitive type
|
|
16
|
+
*
|
|
17
|
+
* ```typescript
|
|
18
|
+
* class Model {
|
|
19
|
+
* @Minimum(10)
|
|
20
|
+
* property: number;
|
|
21
|
+
* }
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* Will produce:
|
|
25
|
+
*
|
|
26
|
+
* ```json
|
|
27
|
+
* {
|
|
28
|
+
* "type": "object",
|
|
29
|
+
* "properties": {
|
|
30
|
+
* "property": {
|
|
31
|
+
* "type": "number",
|
|
32
|
+
* "minimum": 10
|
|
33
|
+
* }
|
|
34
|
+
* }
|
|
35
|
+
* }
|
|
36
|
+
* ```
|
|
37
|
+
*
|
|
38
|
+
* ### With array type
|
|
39
|
+
*
|
|
40
|
+
* ```typescript
|
|
41
|
+
* class Model {
|
|
42
|
+
* @Minimum(10)
|
|
43
|
+
* @CollectionOf(Number)
|
|
44
|
+
* property: number[];
|
|
45
|
+
* }
|
|
46
|
+
* ```
|
|
47
|
+
*
|
|
48
|
+
* Will produce:
|
|
49
|
+
*
|
|
50
|
+
* ```json
|
|
51
|
+
* {
|
|
52
|
+
* "type": "object",
|
|
53
|
+
* "properties": {
|
|
54
|
+
* "property": {
|
|
55
|
+
* "type": "array",
|
|
56
|
+
* "items": {
|
|
57
|
+
* "type": "number",
|
|
58
|
+
* "minimum": 10
|
|
59
|
+
* }
|
|
60
|
+
* }
|
|
61
|
+
* }
|
|
62
|
+
* }
|
|
63
|
+
* ```
|
|
64
|
+
*
|
|
65
|
+
* @param {number} minimum The minimum value allowed
|
|
66
|
+
* @param {boolean} exclusive
|
|
67
|
+
* @decorator
|
|
68
|
+
* @validation
|
|
69
|
+
* @swagger
|
|
70
|
+
* @schema
|
|
71
|
+
* @input
|
|
72
|
+
* @ajv-errors
|
|
73
|
+
*/
|
|
74
|
+
export const Minimum = withErrorMsg("minimum", (minimum: number, exclusive: boolean = false) => {
|
|
75
|
+
return JsonEntityFn((store) => {
|
|
76
|
+
exclusive ? store.itemSchema.exclusiveMinimum(minimum) : store.itemSchema.minimum(minimum);
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* The value of `minimum` MUST be a number, representing an inclusive upper limit for a numeric instance.
|
|
82
|
+
*
|
|
83
|
+
* If the instance is a number, then this keyword validates only if the instance is greater than or exactly equal to `minimum`.
|
|
84
|
+
*
|
|
85
|
+
* ## Example
|
|
86
|
+
* ### With primitive type
|
|
87
|
+
*
|
|
88
|
+
* ```typescript
|
|
89
|
+
* class Model {
|
|
90
|
+
* @Min(10)
|
|
91
|
+
* property: number;
|
|
92
|
+
* }
|
|
93
|
+
* ```
|
|
94
|
+
*
|
|
95
|
+
* Will produce:
|
|
96
|
+
*
|
|
97
|
+
* ```json
|
|
98
|
+
* {
|
|
99
|
+
* "type": "object",
|
|
100
|
+
* "properties": {
|
|
101
|
+
* "property": {
|
|
102
|
+
* "type": "number",
|
|
103
|
+
* "minimum": 10
|
|
104
|
+
* }
|
|
105
|
+
* }
|
|
106
|
+
* }
|
|
107
|
+
* ```
|
|
108
|
+
*
|
|
109
|
+
* ### With array type
|
|
110
|
+
*
|
|
111
|
+
* ```typescript
|
|
112
|
+
* class Model {
|
|
113
|
+
* @Min(10)
|
|
114
|
+
* @CollectionOf(Number)
|
|
115
|
+
* property: number[];
|
|
116
|
+
* }
|
|
117
|
+
* ```
|
|
118
|
+
*
|
|
119
|
+
* Will produce:
|
|
120
|
+
*
|
|
121
|
+
* ```json
|
|
122
|
+
* {
|
|
123
|
+
* "type": "object",
|
|
124
|
+
* "properties": {
|
|
125
|
+
* "property": {
|
|
126
|
+
* "type": "array",
|
|
127
|
+
* "items": {
|
|
128
|
+
* "type": "number",
|
|
129
|
+
* "minimum": 10
|
|
130
|
+
* }
|
|
131
|
+
* }
|
|
132
|
+
* }
|
|
133
|
+
* }
|
|
134
|
+
* ```
|
|
135
|
+
*
|
|
136
|
+
* @alias Minimum
|
|
137
|
+
* @param minimum The minimum value allowed
|
|
138
|
+
* @param exclusive
|
|
139
|
+
* @decorator
|
|
140
|
+
* @validation
|
|
141
|
+
* @swagger
|
|
142
|
+
* @schema
|
|
143
|
+
* @input
|
|
144
|
+
*/
|
|
145
|
+
export const Min = Minimum;
|