@zhin.js/schema 1.1.5 → 1.1.6
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/README.md +2 -2
- package/lib/json-schema.d.ts +9 -0
- package/lib/json-schema.js +2 -0
- package/lib/schema.d.ts +22 -2
- package/lib/schema.js +35 -3
- package/package.json +5 -1
package/README.md
CHANGED
|
@@ -207,8 +207,8 @@ plugins:
|
|
|
207
207
|
| `.default(value)` | 设置默认值 |
|
|
208
208
|
| `.option(label, value)` | 添加可选项 |
|
|
209
209
|
| `.multiple()` | 允许多选 |
|
|
210
|
-
| `.min(value)` |
|
|
211
|
-
| `.max(value)` |
|
|
210
|
+
| `.min(value)` | 设置最小值(数字下限 / 字符串最小长度 / 列表最少项);写入导出的 JSON Schema,由 Runtime 配置校验执行,`schema(value)` 与 `safeParse` 只做结构校验 |
|
|
211
|
+
| `.max(value)` | 设置最大值,规则同 `.min` |
|
|
212
212
|
| `.step(value)` | 设置步长 |
|
|
213
213
|
| `.component(name)` | 指定渲染组件 |
|
|
214
214
|
| `.toJSON()` | 业务 Schema 的内部序列化 |
|
package/lib/json-schema.d.ts
CHANGED
|
@@ -12,6 +12,15 @@ export interface ConfigFieldMetadata {
|
|
|
12
12
|
readonly component?: string;
|
|
13
13
|
readonly options?: readonly Schema.Option[];
|
|
14
14
|
readonly effect?: 'reload';
|
|
15
|
+
/** Property-level narrowing of the Plugin's feature levels (ADR 0050 §2). */
|
|
16
|
+
readonly levels?: readonly ('global' | 'adapter' | 'endpoint' | 'conversation')[];
|
|
17
|
+
}
|
|
18
|
+
/** Root `x-zhin-config` of a Plugin schema: layered levels (ADR 0050 / 0055). */
|
|
19
|
+
export interface ConfigFeatureLevels {
|
|
20
|
+
readonly levels: readonly ('global' | 'adapter' | 'endpoint' | 'conversation')[];
|
|
21
|
+
readonly conversationFields?: readonly string[];
|
|
22
|
+
readonly selfServiceFields?: readonly string[];
|
|
23
|
+
readonly chatAdminFields?: readonly string[];
|
|
15
24
|
}
|
|
16
25
|
export type ConfigJsonSchema = Readonly<Record<string, unknown>>;
|
|
17
26
|
/** Export serializable configuration declarations; non-JSON business types are explicit errors. */
|
package/lib/json-schema.js
CHANGED
|
@@ -100,6 +100,8 @@ export function toJsonSchema(schema) {
|
|
|
100
100
|
...(meta.component !== undefined ? { component: meta.component } : {}),
|
|
101
101
|
...(meta.options !== undefined ? { options: structuredClone(meta.options) } : {}),
|
|
102
102
|
...(meta.effect !== undefined ? { effect: meta.effect } : {}),
|
|
103
|
+
...(meta.feature !== undefined ? structuredClone(meta.feature) : {}),
|
|
104
|
+
...(meta.fieldLevels !== undefined ? { levels: [...meta.fieldLevels] } : {}),
|
|
103
105
|
};
|
|
104
106
|
if (Object.keys(annotations).length)
|
|
105
107
|
result['x-zhin-config'] = annotations;
|
package/lib/schema.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { safeParseSchema, type SchemaSafeParseResult } from './validate.js';
|
|
2
|
-
import { type ConfigFieldMetadata, type ConfigFieldI18n } from './json-schema.js';
|
|
2
|
+
import { type ConfigFeatureLevels, type ConfigFieldMetadata, type ConfigFieldI18n } from './json-schema.js';
|
|
3
3
|
export { SchemaValidationError, type SchemaSafeParseResult, type SchemaValidationIssue } from './validate.js';
|
|
4
4
|
export { safeParseSchema };
|
|
5
5
|
declare const configPresent: unique symbol;
|
|
@@ -31,13 +31,31 @@ export declare class Schema<S = any, T = S> {
|
|
|
31
31
|
secret(): this;
|
|
32
32
|
/** Declare the effect shown in configuration forms. */
|
|
33
33
|
effect(effect: ConfigFieldMetadata['effect']): this;
|
|
34
|
+
/**
|
|
35
|
+
* Plugin root only: layered feature levels (ADR 0050 / 0055) and the fields editable at the
|
|
36
|
+
* conversation level, in chat by group admins, and in chat above the conversation level.
|
|
37
|
+
*/
|
|
38
|
+
featureLevels(levels: ConfigFeatureLevels['levels'], fields?: Omit<ConfigFeatureLevels, 'levels'>): this;
|
|
39
|
+
/**
|
|
40
|
+
* Plugin property only: narrow the feature levels that may override this field (ADR 0050 §2),
|
|
41
|
+
* e.g. `.fieldLevels(['global'])` for secrets and process-wide resources. It can never widen
|
|
42
|
+
* the root `featureLevels` declaration; the Runtime rejects a wider list at startup.
|
|
43
|
+
*/
|
|
44
|
+
fieldLevels(levels: ConfigFeatureLevels['levels']): this;
|
|
34
45
|
/** Export the standard JSON Schema consumed by Runtime and Console. */
|
|
35
46
|
toJsonSchema(): Readonly<Record<string, unknown>>;
|
|
36
47
|
/** 设置选项列表 */
|
|
37
48
|
option(list: (T | Schema.Option<T>)[]): this;
|
|
38
49
|
/** 设置是否允许多选 */
|
|
39
50
|
multiple(): this;
|
|
40
|
-
/**
|
|
51
|
+
/**
|
|
52
|
+
* 设置最小值。
|
|
53
|
+
* `min` / `max` / `step` are range constraints of the exported JSON Schema
|
|
54
|
+
* (minimum/minLength/minItems, maximum/…, multipleOf) and are enforced where that
|
|
55
|
+
* schema is compiled (Runtime config validation). The callable schema and
|
|
56
|
+
* `safeParse` are structural only; `toJsonSchema` relies on that when it matches
|
|
57
|
+
* union branches (see assertPublicDefault).
|
|
58
|
+
*/
|
|
41
59
|
min(value: number): this;
|
|
42
60
|
/** 设置最大值 */
|
|
43
61
|
max(value: number): this;
|
|
@@ -125,6 +143,8 @@ export declare namespace Schema {
|
|
|
125
143
|
step?: number;
|
|
126
144
|
secret?: boolean;
|
|
127
145
|
effect?: ConfigFieldMetadata['effect'];
|
|
146
|
+
feature?: ConfigFeatureLevels;
|
|
147
|
+
fieldLevels?: ConfigFeatureLevels['levels'];
|
|
128
148
|
}
|
|
129
149
|
interface Options {
|
|
130
150
|
object?: Record<string, Schema>;
|
package/lib/schema.js
CHANGED
|
@@ -102,6 +102,27 @@ export class Schema {
|
|
|
102
102
|
this.meta.effect = effect;
|
|
103
103
|
return this;
|
|
104
104
|
}
|
|
105
|
+
/**
|
|
106
|
+
* Plugin root only: layered feature levels (ADR 0050 / 0055) and the fields editable at the
|
|
107
|
+
* conversation level, in chat by group admins, and in chat above the conversation level.
|
|
108
|
+
*/
|
|
109
|
+
featureLevels(levels, fields = {}) {
|
|
110
|
+
if (this.meta.type !== 'object')
|
|
111
|
+
throw new TypeError('featureLevels only supports the plugin root object');
|
|
112
|
+
this.meta.feature = structuredClone({ levels, ...fields });
|
|
113
|
+
return this;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Plugin property only: narrow the feature levels that may override this field (ADR 0050 §2),
|
|
117
|
+
* e.g. `.fieldLevels(['global'])` for secrets and process-wide resources. It can never widen
|
|
118
|
+
* the root `featureLevels` declaration; the Runtime rejects a wider list at startup.
|
|
119
|
+
*/
|
|
120
|
+
fieldLevels(levels) {
|
|
121
|
+
if (!levels.length || !levels.includes('global'))
|
|
122
|
+
throw new TypeError('fieldLevels must include global');
|
|
123
|
+
this.meta.fieldLevels = [...levels];
|
|
124
|
+
return this;
|
|
125
|
+
}
|
|
105
126
|
/** Export the standard JSON Schema consumed by Runtime and Console. */
|
|
106
127
|
toJsonSchema() {
|
|
107
128
|
return toJsonSchema(this);
|
|
@@ -118,7 +139,14 @@ export class Schema {
|
|
|
118
139
|
this.meta.multiple = true;
|
|
119
140
|
return this;
|
|
120
141
|
}
|
|
121
|
-
/**
|
|
142
|
+
/**
|
|
143
|
+
* 设置最小值。
|
|
144
|
+
* `min` / `max` / `step` are range constraints of the exported JSON Schema
|
|
145
|
+
* (minimum/minLength/minItems, maximum/…, multipleOf) and are enforced where that
|
|
146
|
+
* schema is compiled (Runtime config validation). The callable schema and
|
|
147
|
+
* `safeParse` are structural only; `toJsonSchema` relies on that when it matches
|
|
148
|
+
* union branches (see assertPublicDefault).
|
|
149
|
+
*/
|
|
122
150
|
min(value) {
|
|
123
151
|
this.meta.min = value;
|
|
124
152
|
return this;
|
|
@@ -366,8 +394,12 @@ Schema.extend('const', function (key, value) {
|
|
|
366
394
|
return value;
|
|
367
395
|
});
|
|
368
396
|
Schema.extend('tuple', function (key, value) {
|
|
369
|
-
|
|
370
|
-
|
|
397
|
+
// No `[]` fallback: an empty array is not a valid tuple (JSON Schema exports
|
|
398
|
+
// minItems = maxItems = length, and safeParse rejects it), so absent stays absent.
|
|
399
|
+
value = Schema.checkDefault(this, key, value);
|
|
400
|
+
if (value === undefined)
|
|
401
|
+
return value;
|
|
402
|
+
if (value.length !== this.options.list.length)
|
|
371
403
|
throw new TypeError(`${key} tuple length mismatch`);
|
|
372
404
|
return value.map((item, index) => this.options.list[index](item, `${key}[${index}]`));
|
|
373
405
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zhin.js/schema",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.6",
|
|
4
4
|
"description": "Configuration validation and schema system for Zhin.js",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./lib/index.js",
|
|
@@ -14,6 +14,10 @@
|
|
|
14
14
|
"files": [
|
|
15
15
|
"lib"
|
|
16
16
|
],
|
|
17
|
+
"engines": {
|
|
18
|
+
"node": ">=22.19.0"
|
|
19
|
+
},
|
|
20
|
+
"license": "MIT",
|
|
17
21
|
"devDependencies": {
|
|
18
22
|
"@types/node": "^26.1.2",
|
|
19
23
|
"typescript": "^6.0.3"
|