@flowgram.ai/json-schema 0.3.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/dist/esm/index.js +959 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/index.d.mts +271 -0
- package/dist/index.d.ts +271 -0
- package/dist/index.js +995 -0
- package/dist/index.js.map +1 -0
- package/package.json +65 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
import * as _flowgram_ai_utils from '@flowgram.ai/utils';
|
|
2
|
+
import { Emitter } from '@flowgram.ai/utils';
|
|
3
|
+
import React$1 from 'react';
|
|
4
|
+
import { ContainerModule } from 'inversify';
|
|
5
|
+
import { ASTNodeJSON, ASTNode, BaseType } from '@flowgram.ai/variable-core';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
|
|
9
|
+
* SPDX-License-Identifier: MIT
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Base information for TypeRegistry
|
|
14
|
+
*/
|
|
15
|
+
interface BaseTypeRegistry {
|
|
16
|
+
/**
|
|
17
|
+
* type reference
|
|
18
|
+
*/
|
|
19
|
+
type: string;
|
|
20
|
+
/**
|
|
21
|
+
* The inherited type. If there is an inheritance, the definition of this type will use the inherited type definition,
|
|
22
|
+
* and the new definition will override the inherited definition.
|
|
23
|
+
*/
|
|
24
|
+
extend?: string;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* TypeRegistryCreator
|
|
28
|
+
*/
|
|
29
|
+
type TypeRegistryCreator<Schema, Registry extends BaseTypeRegistry, Manager extends BaseTypeManager<Schema, Registry, Manager>> = (ctx: {
|
|
30
|
+
typeManager: Manager;
|
|
31
|
+
}) => Partial<Registry>;
|
|
32
|
+
|
|
33
|
+
declare abstract class BaseTypeManager<Schema, Registry extends BaseTypeRegistry, Manager extends BaseTypeManager<Schema, Registry, Manager>> {
|
|
34
|
+
protected typeRegistryMap: Map<string, Registry>;
|
|
35
|
+
protected onTypeRegistryChangeEmitter: Emitter<Registry[]>;
|
|
36
|
+
onTypeRegistryChange: _flowgram_ai_utils.Event<Registry[]>;
|
|
37
|
+
/**
|
|
38
|
+
* 获取 typeSchema 对应的 type
|
|
39
|
+
* 不能直接访问 type 的原因的是
|
|
40
|
+
* Schema 中 type 可能为空,需要获取 $ref 中的 type
|
|
41
|
+
*/
|
|
42
|
+
protected abstract getTypeNameFromSchema(typeSchema: Schema): string;
|
|
43
|
+
getTypeByName(typeName: string): Registry | undefined;
|
|
44
|
+
getTypeBySchema(type: Schema): Registry | undefined;
|
|
45
|
+
getDefaultTypeRegistry(): Registry | undefined;
|
|
46
|
+
/**
|
|
47
|
+
* 注册 TypeRegistry
|
|
48
|
+
*/
|
|
49
|
+
register(originRegistry: Partial<Registry> | TypeRegistryCreator<Schema, Registry, Manager>): void;
|
|
50
|
+
triggerChanges(): void;
|
|
51
|
+
/**
|
|
52
|
+
* 获取全量的 TypeRegistries
|
|
53
|
+
*/
|
|
54
|
+
getAllTypeRegistries(): Registry[];
|
|
55
|
+
unregister(type: string): void;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
|
|
60
|
+
* SPDX-License-Identifier: MIT
|
|
61
|
+
*/
|
|
62
|
+
|
|
63
|
+
declare class JsonSchemaTypeManager<Schema extends Partial<IJsonSchema> = IJsonSchema, Registry extends JsonSchemaTypeRegistry<Schema> = JsonSchemaTypeRegistry<Schema>> extends BaseTypeManager<Schema, Registry, JsonSchemaTypeManager<Schema, Registry>> {
|
|
64
|
+
/**
|
|
65
|
+
* get type name
|
|
66
|
+
* @param typeSchema
|
|
67
|
+
* @returns
|
|
68
|
+
*/
|
|
69
|
+
protected getTypeNameFromSchema(typeSchema: Schema): string;
|
|
70
|
+
constructor();
|
|
71
|
+
/**
|
|
72
|
+
* Get TypeRegistries based on the current parentType
|
|
73
|
+
*/
|
|
74
|
+
getTypeRegistriesWithParentType: (parentType?: string) => Registry[];
|
|
75
|
+
/**
|
|
76
|
+
* Get the deepest child field of a field
|
|
77
|
+
* Array<Array<String>> -> String
|
|
78
|
+
*/
|
|
79
|
+
getTypeSchemaDeepChildField: (type: Schema) => Schema;
|
|
80
|
+
/**
|
|
81
|
+
* Get the plain text display string of the type schema, for example:
|
|
82
|
+
* Array<Array<String>>, Map<String, Number>
|
|
83
|
+
*/
|
|
84
|
+
getComplexText: (type: Schema) => string;
|
|
85
|
+
getDisplayIcon: (type: Schema) => React$1.JSX.Element;
|
|
86
|
+
getTypeSchemaProperties: (type: Schema) => Record<string, Schema> | undefined;
|
|
87
|
+
getJsonPaths: (type: Schema) => string[] | undefined;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
|
|
92
|
+
* SPDX-License-Identifier: MIT
|
|
93
|
+
*/
|
|
94
|
+
|
|
95
|
+
type JsonSchemaBasicType = 'boolean' | 'string' | 'integer' | 'number' | 'object' | 'array' | 'map';
|
|
96
|
+
interface IJsonSchema<T = string> {
|
|
97
|
+
type?: T;
|
|
98
|
+
default?: any;
|
|
99
|
+
title?: string;
|
|
100
|
+
description?: string;
|
|
101
|
+
enum?: (string | number)[];
|
|
102
|
+
properties?: Record<string, IJsonSchema<T>>;
|
|
103
|
+
additionalProperties?: IJsonSchema<T>;
|
|
104
|
+
items?: IJsonSchema<T>;
|
|
105
|
+
required?: string[];
|
|
106
|
+
$ref?: string;
|
|
107
|
+
extra?: {
|
|
108
|
+
index?: number;
|
|
109
|
+
weak?: boolean;
|
|
110
|
+
formComponent?: string;
|
|
111
|
+
[key: string]: any;
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
type IBasicJsonSchema = IJsonSchema<JsonSchemaBasicType>;
|
|
115
|
+
/**
|
|
116
|
+
* TypeRegistry based on IJsonSchema
|
|
117
|
+
*/
|
|
118
|
+
interface JsonSchemaTypeRegistry<Schema extends Partial<IJsonSchema> = IJsonSchema> extends BaseTypeRegistry {
|
|
119
|
+
/**
|
|
120
|
+
* The icon of this type
|
|
121
|
+
*/
|
|
122
|
+
icon: React.JSX.Element;
|
|
123
|
+
/**
|
|
124
|
+
* The display text of this type, not including the icon
|
|
125
|
+
*/
|
|
126
|
+
label: string;
|
|
127
|
+
/**
|
|
128
|
+
* Whether it is a container type
|
|
129
|
+
*/
|
|
130
|
+
container: boolean;
|
|
131
|
+
/**
|
|
132
|
+
* Supported parent types. Some types can only appear as subtypes in type selection, but not as basic types.
|
|
133
|
+
*/
|
|
134
|
+
parentType?: string[];
|
|
135
|
+
getSupportedItemTypes?: (ctx: {
|
|
136
|
+
level: number;
|
|
137
|
+
parentTypes?: string[];
|
|
138
|
+
}) => Array<{
|
|
139
|
+
type: string;
|
|
140
|
+
disabled?: string;
|
|
141
|
+
}>;
|
|
142
|
+
/**
|
|
143
|
+
* Get the display label
|
|
144
|
+
*/
|
|
145
|
+
getDisplayLabel: (typeSchema: Schema) => React.JSX.Element;
|
|
146
|
+
/**
|
|
147
|
+
* Get the display text
|
|
148
|
+
*/
|
|
149
|
+
getDisplayText: (typeSchema: Schema) => string | undefined;
|
|
150
|
+
/**
|
|
151
|
+
* Get the sub-type
|
|
152
|
+
*/
|
|
153
|
+
getItemType?: (typeSchema: Schema) => Schema | undefined;
|
|
154
|
+
/**
|
|
155
|
+
* Generate default Schema
|
|
156
|
+
*/
|
|
157
|
+
getDefaultSchema: () => Schema;
|
|
158
|
+
/**
|
|
159
|
+
* onInit initialization logic, which is called at the appropriate time externally to register data into the type system
|
|
160
|
+
*/
|
|
161
|
+
onInit?: () => void;
|
|
162
|
+
/**
|
|
163
|
+
* Whether to allow adding fields, such as object
|
|
164
|
+
*/
|
|
165
|
+
canAddField: (typeSchema: Schema) => boolean;
|
|
166
|
+
/**
|
|
167
|
+
* Get the string value, for example
|
|
168
|
+
* { type: "array", items: { type: "string" } }
|
|
169
|
+
* The value is "array-string"
|
|
170
|
+
*
|
|
171
|
+
* The use case is that in some UI components, a string value needs to be generated
|
|
172
|
+
*/
|
|
173
|
+
getStringValueByTypeSchema?: (optionValue: Schema) => string | undefined;
|
|
174
|
+
/**
|
|
175
|
+
* Restore the string value to typeSchema
|
|
176
|
+
* "array-string" is restored to
|
|
177
|
+
* { type: "array", items: { type: "string" } }
|
|
178
|
+
*/
|
|
179
|
+
getTypeSchemaByStringValue?: (type: string) => Schema;
|
|
180
|
+
/**
|
|
181
|
+
* Get the display icon, and the composite icon of the array is also processed
|
|
182
|
+
*/
|
|
183
|
+
getDisplayIcon: (typeSchema: Schema) => JSX.Element;
|
|
184
|
+
/**
|
|
185
|
+
* Get sub-properties
|
|
186
|
+
*/
|
|
187
|
+
getTypeSchemaProperties: (typeSchema: Schema) => Record<string, Schema> | undefined;
|
|
188
|
+
/**
|
|
189
|
+
* Get the default value
|
|
190
|
+
*/
|
|
191
|
+
getDefaultValue: () => unknown;
|
|
192
|
+
/**
|
|
193
|
+
* Get the display text based on the value
|
|
194
|
+
*/
|
|
195
|
+
getValueText: (value?: unknown) => string;
|
|
196
|
+
/**
|
|
197
|
+
* Get the json path of a certain type in the flow schema
|
|
198
|
+
* For example
|
|
199
|
+
* { type: "object", properties: { name: { type: "string" } } }
|
|
200
|
+
* -> ['properties', 'name']
|
|
201
|
+
* { type: "array", items: { type: "string" } }
|
|
202
|
+
* ->['items']
|
|
203
|
+
*/
|
|
204
|
+
getJsonPaths: (typeSchema: Schema) => string[];
|
|
205
|
+
/**
|
|
206
|
+
* Get the parent node of the sub-field
|
|
207
|
+
* object is itself
|
|
208
|
+
* array<object> is items
|
|
209
|
+
* map<object> is additionalProperties
|
|
210
|
+
*/
|
|
211
|
+
getPropertiesParent: (typeSchema: Schema) => Schema | undefined;
|
|
212
|
+
/**
|
|
213
|
+
* The complexText of a custom type
|
|
214
|
+
* For example, Array<string>, can be modified
|
|
215
|
+
*/
|
|
216
|
+
customComplexText?: (typeSchema: Schema) => string;
|
|
217
|
+
}
|
|
218
|
+
type JsonSchemaTypeRegistryCreator<Schema extends Partial<IJsonSchema> = IJsonSchema, Registry extends JsonSchemaTypeRegistry<Schema> = JsonSchemaTypeRegistry<Schema>, Manager extends JsonSchemaTypeManager<Schema, Registry> = JsonSchemaTypeManager<Schema, Registry>> = TypeRegistryCreator<Schema, Registry, Manager>;
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
|
|
222
|
+
* SPDX-License-Identifier: MIT
|
|
223
|
+
*/
|
|
224
|
+
|
|
225
|
+
declare namespace JsonSchemaUtils {
|
|
226
|
+
/**
|
|
227
|
+
* Converts a JSON schema to an Abstract Syntax Tree (AST) representation.
|
|
228
|
+
* This function recursively processes the JSON schema and creates corresponding AST nodes.
|
|
229
|
+
*
|
|
230
|
+
* For more information on JSON Schema, refer to the official documentation:
|
|
231
|
+
* https://json-schema.org/
|
|
232
|
+
*
|
|
233
|
+
* @param jsonSchema - The JSON schema to convert.
|
|
234
|
+
* @returns An AST node representing the JSON schema, or undefined if the schema type is not recognized.
|
|
235
|
+
*/
|
|
236
|
+
function schemaToAST(jsonSchema: IJsonSchema): ASTNodeJSON | undefined;
|
|
237
|
+
/**
|
|
238
|
+
* Convert AST To JSON Schema
|
|
239
|
+
* @param typeAST
|
|
240
|
+
* @returns
|
|
241
|
+
*/
|
|
242
|
+
function astToSchema(typeAST?: ASTNode, options?: {
|
|
243
|
+
drilldown?: boolean;
|
|
244
|
+
drilldownObject?: boolean;
|
|
245
|
+
drilldownMap?: boolean;
|
|
246
|
+
drilldownArray?: boolean;
|
|
247
|
+
}): IJsonSchema | undefined;
|
|
248
|
+
/**
|
|
249
|
+
* Check if the AST type is match the JSON Schema
|
|
250
|
+
* @param typeAST
|
|
251
|
+
* @param schema
|
|
252
|
+
* @returns
|
|
253
|
+
*/
|
|
254
|
+
function isASTMatchSchema(typeAST: BaseType, schema: IJsonSchema | IJsonSchema[]): boolean;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
declare const jsonSchemaTypeManager: JsonSchemaTypeManager<IJsonSchema<string>, JsonSchemaTypeRegistry<IJsonSchema<string>>>;
|
|
258
|
+
|
|
259
|
+
declare const useTypeManager: () => JsonSchemaTypeManager<IJsonSchema<string>, JsonSchemaTypeRegistry<IJsonSchema<string>>> | BaseTypeManager<unknown, BaseTypeRegistry, BaseTypeManager<unknown, BaseTypeRegistry, BaseTypeManager<unknown, BaseTypeRegistry, BaseTypeManager<any, any, any>>>>;
|
|
260
|
+
declare const TypePresetProvider: <Registry extends JsonSchemaTypeRegistry = JsonSchemaTypeRegistry>({ children, types, }: React$1.PropsWithChildren<{
|
|
261
|
+
types: (Partial<Registry> | TypeRegistryCreator<IJsonSchema, Registry, JsonSchemaTypeManager<IJsonSchema, Registry>>)[];
|
|
262
|
+
}>) => React$1.JSX.Element;
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
|
|
266
|
+
* SPDX-License-Identifier: MIT
|
|
267
|
+
*/
|
|
268
|
+
|
|
269
|
+
declare const jsonSchemaContainerModule: ContainerModule;
|
|
270
|
+
|
|
271
|
+
export { BaseTypeManager, type BaseTypeRegistry, type IBasicJsonSchema, type IJsonSchema, type JsonSchemaBasicType, JsonSchemaTypeManager, type JsonSchemaTypeRegistry, type JsonSchemaTypeRegistryCreator, JsonSchemaUtils, TypePresetProvider, type TypeRegistryCreator, jsonSchemaContainerModule, jsonSchemaTypeManager, useTypeManager };
|