@pactor-app/schema 0.1.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Pactor Contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,42 @@
1
+ # @pactor-app/schema
2
+
3
+ [Pactor](https://github.com/426-330/pactor) 的 Schema 校验包:DSL 版本常量、Page DSL 与 DslNode 的 JSON Schema、基于 Ajv 的结构化校验器。
4
+
5
+ ## 安装
6
+
7
+ ```bash
8
+ npm install @pactor-app/schema
9
+ # 或
10
+ pnpm add @pactor-app/schema
11
+ ```
12
+
13
+ ## 示例
14
+
15
+ ```ts
16
+ import { DSL_VERSION, validatePageDsl } from '@pactor-app/schema';
17
+
18
+ DSL_VERSION; // "1.0"
19
+
20
+ const result = validatePageDsl(pageDsl);
21
+ if (!result.valid) {
22
+ for (const error of result.errors) {
23
+ // { path: "/page/id", message: "...", keyword: "required" }
24
+ console.error(`${error.path}: ${error.message} (${error.keyword})`);
25
+ }
26
+ }
27
+ ```
28
+
29
+ 特点:
30
+
31
+ - 结构化错误(不暴露 Ajv 原始错误对象),`required` 错误路径精确到缺失字段
32
+ - `allErrors` 模式,一次返回全部错误
33
+ - `body` 及任意深度 `children` 递归校验
34
+ - JSON Schema 可导出复用(服务端 / CLI 校验)
35
+
36
+ ## 文档
37
+
38
+ 完整文档见 <https://github.com/426-330/pactor/tree/main/docs>(`pnpm docs:dev` 本地启动文档站)。
39
+
40
+ ## License
41
+
42
+ [MIT](../../LICENSE)
package/dist/index.cjs ADDED
@@ -0,0 +1,281 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/index.ts
31
+ var index_exports = {};
32
+ __export(index_exports, {
33
+ ACTION_CONFIG_SCHEMA_ID: () => ACTION_CONFIG_SCHEMA_ID,
34
+ DSL_NODE_SCHEMA_ID: () => DSL_NODE_SCHEMA_ID,
35
+ DSL_VERSION: () => DSL_VERSION,
36
+ PAGE_DSL_SCHEMA_ID: () => PAGE_DSL_SCHEMA_ID,
37
+ actionConfigSchema: () => actionConfigSchema,
38
+ dslNodeSchema: () => dslNodeSchema,
39
+ pageDslSchema: () => pageDslSchema,
40
+ validatePageDsl: () => validatePageDsl
41
+ });
42
+ module.exports = __toCommonJS(index_exports);
43
+
44
+ // src/version.ts
45
+ var DSL_VERSION = "1.0";
46
+
47
+ // src/schemas/dsl-node.ts
48
+ var ACTION_CONFIG_SCHEMA_ID = "pactor:action-config";
49
+ var DSL_NODE_SCHEMA_ID = "pactor:dsl-node";
50
+ var permissionSchema = {
51
+ anyOf: [
52
+ { type: "string", minLength: 1 },
53
+ { type: "array", items: { type: "string", minLength: 1 } },
54
+ {
55
+ type: "object",
56
+ required: ["code"],
57
+ properties: {
58
+ code: {
59
+ anyOf: [
60
+ { type: "string", minLength: 1 },
61
+ { type: "array", items: { type: "string", minLength: 1 } }
62
+ ]
63
+ },
64
+ denied: { enum: ["hidden", "disabled", "readonly"] }
65
+ },
66
+ additionalProperties: false
67
+ }
68
+ ]
69
+ };
70
+ var conditionSchema = {
71
+ anyOf: [
72
+ { type: "string" },
73
+ {
74
+ type: "object",
75
+ required: ["expression"],
76
+ properties: {
77
+ expression: { type: "string" }
78
+ },
79
+ additionalProperties: false
80
+ }
81
+ ]
82
+ };
83
+ var actionConfigSchema = {
84
+ $id: ACTION_CONFIG_SCHEMA_ID,
85
+ type: "object",
86
+ required: ["action"],
87
+ properties: {
88
+ id: { type: "string", minLength: 1 },
89
+ action: { type: "string", minLength: 1 },
90
+ params: { type: "object" },
91
+ condition: conditionSchema,
92
+ permission: permissionSchema
93
+ },
94
+ additionalProperties: false
95
+ };
96
+ var actionOrArraySchema = {
97
+ anyOf: [
98
+ { $ref: ACTION_CONFIG_SCHEMA_ID },
99
+ { type: "array", items: { $ref: ACTION_CONFIG_SCHEMA_ID } }
100
+ ]
101
+ };
102
+ var dslNodeSchema = {
103
+ $id: DSL_NODE_SCHEMA_ID,
104
+ type: "object",
105
+ required: ["type"],
106
+ properties: {
107
+ id: { type: "string" },
108
+ type: { type: "string", minLength: 1 },
109
+ props: { type: "object" },
110
+ children: {
111
+ type: "array",
112
+ items: { $ref: DSL_NODE_SCHEMA_ID }
113
+ },
114
+ events: {
115
+ type: "object",
116
+ additionalProperties: actionOrArraySchema
117
+ },
118
+ permission: permissionSchema,
119
+ when: conditionSchema
120
+ },
121
+ additionalProperties: false
122
+ };
123
+
124
+ // src/schemas/page-dsl.ts
125
+ var PAGE_DSL_SCHEMA_ID = "pactor:page-dsl";
126
+ var permissionSchema2 = {
127
+ anyOf: [
128
+ { type: "string", minLength: 1 },
129
+ { type: "array", items: { type: "string", minLength: 1 } },
130
+ {
131
+ type: "object",
132
+ required: ["code"],
133
+ properties: {
134
+ code: {
135
+ anyOf: [
136
+ { type: "string", minLength: 1 },
137
+ { type: "array", items: { type: "string", minLength: 1 } }
138
+ ]
139
+ },
140
+ denied: { enum: ["hidden", "disabled", "readonly"] }
141
+ },
142
+ additionalProperties: false
143
+ }
144
+ ]
145
+ };
146
+ var actionOrArraySchema2 = {
147
+ anyOf: [
148
+ { $ref: ACTION_CONFIG_SCHEMA_ID },
149
+ { type: "array", items: { $ref: ACTION_CONFIG_SCHEMA_ID } }
150
+ ]
151
+ };
152
+ var dataSourceSchema = {
153
+ type: "object",
154
+ required: ["type"],
155
+ properties: {
156
+ type: { enum: ["http", "service", "static", "computed", "resource"] },
157
+ service: { type: "string", minLength: 1 },
158
+ autoLoad: { type: "boolean" },
159
+ params: { type: "object" },
160
+ request: {
161
+ type: "object",
162
+ required: ["url"],
163
+ properties: {
164
+ method: { type: "string" },
165
+ url: { type: "string", minLength: 1 },
166
+ params: { type: "object" },
167
+ headers: { type: "object", additionalProperties: { type: "string" } },
168
+ body: true
169
+ },
170
+ additionalProperties: false
171
+ },
172
+ pagination: {
173
+ type: "object",
174
+ properties: {
175
+ page: true,
176
+ pageSize: true
177
+ },
178
+ additionalProperties: false
179
+ },
180
+ value: true
181
+ },
182
+ additionalProperties: false
183
+ };
184
+ var pageDslSchema = {
185
+ $id: PAGE_DSL_SCHEMA_ID,
186
+ type: "object",
187
+ required: ["dsl", "page"],
188
+ properties: {
189
+ /** DSL Specification Version,如 "1.0" */
190
+ dsl: { type: "string", minLength: 1 },
191
+ requires: {
192
+ type: "object",
193
+ properties: {
194
+ /** Runtime 版本范围(semver range 字符串),如 ">=1.0.0" */
195
+ runtime: { type: "string", minLength: 1 },
196
+ /** 组件 API 版本要求:组件名 → semver range 字符串 */
197
+ components: {
198
+ type: "object",
199
+ additionalProperties: { type: "string", minLength: 1 }
200
+ }
201
+ },
202
+ additionalProperties: false
203
+ },
204
+ page: {
205
+ type: "object",
206
+ required: ["id"],
207
+ properties: {
208
+ id: { type: "string", minLength: 1 },
209
+ title: { type: "string" },
210
+ type: { type: "string" },
211
+ permission: permissionSchema2
212
+ },
213
+ additionalProperties: false
214
+ },
215
+ state: { type: "object" },
216
+ dataSources: {
217
+ type: "object",
218
+ additionalProperties: dataSourceSchema
219
+ },
220
+ lifecycle: {
221
+ type: "object",
222
+ properties: {
223
+ onInit: actionOrArraySchema2,
224
+ onLoad: actionOrArraySchema2,
225
+ onReady: actionOrArraySchema2,
226
+ onShow: actionOrArraySchema2,
227
+ onHide: actionOrArraySchema2,
228
+ onDestroy: actionOrArraySchema2
229
+ },
230
+ additionalProperties: false
231
+ },
232
+ body: { $ref: DSL_NODE_SCHEMA_ID }
233
+ },
234
+ additionalProperties: false
235
+ };
236
+
237
+ // src/validate.ts
238
+ var import_ajv = __toESM(require("ajv"), 1);
239
+ var ajv = new import_ajv.default({ allErrors: true, strict: false });
240
+ ajv.addSchema(actionConfigSchema);
241
+ ajv.addSchema(dslNodeSchema);
242
+ ajv.addSchema(pageDslSchema);
243
+ function toStructuredErrors(errors) {
244
+ return errors.map((error) => {
245
+ let path = error.instancePath || "/";
246
+ if (error.keyword === "required") {
247
+ const missing = error.params.missingProperty;
248
+ if (missing) {
249
+ path = `${path === "/" ? "" : path}/${missing}`;
250
+ }
251
+ }
252
+ return {
253
+ path,
254
+ message: error.message ?? "invalid",
255
+ keyword: error.keyword
256
+ };
257
+ });
258
+ }
259
+ function getValidator() {
260
+ const validate = ajv.getSchema(PAGE_DSL_SCHEMA_ID);
261
+ if (!validate) {
262
+ throw new Error(`schema not registered: ${PAGE_DSL_SCHEMA_ID}`);
263
+ }
264
+ return validate;
265
+ }
266
+ function validatePageDsl(document) {
267
+ const validate = getValidator();
268
+ const valid = validate(document);
269
+ return { valid, errors: valid ? [] : toStructuredErrors(validate.errors ?? []) };
270
+ }
271
+ // Annotate the CommonJS export names for ESM import in node:
272
+ 0 && (module.exports = {
273
+ ACTION_CONFIG_SCHEMA_ID,
274
+ DSL_NODE_SCHEMA_ID,
275
+ DSL_VERSION,
276
+ PAGE_DSL_SCHEMA_ID,
277
+ actionConfigSchema,
278
+ dslNodeSchema,
279
+ pageDslSchema,
280
+ validatePageDsl
281
+ });