@xfe-repo/bff-validation 1.0.0

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.
@@ -0,0 +1,158 @@
1
+ import {
2
+ IsBooleanOptional,
3
+ IsIntRequired,
4
+ IsStringOptional
5
+ } from "./chunk-7NGBVWVT.mjs";
6
+ import {
7
+ ValidationPipe,
8
+ createValidationPipe
9
+ } from "./chunk-DSV7LKIC.mjs";
10
+ import {
11
+ __name,
12
+ __publicField
13
+ } from "./chunk-GF5IOLXS.mjs";
14
+
15
+ // src/validation.pipe.spec.ts
16
+ import { BadRequestException } from "@nestjs/common";
17
+ import { Test } from "@nestjs/testing";
18
+ function _ts_decorate(decorators, target, key, desc) {
19
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
20
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
21
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
22
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
23
+ }
24
+ __name(_ts_decorate, "_ts_decorate");
25
+ function _ts_metadata(k, v) {
26
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
27
+ }
28
+ __name(_ts_metadata, "_ts_metadata");
29
+ var _User = class _User {
30
+ constructor() {
31
+ __publicField(this, "userId");
32
+ __publicField(this, "userName");
33
+ __publicField(this, "isStudent");
34
+ }
35
+ };
36
+ __name(_User, "User");
37
+ var User = _User;
38
+ _ts_decorate([
39
+ IsIntRequired({
40
+ description: "\u7528\u6237Id"
41
+ }),
42
+ _ts_metadata("design:type", Number)
43
+ ], User.prototype, "userId", void 0);
44
+ _ts_decorate([
45
+ IsStringOptional({
46
+ description: "\u7528\u6237\u540D"
47
+ }),
48
+ _ts_metadata("design:type", String)
49
+ ], User.prototype, "userName", void 0);
50
+ _ts_decorate([
51
+ IsBooleanOptional({
52
+ description: "\u662F\u5426\u5B66\u751F"
53
+ }),
54
+ _ts_metadata("design:type", Boolean)
55
+ ], User.prototype, "isStudent", void 0);
56
+ describe("In ValidationPipe", () => {
57
+ let pipe;
58
+ const transformType = "body";
59
+ beforeEach(async () => {
60
+ const module = await Test.createTestingModule({
61
+ providers: [
62
+ ValidationPipe
63
+ ]
64
+ }).compile();
65
+ pipe = module.get(ValidationPipe);
66
+ });
67
+ it("should valid IsNotEmpty, \u9A8C\u8BC1 Dto \u4E2D\u5FC5\u4F20\u5C5E\u6027 \u5FC5\u987B\u8981\u4F20", async () => {
68
+ const input = {
69
+ userName: "felixpan"
70
+ };
71
+ const transformedFn = pipe.transform(input, {
72
+ type: transformType,
73
+ metatype: User
74
+ });
75
+ await expect(transformedFn).rejects.toThrow("\u7528\u6237Id \u4E0D\u80FD\u4E3A\u7A7A");
76
+ });
77
+ it("should valid Dto \u4E2D\u672A\u5B9A\u4E49\u7684\u5C5E\u6027 \u5FC5\u987B\u8981\u6392\u9664", async () => {
78
+ const input = {
79
+ userId: 1,
80
+ userName: "felixpan",
81
+ extraField: "extraValue"
82
+ };
83
+ const data = pipe.transform(input, {
84
+ type: transformType,
85
+ metatype: User
86
+ });
87
+ await expect(data).resolves.toEqual({
88
+ userId: 1,
89
+ userName: "felixpan"
90
+ });
91
+ });
92
+ it("should valid \u6392\u9664 Dto \u4E2D undefined \u7684\u5C5E\u6027\u5B57\u6BB5", async () => {
93
+ const input = {
94
+ userId: 1,
95
+ userName: void 0
96
+ };
97
+ const data = pipe.transform(input, {
98
+ type: transformType,
99
+ metatype: User
100
+ });
101
+ await expect(data).resolves.toEqual({
102
+ userId: 1
103
+ });
104
+ });
105
+ });
106
+ describe("Out ValidationPipe", () => {
107
+ const pipe = createValidationPipe({
108
+ // 配合配置 validationPipe.transform({ type: 'custom' }) 时使用
109
+ validateCustomDecorators: true
110
+ });
111
+ const transformType = "custom";
112
+ beforeEach(async () => {
113
+ await Test.createTestingModule({
114
+ providers: []
115
+ }).compile();
116
+ });
117
+ it("should valid IsNotEmpty, \u9A8C\u8BC1 Dto \u4E2D\u5FC5\u4F20\u5C5E\u6027 \u5FC5\u987B\u8981\u4F20", async () => {
118
+ const input = {
119
+ userName: "felixpan"
120
+ };
121
+ const transformedFn = pipe.transform(input, {
122
+ type: transformType,
123
+ metatype: User
124
+ });
125
+ await expect(transformedFn).rejects.toThrow(BadRequestException);
126
+ });
127
+ it("should valid Dto \u4E2D\u672A\u5B9A\u4E49\u7684\u5C5E\u6027 \u5FC5\u987B\u8981\u6392\u9664", async () => {
128
+ const input = {
129
+ userId: 1,
130
+ userName: "felixpan",
131
+ extraField: "extraValue"
132
+ };
133
+ const data = pipe.transform(input, {
134
+ type: transformType,
135
+ metatype: User
136
+ });
137
+ await expect(data).resolves.toEqual({
138
+ userId: 1,
139
+ userName: "felixpan"
140
+ });
141
+ });
142
+ it("should valid \u6392\u9664 Dto \u4E2D undefined \u7684\u5C5E\u6027\u5B57\u6BB5", async () => {
143
+ const input = {
144
+ userId: 1,
145
+ userName: void 0
146
+ };
147
+ const data = pipe.transform(input, {
148
+ type: transformType,
149
+ metatype: User
150
+ });
151
+ await expect(data).resolves.toEqual({
152
+ userId: 1
153
+ });
154
+ });
155
+ });
156
+ export {
157
+ User
158
+ };
package/package.json ADDED
@@ -0,0 +1,46 @@
1
+ {
2
+ "name": "@xfe-repo/bff-validation",
3
+ "version": "1.0.0",
4
+ "sideEffects": false,
5
+ "module": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "exports": {
8
+ ".": {
9
+ "import": {
10
+ "types": "./dist/index.d.mts",
11
+ "default": "./dist/index.mjs"
12
+ },
13
+ "require": {
14
+ "types": "./dist/index.d.ts",
15
+ "default": "./dist/index.js"
16
+ }
17
+ }
18
+ },
19
+ "files": [
20
+ "dist"
21
+ ],
22
+ "dependencies": {
23
+ "class-transformer": "^0.5.1",
24
+ "class-validator": "^0.14.3",
25
+ "@xfe-repo/bff-core": "1.0.0"
26
+ },
27
+ "devDependencies": {
28
+ "@types/node": "^25.0.10",
29
+ "@xfe-repo/typescript-config": "0.0.6",
30
+ "@xfe-repo/eslint-config": "0.0.5"
31
+ },
32
+ "peerDependencies": {
33
+ "@nestjs/common": "^11.1.12",
34
+ "@nestjs/testing": "^11.1.12",
35
+ "@types/jest": "^30.0.0"
36
+ },
37
+ "publishConfig": {
38
+ "registry": "https://registry.npmjs.org/"
39
+ },
40
+ "scripts": {
41
+ "build": "tsup",
42
+ "dev": "tsup --watch",
43
+ "lint": "eslint \"src/**/*.ts*\"",
44
+ "clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist"
45
+ }
46
+ }