@yimoka/designable-playground 0.0.0-stage → 0.1.1
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 +8 -2
- package/dist/demo/components/Alert/index.d.ts +4 -0
- package/dist/demo/components/Alert/index.js +41 -0
- package/dist/demo/components/ArrayCards/index.d.ts +17 -0
- package/dist/demo/components/ArrayCards/index.js +62 -0
- package/dist/demo/components/ArrayList/index.d.ts +25 -0
- package/dist/demo/components/ArrayList/index.js +143 -0
- package/dist/demo/components/ArrayTable/index.d.ts +39 -0
- package/dist/demo/components/ArrayTable/index.js +247 -0
- package/dist/demo/components/AutoComplete/index.d.ts +4 -0
- package/dist/demo/components/AutoComplete/index.js +52 -0
- package/dist/demo/components/Avatar/index.d.ts +4 -0
- package/dist/demo/components/Avatar/index.js +34 -0
- package/dist/demo/components/Badge/index.d.ts +4 -0
- package/dist/demo/components/Badge/index.js +35 -0
- package/dist/demo/components/Button/index.d.ts +4 -0
- package/dist/demo/components/Button/index.js +86 -0
- package/dist/demo/components/Card/index.d.ts +4 -0
- package/dist/demo/components/Card/index.js +72 -0
- package/dist/demo/components/Carousel/index.d.ts +4 -0
- package/dist/demo/components/Carousel/index.js +33 -0
- package/dist/demo/components/Cascader/index.d.ts +4 -0
- package/dist/demo/components/Cascader/index.js +60 -0
- package/dist/demo/components/Checkbox/index.d.ts +8 -0
- package/dist/demo/components/Checkbox/index.js +55 -0
- package/dist/demo/components/ColorPicker/index.d.ts +4 -0
- package/dist/demo/components/ColorPicker/index.js +38 -0
- package/dist/demo/components/DatePicker/index.d.ts +4 -0
- package/dist/demo/components/DatePicker/index.js +61 -0
- package/dist/demo/components/DateRangePicker/index.d.ts +4 -0
- package/dist/demo/components/DateRangePicker/index.js +58 -0
- package/dist/demo/components/Divider/index.d.ts +4 -0
- package/dist/demo/components/Divider/index.js +39 -0
- package/dist/demo/components/Empty/index.d.ts +4 -0
- package/dist/demo/components/Empty/index.js +29 -0
- package/dist/demo/components/Flex/index.d.ts +4 -0
- package/dist/demo/components/Flex/index.js +51 -0
- package/dist/demo/components/Form/index.d.ts +21 -0
- package/dist/demo/components/Form/index.js +116 -0
- package/dist/demo/components/FormCollapse/index.d.ts +28 -0
- package/dist/demo/components/FormCollapse/index.js +164 -0
- package/dist/demo/components/FormGrid/index.d.ts +22 -0
- package/dist/demo/components/FormGrid/index.js +134 -0
- package/dist/demo/components/FormItem/index.d.ts +14 -0
- package/dist/demo/components/FormItem/index.js +33 -0
- package/dist/demo/components/FormLayout/index.d.ts +26 -0
- package/dist/demo/components/FormLayout/index.js +260 -0
- package/dist/demo/components/FormTab/index.d.ts +15 -0
- package/dist/demo/components/FormTab/index.js +186 -0
- package/dist/demo/components/GridCol/index.d.ts +4 -0
- package/dist/demo/components/GridCol/index.js +53 -0
- package/dist/demo/components/GridRow/index.d.ts +4 -0
- package/dist/demo/components/GridRow/index.js +70 -0
- package/dist/demo/components/HTMLContent/index.d.ts +4 -0
- package/dist/demo/components/HTMLContent/index.js +34 -0
- package/dist/demo/components/I18nEditor/index.d.ts +8 -0
- package/dist/demo/components/I18nEditor/index.js +64 -0
- package/dist/demo/components/Image/index.d.ts +4 -0
- package/dist/demo/components/Image/index.js +36 -0
- package/dist/demo/components/Input/index.d.ts +4 -0
- package/dist/demo/components/Input/index.js +36 -0
- package/dist/demo/components/InputSearch/index.d.ts +4 -0
- package/dist/demo/components/InputSearch/index.js +40 -0
- package/dist/demo/components/Link/index.d.ts +4 -0
- package/dist/demo/components/Link/index.js +37 -0
- package/dist/demo/components/Mentions/index.d.ts +4 -0
- package/dist/demo/components/Mentions/index.js +41 -0
- package/dist/demo/components/NumberPicker/index.d.ts +6 -0
- package/dist/demo/components/NumberPicker/index.js +63 -0
- package/dist/demo/components/ObjectContainer/index.d.ts +14 -0
- package/dist/demo/components/ObjectContainer/index.js +46 -0
- package/dist/demo/components/Password/index.d.ts +6 -0
- package/dist/demo/components/Password/index.js +44 -0
- package/dist/demo/components/QRCode/index.d.ts +4 -0
- package/dist/demo/components/QRCode/index.js +34 -0
- package/dist/demo/components/Radio/index.d.ts +6 -0
- package/dist/demo/components/Radio/index.js +49 -0
- package/dist/demo/components/Rate/index.d.ts +6 -0
- package/dist/demo/components/Rate/index.js +43 -0
- package/dist/demo/components/Search/index.d.ts +4 -0
- package/dist/demo/components/Search/index.js +42 -0
- package/dist/demo/components/SearchSelect/index.d.ts +4 -0
- package/dist/demo/components/SearchSelect/index.js +53 -0
- package/dist/demo/components/Select/index.d.ts +4 -0
- package/dist/demo/components/Select/index.js +59 -0
- package/dist/demo/components/Slider/index.d.ts +6 -0
- package/dist/demo/components/Slider/index.js +61 -0
- package/dist/demo/components/Space/index.d.ts +9 -0
- package/dist/demo/components/Space/index.js +61 -0
- package/dist/demo/components/Statistic/index.d.ts +4 -0
- package/dist/demo/components/Statistic/index.js +33 -0
- package/dist/demo/components/Switch/index.d.ts +4 -0
- package/dist/demo/components/Switch/index.js +49 -0
- package/dist/demo/components/Tabs/index.d.ts +10 -0
- package/dist/demo/components/Tabs/index.js +181 -0
- package/dist/demo/components/Tag/index.d.ts +4 -0
- package/dist/demo/components/Tag/index.js +33 -0
- package/dist/demo/components/Text/index.d.ts +11 -0
- package/dist/demo/components/Text/index.js +64 -0
- package/dist/demo/components/TextArea/index.d.ts +6 -0
- package/dist/demo/components/TextArea/index.js +43 -0
- package/dist/demo/components/TimePicker/index.d.ts +4 -0
- package/dist/demo/components/TimePicker/index.js +69 -0
- package/dist/demo/components/TimeRangePicker/index.d.ts +4 -0
- package/dist/demo/components/TimeRangePicker/index.js +68 -0
- package/dist/demo/components/Transfer/index.d.ts +6 -0
- package/dist/demo/components/Transfer/index.js +59 -0
- package/dist/demo/components/TreeSelect/index.d.ts +4 -0
- package/dist/demo/components/TreeSelect/index.js +63 -0
- package/dist/demo/components/Upload/index.d.ts +8 -0
- package/dist/demo/components/Upload/index.js +64 -0
- package/dist/demo/components/index.d.ts +55 -0
- package/dist/demo/components/index.js +55 -0
- package/dist/demo/components/shared.d.ts +143 -0
- package/dist/demo/components/shared.js +315 -0
- package/dist/src/Playground.d.ts +3 -0
- package/dist/src/Playground.js +304 -0
- package/dist/src/antd/AntdPlayground.d.ts +16 -0
- package/dist/src/antd/AntdPlayground.js +136 -0
- package/dist/src/antd/index.d.ts +1 -0
- package/dist/src/antd/index.js +1 -0
- package/dist/src/context.d.ts +4 -0
- package/dist/src/context.js +13 -0
- package/dist/src/examples/index.d.ts +267 -0
- package/dist/src/examples/index.js +824 -0
- package/dist/src/hooks/index.d.ts +4 -0
- package/dist/src/hooks/index.js +4 -0
- package/dist/src/hooks/remoteSchemaAdapter.d.ts +53 -0
- package/dist/src/hooks/remoteSchemaAdapter.js +262 -0
- package/dist/src/hooks/usePlayground.d.ts +38 -0
- package/dist/src/hooks/usePlayground.js +642 -0
- package/dist/src/hooks/usePlaygroundRemotePersistence.d.ts +135 -0
- package/dist/src/hooks/usePlaygroundRemotePersistence.js +736 -0
- package/dist/src/hooks/useSchemaEditorDraft.d.ts +4 -0
- package/dist/src/hooks/useSchemaEditorDraft.js +44 -0
- package/dist/src/index.d.ts +14 -0
- package/dist/src/index.js +14 -0
- package/dist/src/layouts/PlaygroundLayout/ErrorBoundaries.d.ts +59 -0
- package/dist/src/layouts/PlaygroundLayout/ErrorBoundaries.js +161 -0
- package/dist/src/layouts/PlaygroundLayout/index.d.ts +7 -0
- package/dist/src/layouts/PlaygroundLayout/index.js +443 -0
- package/dist/src/layouts/index.d.ts +1 -0
- package/dist/src/layouts/index.js +1 -0
- package/dist/src/panels/CompositePanel/index.d.ts +6 -0
- package/dist/src/panels/CompositePanel/index.js +139 -0
- package/dist/src/panels/ToolbarPanel/index.d.ts +3 -0
- package/dist/src/panels/ToolbarPanel/index.js +24 -0
- package/dist/src/panels/index.d.ts +2 -0
- package/dist/src/panels/index.js +2 -0
- package/dist/src/presets/antd.d.ts +19 -0
- package/dist/src/presets/antd.js +112 -0
- package/dist/src/presets/index.d.ts +4 -0
- package/dist/src/presets/index.js +3 -0
- package/dist/src/presets/runtime-array-helpers.d.ts +8 -0
- package/dist/src/presets/runtime-array-helpers.js +147 -0
- package/dist/src/presets/runtime-component-props.d.ts +3 -0
- package/dist/src/presets/runtime-component-props.js +5 -0
- package/dist/src/presets/runtime-components.d.ts +5 -0
- package/dist/src/presets/runtime-components.js +419 -0
- package/dist/src/presets/runtime-form-components.d.ts +9 -0
- package/dist/src/presets/runtime-form-components.js +94 -0
- package/dist/src/presets/scenario-catalog.d.ts +6 -0
- package/dist/src/presets/scenario-catalog.js +128 -0
- package/dist/src/presets/scenarios.d.ts +52 -0
- package/dist/src/presets/scenarios.js +603 -0
- package/dist/src/runtime/actions.d.ts +19 -0
- package/dist/src/runtime/actions.js +307 -0
- package/dist/src/runtime/contracts.d.ts +149 -0
- package/dist/src/runtime/contracts.js +1 -0
- package/dist/src/runtime/document-bindings.d.ts +3 -0
- package/dist/src/runtime/document-bindings.js +101 -0
- package/dist/src/runtime/document-core.d.ts +57 -0
- package/dist/src/runtime/document-core.js +379 -0
- package/dist/src/runtime/document-entity.d.ts +12 -0
- package/dist/src/runtime/document-entity.js +351 -0
- package/dist/src/runtime/document-expressions.d.ts +35 -0
- package/dist/src/runtime/document-expressions.js +346 -0
- package/dist/src/runtime/document-security.d.ts +34 -0
- package/dist/src/runtime/document-security.js +538 -0
- package/dist/src/runtime/document.d.ts +16 -0
- package/dist/src/runtime/document.js +212 -0
- package/dist/src/runtime/environment.d.ts +16 -0
- package/dist/src/runtime/environment.js +76 -0
- package/dist/src/runtime/i18n-schema.d.ts +36 -0
- package/dist/src/runtime/i18n-schema.js +338 -0
- package/dist/src/runtime/i18n.d.ts +25 -0
- package/dist/src/runtime/i18n.js +447 -0
- package/dist/src/runtime/index.d.ts +8 -0
- package/dist/src/runtime/index.js +8 -0
- package/dist/src/runtime/navigation.d.ts +5 -0
- package/dist/src/runtime/navigation.js +43 -0
- package/dist/src/runtime/playground.d.ts +30 -0
- package/dist/src/runtime/playground.js +243 -0
- package/dist/src/runtime/preview-boundaries.d.ts +13 -0
- package/dist/src/runtime/preview-boundaries.js +43 -0
- package/dist/src/runtime/preview.d.ts +44 -0
- package/dist/src/runtime/preview.js +574 -0
- package/dist/src/runtime/settings/RuntimeJsonEditor.d.ts +10 -0
- package/dist/src/runtime/settings/RuntimeJsonEditor.js +21 -0
- package/dist/src/runtime/settings/RuntimeStoreEntitySettings.d.ts +15 -0
- package/dist/src/runtime/settings/RuntimeStoreEntitySettings.js +65 -0
- package/dist/src/runtime/templates.d.ts +1560 -0
- package/dist/src/runtime/templates.js +508 -0
- package/dist/src/runtime/website-contact-template.d.ts +1 -0
- package/dist/src/runtime/website-contact-template.js +66 -0
- package/dist/src/runtime/website-template-shared.d.ts +14 -0
- package/dist/src/runtime/website-template-shared.js +210 -0
- package/dist/src/runtime/website-templates.d.ts +5 -0
- package/dist/src/runtime/website-templates.js +976 -0
- package/dist/src/shared/clipboard.d.ts +4 -0
- package/dist/src/shared/clipboard.js +56 -0
- package/dist/src/shared/componentRegistry.d.ts +133 -0
- package/dist/src/shared/componentRegistry.js +381 -0
- package/dist/src/shared/formilyCompatibility.d.ts +8 -0
- package/dist/src/shared/formilyCompatibility.js +40 -0
- package/dist/src/shared/index.d.ts +6 -0
- package/dist/src/shared/index.js +6 -0
- package/dist/src/shared/productionAcceptance.d.ts +63 -0
- package/dist/src/shared/productionAcceptance.js +201 -0
- package/dist/src/shared/schema-options.d.ts +16 -0
- package/dist/src/shared/schema-options.js +138 -0
- package/dist/src/shared/schema.d.ts +63 -0
- package/dist/src/shared/schema.js +627 -0
- package/dist/src/shared/styleSecurity.d.ts +2 -0
- package/dist/src/shared/styleSecurity.js +35 -0
- package/dist/src/transformers/formily.d.ts +34 -0
- package/dist/src/transformers/formily.js +943 -0
- package/dist/src/transformers/index.d.ts +1 -0
- package/dist/src/transformers/index.js +1 -0
- package/dist/src/types.d.ts +186 -0
- package/dist/src/types.js +1 -0
- package/dist/src/widgets/ActionsWidget/index.d.ts +3 -0
- package/dist/src/widgets/ActionsWidget/index.js +10 -0
- package/dist/src/widgets/ComponentTreeWidget/index.d.ts +3 -0
- package/dist/src/widgets/ComponentTreeWidget/index.js +333 -0
- package/dist/src/widgets/NodeActionsWidget/index.d.ts +2 -0
- package/dist/src/widgets/NodeActionsWidget/index.js +125 -0
- package/dist/src/widgets/OutlineTreeWidget/index.d.ts +3 -0
- package/dist/src/widgets/OutlineTreeWidget/index.js +432 -0
- package/dist/src/widgets/OutlineTreeWidget/shared.d.ts +32 -0
- package/dist/src/widgets/OutlineTreeWidget/shared.js +79 -0
- package/dist/src/widgets/PreviewWidget/index.d.ts +2 -0
- package/dist/src/widgets/PreviewWidget/index.js +1566 -0
- package/dist/src/widgets/ProductionReadinessWidget/index.d.ts +18 -0
- package/dist/src/widgets/ProductionReadinessWidget/index.js +356 -0
- package/dist/src/widgets/RemotePersistenceStatusWidget/index.d.ts +18 -0
- package/dist/src/widgets/RemotePersistenceStatusWidget/index.js +438 -0
- package/dist/src/widgets/RemoteVersionHistoryWidget/index.d.ts +39 -0
- package/dist/src/widgets/RemoteVersionHistoryWidget/index.js +475 -0
- package/dist/src/widgets/ResourceWidget/index.d.ts +3 -0
- package/dist/src/widgets/ResourceWidget/index.js +491 -0
- package/dist/src/widgets/SchemaCodeView/index.d.ts +7 -0
- package/dist/src/widgets/SchemaCodeView/index.js +225 -0
- package/dist/src/widgets/SchemaEditorWidget/index.d.ts +10 -0
- package/dist/src/widgets/SchemaEditorWidget/index.js +484 -0
- package/dist/src/widgets/SettingsPanelWidget/index.d.ts +3 -0
- package/dist/src/widgets/SettingsPanelWidget/index.js +190 -0
- package/dist/src/widgets/StorageActionsWidget/index.d.ts +2 -0
- package/dist/src/widgets/StorageActionsWidget/index.js +284 -0
- package/dist/src/widgets/index.d.ts +13 -0
- package/dist/src/widgets/index.js +13 -0
- package/package.json +100 -4
|
@@ -0,0 +1,627 @@
|
|
|
1
|
+
const isRecord = (value) => (typeof value === 'object'
|
|
2
|
+
&& value !== null
|
|
3
|
+
&& !Array.isArray(value));
|
|
4
|
+
export const FORMILY_SCHEMA_RECORD_CHILD_KEYS = [
|
|
5
|
+
'$defs',
|
|
6
|
+
'definitions',
|
|
7
|
+
'dependentSchemas',
|
|
8
|
+
'patternProperties',
|
|
9
|
+
'properties',
|
|
10
|
+
'x-additional-schema',
|
|
11
|
+
];
|
|
12
|
+
export const FORMILY_SCHEMA_SINGLE_CHILD_KEYS = [
|
|
13
|
+
'additionalItems',
|
|
14
|
+
'additionalProperties',
|
|
15
|
+
'contains',
|
|
16
|
+
'else',
|
|
17
|
+
'if',
|
|
18
|
+
'not',
|
|
19
|
+
'then',
|
|
20
|
+
];
|
|
21
|
+
export const FORMILY_SCHEMA_ARRAY_CHILD_KEYS = [
|
|
22
|
+
'allOf',
|
|
23
|
+
'anyOf',
|
|
24
|
+
'oneOf',
|
|
25
|
+
'prefixItems',
|
|
26
|
+
];
|
|
27
|
+
export const visitFormilySchemaNodes = (value, path, visitor) => {
|
|
28
|
+
if (!isRecord(value))
|
|
29
|
+
return;
|
|
30
|
+
visitor({
|
|
31
|
+
node: value,
|
|
32
|
+
path,
|
|
33
|
+
});
|
|
34
|
+
FORMILY_SCHEMA_RECORD_CHILD_KEYS.forEach((key) => {
|
|
35
|
+
const children = value[key];
|
|
36
|
+
if (!isRecord(children))
|
|
37
|
+
return;
|
|
38
|
+
Object.entries(children).forEach(([childKey, child]) => {
|
|
39
|
+
visitFormilySchemaNodes(child, `${path}.${key}.${childKey}`, visitor);
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
FORMILY_SCHEMA_SINGLE_CHILD_KEYS.forEach((key) => {
|
|
43
|
+
const child = value[key];
|
|
44
|
+
if (isRecord(child)) {
|
|
45
|
+
visitFormilySchemaNodes(child, `${path}.${key}`, visitor);
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
FORMILY_SCHEMA_ARRAY_CHILD_KEYS.forEach((key) => {
|
|
49
|
+
const children = value[key];
|
|
50
|
+
if (!Array.isArray(children))
|
|
51
|
+
return;
|
|
52
|
+
children.forEach((child, index) => {
|
|
53
|
+
visitFormilySchemaNodes(child, `${path}.${key}[${index}]`, visitor);
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
const { items } = value;
|
|
57
|
+
if (Array.isArray(items)) {
|
|
58
|
+
items.forEach((item, index) => {
|
|
59
|
+
visitFormilySchemaNodes(item, `${path}.items[${index}]`, visitor);
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
else if (isRecord(items)) {
|
|
63
|
+
visitFormilySchemaNodes(items, `${path}.items`, visitor);
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
export const CURRENT_DESIGNABLE_SCHEMA_VERSION = 1;
|
|
67
|
+
export const DESIGNABLE_SCHEMA_VERSION_PROP = 'x-designable-schema-version';
|
|
68
|
+
export const RUNTIME_DOCUMENT_SIDECAR_PROP = 'x-designable-runtime-sidecar-v1';
|
|
69
|
+
export const UNSAFE_SCHEMA_OBJECT_KEYS = new Set([
|
|
70
|
+
'__proto__',
|
|
71
|
+
'constructor',
|
|
72
|
+
'prototype',
|
|
73
|
+
]);
|
|
74
|
+
export const isSafeSchemaObjectKey = (key) => !UNSAFE_SCHEMA_OBJECT_KEYS.has(key);
|
|
75
|
+
export const decodeJsonPointerSegment = (segment) => {
|
|
76
|
+
try {
|
|
77
|
+
const decoded = decodeURIComponent(segment);
|
|
78
|
+
if (/~(?:[^01]|$)/u.test(decoded))
|
|
79
|
+
return undefined;
|
|
80
|
+
return decoded.replace(/~1/gu, '/').replace(/~0/gu, '~');
|
|
81
|
+
}
|
|
82
|
+
catch {
|
|
83
|
+
return undefined;
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
export const assertSafeSchemaObjectKeys = (value, path = 'Schema') => {
|
|
87
|
+
if (Array.isArray(value)) {
|
|
88
|
+
value.forEach((item, index) => {
|
|
89
|
+
assertSafeSchemaObjectKeys(item, `${path}.${index}`);
|
|
90
|
+
});
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
if (!isRecord(value))
|
|
94
|
+
return;
|
|
95
|
+
Object.entries(value).forEach(([key, item]) => {
|
|
96
|
+
const nextPath = `${path}.${key}`;
|
|
97
|
+
if (!isSafeSchemaObjectKey(key)) {
|
|
98
|
+
throw new Error(`Schema 包含不安全字段路径:${nextPath}`);
|
|
99
|
+
}
|
|
100
|
+
assertSafeSchemaObjectKeys(item, nextPath);
|
|
101
|
+
});
|
|
102
|
+
};
|
|
103
|
+
export const isUnsafeSchemaObjectKeyError = (error) => (error instanceof Error
|
|
104
|
+
&& error.message.startsWith('Schema 包含不安全字段路径:'));
|
|
105
|
+
export const parseSchemaJSON = (text) => {
|
|
106
|
+
const schema = JSON.parse(text);
|
|
107
|
+
assertSafeSchemaObjectKeys(schema);
|
|
108
|
+
return schema;
|
|
109
|
+
};
|
|
110
|
+
export const FORMILY_SCHEMA_PROP_NAMES = new Set([
|
|
111
|
+
'$anchor',
|
|
112
|
+
'$comment',
|
|
113
|
+
'$defs',
|
|
114
|
+
'$id',
|
|
115
|
+
'$ref',
|
|
116
|
+
'$schema',
|
|
117
|
+
'additionalItems',
|
|
118
|
+
'additionalProperties',
|
|
119
|
+
'allOf',
|
|
120
|
+
'anyOf',
|
|
121
|
+
'contains',
|
|
122
|
+
'const',
|
|
123
|
+
'contentEncoding',
|
|
124
|
+
'contentMediaType',
|
|
125
|
+
'contentSchema',
|
|
126
|
+
'default',
|
|
127
|
+
'definitions',
|
|
128
|
+
'dependencies',
|
|
129
|
+
'dependentRequired',
|
|
130
|
+
'dependentSchemas',
|
|
131
|
+
'deprecated',
|
|
132
|
+
'description',
|
|
133
|
+
'discriminator',
|
|
134
|
+
'else',
|
|
135
|
+
'enum',
|
|
136
|
+
'enumNames',
|
|
137
|
+
'examples',
|
|
138
|
+
'exclusiveMaximum',
|
|
139
|
+
'exclusiveMinimum',
|
|
140
|
+
'format',
|
|
141
|
+
'if',
|
|
142
|
+
'maxContains',
|
|
143
|
+
'maxItems',
|
|
144
|
+
'maxLength',
|
|
145
|
+
'maxProperties',
|
|
146
|
+
'maximum',
|
|
147
|
+
'minContains',
|
|
148
|
+
'minItems',
|
|
149
|
+
'minLength',
|
|
150
|
+
'minProperties',
|
|
151
|
+
'minimum',
|
|
152
|
+
'multipleOf',
|
|
153
|
+
'not',
|
|
154
|
+
'nullable',
|
|
155
|
+
'oneOf',
|
|
156
|
+
'pattern',
|
|
157
|
+
'patternProperties',
|
|
158
|
+
'prefixItems',
|
|
159
|
+
'propertyNames',
|
|
160
|
+
'readOnly',
|
|
161
|
+
'required',
|
|
162
|
+
'then',
|
|
163
|
+
'unevaluatedItems',
|
|
164
|
+
'unevaluatedProperties',
|
|
165
|
+
'uniqueItems',
|
|
166
|
+
'writeOnly',
|
|
167
|
+
]);
|
|
168
|
+
export const RESERVED_FORMILY_SCHEMA_PROP_NAMES = new Set([
|
|
169
|
+
'items',
|
|
170
|
+
'properties',
|
|
171
|
+
'title',
|
|
172
|
+
'type',
|
|
173
|
+
'x-component',
|
|
174
|
+
'x-component-props',
|
|
175
|
+
'x-designable-id',
|
|
176
|
+
'x-index',
|
|
177
|
+
]);
|
|
178
|
+
export const RENDER_ONLY_SCHEMA_PROP_NAMES = new Set([
|
|
179
|
+
'x-component',
|
|
180
|
+
'x-component-props',
|
|
181
|
+
'x-designable-id',
|
|
182
|
+
'x-index',
|
|
183
|
+
]);
|
|
184
|
+
const RISKY_EXPRESSION_PATTERN = /\{\{[\s\S]*(?:\b(?:document|eval|fetch|Function|globalThis|localStorage|sessionStorage|window|XMLHttpRequest)\b|import\s*\(|new\s+Function)[\s\S]*\}\}/i;
|
|
185
|
+
const UNSAFE_HTML_PATTERN = /<\s*(?:script|iframe|object|embed)\b|javascript\s*:|\son[a-z]+\s*=/i;
|
|
186
|
+
const EVENT_HANDLER_PROP_PATTERN = /^on[A-Z]/;
|
|
187
|
+
export const INTERNAL_DESIGNABLE_SCHEMA_PROP_NAMES = new Set([
|
|
188
|
+
'x-designable-formily-package',
|
|
189
|
+
'x-designable-root-form',
|
|
190
|
+
'x-designable-root-schema',
|
|
191
|
+
'x-designable-root-schema-form',
|
|
192
|
+
RUNTIME_DOCUMENT_SIDECAR_PROP,
|
|
193
|
+
'x-designable-schema-key',
|
|
194
|
+
'x-designable-schema-type',
|
|
195
|
+
DESIGNABLE_SCHEMA_VERSION_PROP,
|
|
196
|
+
'x-designable-state-hidden-from-visible',
|
|
197
|
+
'x-designable-scalar-array-item',
|
|
198
|
+
'x-designable-synthetic-array-item',
|
|
199
|
+
]);
|
|
200
|
+
export const COMPONENT_SCHEMA_PROP_NAMES_AS_COMPONENT_PROPS = {
|
|
201
|
+
Alert: new Set(['description']),
|
|
202
|
+
ColorPicker: new Set(['format']),
|
|
203
|
+
DatePicker: new Set(['format']),
|
|
204
|
+
DateRangePicker: new Set(['format']),
|
|
205
|
+
Empty: new Set(['description']),
|
|
206
|
+
Statistic: new Set(['title']),
|
|
207
|
+
TimePicker: new Set(['format']),
|
|
208
|
+
TimeRangePicker: new Set(['format']),
|
|
209
|
+
};
|
|
210
|
+
export const isComponentSchemaPropName = (componentName, key) => (Boolean(componentName && COMPONENT_SCHEMA_PROP_NAMES_AS_COMPONENT_PROPS[componentName]?.has(key)));
|
|
211
|
+
export const isSchemaMetadataPropName = (key, componentName) => (!INTERNAL_DESIGNABLE_SCHEMA_PROP_NAMES.has(key)
|
|
212
|
+
&& !isComponentSchemaPropName(componentName, key)
|
|
213
|
+
&& (FORMILY_SCHEMA_PROP_NAMES.has(key)
|
|
214
|
+
|| (key.startsWith('x-') && !RESERVED_FORMILY_SCHEMA_PROP_NAMES.has(key))));
|
|
215
|
+
export const isSchemaOnlyRenderPropName = (key, componentName) => {
|
|
216
|
+
if (isComponentSchemaPropName(componentName, key))
|
|
217
|
+
return false;
|
|
218
|
+
return (FORMILY_SCHEMA_PROP_NAMES.has(key)
|
|
219
|
+
|| RENDER_ONLY_SCHEMA_PROP_NAMES.has(key)
|
|
220
|
+
|| INTERNAL_DESIGNABLE_SCHEMA_PROP_NAMES.has(key)
|
|
221
|
+
|| key.startsWith('x-'));
|
|
222
|
+
};
|
|
223
|
+
const validateTreeSchemaBase = (schema) => {
|
|
224
|
+
if (!isRecord(schema) || typeof schema.componentName !== 'string') {
|
|
225
|
+
throw new Error('Schema 必须包含字符串类型的 componentName');
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
const validateTreeSchemaProps = (schema) => {
|
|
229
|
+
if (schema.props !== undefined && !isRecord(schema.props)) {
|
|
230
|
+
throw new Error('Schema props 必须是对象');
|
|
231
|
+
}
|
|
232
|
+
if (schema.sourceName !== undefined && typeof schema.sourceName !== 'string') {
|
|
233
|
+
throw new Error('Schema sourceName 必须是字符串');
|
|
234
|
+
}
|
|
235
|
+
if (schema.hidden !== undefined && typeof schema.hidden !== 'boolean') {
|
|
236
|
+
throw new Error('Schema hidden 必须是布尔值');
|
|
237
|
+
}
|
|
238
|
+
};
|
|
239
|
+
const validateTreeSchemaId = (schema, seenIds) => {
|
|
240
|
+
if (schema.id === undefined)
|
|
241
|
+
return;
|
|
242
|
+
if (typeof schema.id !== 'string') {
|
|
243
|
+
throw new Error('Schema id 必须是字符串');
|
|
244
|
+
}
|
|
245
|
+
if (seenIds.has(schema.id)) {
|
|
246
|
+
throw new Error(`Schema id 不能重复:${schema.id}`);
|
|
247
|
+
}
|
|
248
|
+
seenIds.add(schema.id);
|
|
249
|
+
};
|
|
250
|
+
const validateTreeSchemaChildren = (schema, seenIds) => {
|
|
251
|
+
if (schema.children === undefined)
|
|
252
|
+
return;
|
|
253
|
+
if (!Array.isArray(schema.children)) {
|
|
254
|
+
throw new Error('Schema children 必须是数组');
|
|
255
|
+
}
|
|
256
|
+
schema.children.forEach(child => validateTreeSchemaNode(child, seenIds));
|
|
257
|
+
};
|
|
258
|
+
const validateTreeSchemaNode = (schema, seenIds) => {
|
|
259
|
+
validateTreeSchemaBase(schema);
|
|
260
|
+
validateTreeSchemaProps(schema);
|
|
261
|
+
validateTreeSchemaId(schema, seenIds);
|
|
262
|
+
validateTreeSchemaChildren(schema, seenIds);
|
|
263
|
+
};
|
|
264
|
+
export const assertTreeSchema = (schema) => {
|
|
265
|
+
assertSafeSchemaObjectKeys(schema);
|
|
266
|
+
validateTreeSchemaNode(schema, new Set());
|
|
267
|
+
};
|
|
268
|
+
const createValidationResult = (issues) => ({
|
|
269
|
+
issues,
|
|
270
|
+
valid: !issues.some(issue => issue.severity === 'error'),
|
|
271
|
+
});
|
|
272
|
+
const getSchemaVersionValue = (schema) => (isRecord(schema) ? schema[DESIGNABLE_SCHEMA_VERSION_PROP] : undefined);
|
|
273
|
+
const isPositiveInteger = (value) => (typeof value === 'number'
|
|
274
|
+
&& Number.isInteger(value)
|
|
275
|
+
&& value > 0);
|
|
276
|
+
export const getDesignableSchemaVersion = (schema) => {
|
|
277
|
+
const version = getSchemaVersionValue(schema);
|
|
278
|
+
return isPositiveInteger(version) ? version : undefined;
|
|
279
|
+
};
|
|
280
|
+
const DESIGNABLE_SCHEMA_MIGRATORS = {};
|
|
281
|
+
const cloneSafeDesignableSchema = (schema) => {
|
|
282
|
+
const schemaText = stringifySchemaForExport(schema, 0);
|
|
283
|
+
return parseSchemaJSON(schemaText);
|
|
284
|
+
};
|
|
285
|
+
export const validateDesignableSchemaVersion = (schema, path = `Schema.${DESIGNABLE_SCHEMA_VERSION_PROP}`) => {
|
|
286
|
+
const version = getSchemaVersionValue(schema);
|
|
287
|
+
if (version === undefined)
|
|
288
|
+
return [];
|
|
289
|
+
if (!isPositiveInteger(version)) {
|
|
290
|
+
return [{
|
|
291
|
+
code: 'schema_version_invalid',
|
|
292
|
+
message: `${DESIGNABLE_SCHEMA_VERSION_PROP} 必须是正整数`,
|
|
293
|
+
path,
|
|
294
|
+
severity: 'error',
|
|
295
|
+
}];
|
|
296
|
+
}
|
|
297
|
+
if (version > CURRENT_DESIGNABLE_SCHEMA_VERSION) {
|
|
298
|
+
return [{
|
|
299
|
+
code: 'schema_version_future',
|
|
300
|
+
message: `Schema 版本 ${version} 高于当前支持版本 ${CURRENT_DESIGNABLE_SCHEMA_VERSION},生产保存前建议确认兼容性`,
|
|
301
|
+
path,
|
|
302
|
+
severity: 'warning',
|
|
303
|
+
}];
|
|
304
|
+
}
|
|
305
|
+
if (version < CURRENT_DESIGNABLE_SCHEMA_VERSION) {
|
|
306
|
+
return [{
|
|
307
|
+
code: 'schema_version_legacy',
|
|
308
|
+
message: `Schema 版本 ${version} 低于当前支持版本 ${CURRENT_DESIGNABLE_SCHEMA_VERSION},已按兼容模式处理`,
|
|
309
|
+
path,
|
|
310
|
+
severity: 'warning',
|
|
311
|
+
}];
|
|
312
|
+
}
|
|
313
|
+
return [];
|
|
314
|
+
};
|
|
315
|
+
export const migrateDesignableSchema = (schema, options = {}) => {
|
|
316
|
+
const targetVersion = options.targetVersion ?? CURRENT_DESIGNABLE_SCHEMA_VERSION;
|
|
317
|
+
if (!isPositiveInteger(targetVersion)) {
|
|
318
|
+
throw new Error('目标 Schema 版本必须是正整数');
|
|
319
|
+
}
|
|
320
|
+
const nextSchema = cloneSafeDesignableSchema(schema);
|
|
321
|
+
const path = options.path ?? `Schema.${DESIGNABLE_SCHEMA_VERSION_PROP}`;
|
|
322
|
+
const issues = validateDesignableSchemaVersion(nextSchema, path);
|
|
323
|
+
const blockingIssue = issues.find(issue => issue.severity === 'error');
|
|
324
|
+
if (blockingIssue) {
|
|
325
|
+
throw new Error(blockingIssue.message);
|
|
326
|
+
}
|
|
327
|
+
const sourceVersion = getDesignableSchemaVersion(nextSchema) ?? CURRENT_DESIGNABLE_SCHEMA_VERSION;
|
|
328
|
+
if (sourceVersion > targetVersion) {
|
|
329
|
+
return {
|
|
330
|
+
fromVersion: sourceVersion,
|
|
331
|
+
issues,
|
|
332
|
+
migrated: false,
|
|
333
|
+
schema: nextSchema,
|
|
334
|
+
toVersion: targetVersion,
|
|
335
|
+
};
|
|
336
|
+
}
|
|
337
|
+
let migratedSchema = nextSchema;
|
|
338
|
+
for (let version = sourceVersion; version < targetVersion; version += 1) {
|
|
339
|
+
const migrator = DESIGNABLE_SCHEMA_MIGRATORS[version];
|
|
340
|
+
if (!migrator) {
|
|
341
|
+
throw new Error(`缺少 Schema v${version} 到 v${version + 1} 的迁移器`);
|
|
342
|
+
}
|
|
343
|
+
migratedSchema = migrator(migratedSchema);
|
|
344
|
+
}
|
|
345
|
+
return {
|
|
346
|
+
fromVersion: sourceVersion,
|
|
347
|
+
issues,
|
|
348
|
+
migrated: sourceVersion < targetVersion,
|
|
349
|
+
schema: migratedSchema,
|
|
350
|
+
toVersion: targetVersion,
|
|
351
|
+
};
|
|
352
|
+
};
|
|
353
|
+
const getKnownComponentNameSet = (options) => {
|
|
354
|
+
if (!options?.knownComponentNames)
|
|
355
|
+
return undefined;
|
|
356
|
+
const names = new Set(options.knownComponentNames);
|
|
357
|
+
if (options.rootComponentName) {
|
|
358
|
+
names.add(options.rootComponentName);
|
|
359
|
+
}
|
|
360
|
+
names.add('Root');
|
|
361
|
+
return names;
|
|
362
|
+
};
|
|
363
|
+
const getEditableComponentNameSet = (options) => {
|
|
364
|
+
if (!options?.editableComponentNames)
|
|
365
|
+
return undefined;
|
|
366
|
+
const names = new Set(options.editableComponentNames);
|
|
367
|
+
if (options.rootComponentName) {
|
|
368
|
+
names.add(options.rootComponentName);
|
|
369
|
+
}
|
|
370
|
+
names.add('Root');
|
|
371
|
+
return names;
|
|
372
|
+
};
|
|
373
|
+
const pushIssue = (issues, issue) => {
|
|
374
|
+
issues.push(issue);
|
|
375
|
+
};
|
|
376
|
+
const getRecordComponentName = (record) => {
|
|
377
|
+
if (typeof record.componentName === 'string')
|
|
378
|
+
return record.componentName;
|
|
379
|
+
if (typeof record['x-component'] === 'string')
|
|
380
|
+
return record['x-component'];
|
|
381
|
+
return undefined;
|
|
382
|
+
};
|
|
383
|
+
const getRecordComponentProps = (record) => {
|
|
384
|
+
if (isRecord(record.props))
|
|
385
|
+
return {
|
|
386
|
+
pathKey: 'props',
|
|
387
|
+
value: record.props,
|
|
388
|
+
};
|
|
389
|
+
if (isRecord(record['x-component-props']))
|
|
390
|
+
return {
|
|
391
|
+
pathKey: 'x-component-props',
|
|
392
|
+
value: record['x-component-props'],
|
|
393
|
+
};
|
|
394
|
+
return undefined;
|
|
395
|
+
};
|
|
396
|
+
const pushUnsafeHtmlIssue = (issues, path, componentName) => {
|
|
397
|
+
pushIssue(issues, {
|
|
398
|
+
code: 'unsafe_html_content',
|
|
399
|
+
componentName,
|
|
400
|
+
message: 'HTML 内容包含不安全脚本、事件属性或协议',
|
|
401
|
+
path,
|
|
402
|
+
severity: 'error',
|
|
403
|
+
});
|
|
404
|
+
};
|
|
405
|
+
const pushRiskyExpressionIssue = (issues, path, componentName) => {
|
|
406
|
+
pushIssue(issues, {
|
|
407
|
+
code: 'risky_expression',
|
|
408
|
+
componentName,
|
|
409
|
+
message: '表达式引用了浏览器全局对象或动态执行能力,生产保存前建议人工确认',
|
|
410
|
+
path,
|
|
411
|
+
severity: 'warning',
|
|
412
|
+
});
|
|
413
|
+
};
|
|
414
|
+
const pushEventExpressionIssue = (issues, path, componentName) => {
|
|
415
|
+
pushIssue(issues, {
|
|
416
|
+
code: 'event_expression_handler',
|
|
417
|
+
componentName,
|
|
418
|
+
message: '事件属性使用表达式,生产保存前建议确认执行边界',
|
|
419
|
+
path,
|
|
420
|
+
severity: 'warning',
|
|
421
|
+
});
|
|
422
|
+
};
|
|
423
|
+
const validateSecurityStringValue = (value, issues, path, key, componentName) => {
|
|
424
|
+
if (((componentName === 'HTMLContent' && key === 'value') || key === '__html')
|
|
425
|
+
&& UNSAFE_HTML_PATTERN.test(value)) {
|
|
426
|
+
pushUnsafeHtmlIssue(issues, path, componentName);
|
|
427
|
+
}
|
|
428
|
+
if (RISKY_EXPRESSION_PATTERN.test(value)) {
|
|
429
|
+
pushRiskyExpressionIssue(issues, path, componentName);
|
|
430
|
+
}
|
|
431
|
+
if (EVENT_HANDLER_PROP_PATTERN.test(key) && value.trim()) {
|
|
432
|
+
pushEventExpressionIssue(issues, path, componentName);
|
|
433
|
+
}
|
|
434
|
+
};
|
|
435
|
+
const validateSchemaSecurityNode = (value, issues, path, inheritedComponentName) => {
|
|
436
|
+
if (Array.isArray(value)) {
|
|
437
|
+
value.forEach((item, index) => {
|
|
438
|
+
validateSchemaSecurityNode(item, issues, `${path}.${index}`, inheritedComponentName);
|
|
439
|
+
});
|
|
440
|
+
return;
|
|
441
|
+
}
|
|
442
|
+
if (!isRecord(value))
|
|
443
|
+
return;
|
|
444
|
+
const componentName = getRecordComponentName(value) ?? inheritedComponentName;
|
|
445
|
+
const componentProps = getRecordComponentProps(value);
|
|
446
|
+
if (componentProps) {
|
|
447
|
+
Object.entries(componentProps.value).forEach(([key, item]) => {
|
|
448
|
+
const propPath = `${path}.${componentProps.pathKey}.${key}`;
|
|
449
|
+
if (typeof item === 'string') {
|
|
450
|
+
validateSecurityStringValue(item, issues, propPath, key, componentName);
|
|
451
|
+
}
|
|
452
|
+
validateSchemaSecurityNode(item, issues, propPath, componentName);
|
|
453
|
+
});
|
|
454
|
+
}
|
|
455
|
+
Object.entries(value).forEach(([key, item]) => {
|
|
456
|
+
if (key === 'props' || key === 'x-component-props')
|
|
457
|
+
return;
|
|
458
|
+
const nextPath = `${path}.${key}`;
|
|
459
|
+
if (typeof item === 'string') {
|
|
460
|
+
validateSecurityStringValue(item, issues, nextPath, key, componentName);
|
|
461
|
+
}
|
|
462
|
+
validateSchemaSecurityNode(item, issues, nextPath, componentName);
|
|
463
|
+
});
|
|
464
|
+
};
|
|
465
|
+
export const validateSchemaSecurity = (schema, path = 'Schema') => {
|
|
466
|
+
const issues = [];
|
|
467
|
+
validateSchemaSecurityNode(schema, issues, path);
|
|
468
|
+
return issues;
|
|
469
|
+
};
|
|
470
|
+
const getDiagnosticComponentName = (schema) => (typeof schema.componentName === 'string' && schema.componentName.trim()
|
|
471
|
+
? schema.componentName
|
|
472
|
+
: undefined);
|
|
473
|
+
const validateDiagnosticComponentName = (schema, issues, path, knownComponentNames, editableComponentNames) => {
|
|
474
|
+
const componentName = getDiagnosticComponentName(schema);
|
|
475
|
+
if (!componentName) {
|
|
476
|
+
pushIssue(issues, {
|
|
477
|
+
code: 'component_name_required',
|
|
478
|
+
message: 'Schema 节点必须包含非空字符串 componentName',
|
|
479
|
+
path: `${path}.componentName`,
|
|
480
|
+
severity: 'error',
|
|
481
|
+
});
|
|
482
|
+
}
|
|
483
|
+
else if (knownComponentNames && !knownComponentNames.has(componentName)) {
|
|
484
|
+
pushIssue(issues, {
|
|
485
|
+
code: 'component_not_registered',
|
|
486
|
+
componentName,
|
|
487
|
+
message: `组件未注册:${componentName}`,
|
|
488
|
+
path: `${path}.componentName`,
|
|
489
|
+
severity: 'error',
|
|
490
|
+
});
|
|
491
|
+
}
|
|
492
|
+
else if (editableComponentNames && !editableComponentNames.has(componentName)) {
|
|
493
|
+
pushIssue(issues, {
|
|
494
|
+
code: 'component_behavior_missing',
|
|
495
|
+
componentName,
|
|
496
|
+
message: `组件缺少 Behavior,无法在设计视图中编辑:${componentName}`,
|
|
497
|
+
path: `${path}.componentName`,
|
|
498
|
+
severity: 'error',
|
|
499
|
+
});
|
|
500
|
+
}
|
|
501
|
+
return componentName;
|
|
502
|
+
};
|
|
503
|
+
const validateDiagnosticNodeId = (schema, issues, seenIds, path, componentName) => {
|
|
504
|
+
if (schema.id === undefined)
|
|
505
|
+
return;
|
|
506
|
+
if (typeof schema.id !== 'string' || !schema.id.trim()) {
|
|
507
|
+
pushIssue(issues, {
|
|
508
|
+
code: 'node_id_invalid',
|
|
509
|
+
componentName,
|
|
510
|
+
message: 'Schema id 必须是非空字符串',
|
|
511
|
+
path: `${path}.id`,
|
|
512
|
+
severity: 'error',
|
|
513
|
+
});
|
|
514
|
+
return;
|
|
515
|
+
}
|
|
516
|
+
if (seenIds.has(schema.id)) {
|
|
517
|
+
pushIssue(issues, {
|
|
518
|
+
code: 'node_id_duplicated',
|
|
519
|
+
componentName,
|
|
520
|
+
message: `Schema id 不能重复:${schema.id}`,
|
|
521
|
+
path: `${path}.id`,
|
|
522
|
+
severity: 'error',
|
|
523
|
+
});
|
|
524
|
+
return;
|
|
525
|
+
}
|
|
526
|
+
seenIds.add(schema.id);
|
|
527
|
+
};
|
|
528
|
+
const validateDiagnosticNodeProps = (schema, issues, path, componentName) => {
|
|
529
|
+
if (schema.props === undefined || isRecord(schema.props))
|
|
530
|
+
return;
|
|
531
|
+
pushIssue(issues, {
|
|
532
|
+
code: 'node_props_invalid',
|
|
533
|
+
componentName,
|
|
534
|
+
message: 'Schema props 必须是对象',
|
|
535
|
+
path: `${path}.props`,
|
|
536
|
+
severity: 'error',
|
|
537
|
+
});
|
|
538
|
+
};
|
|
539
|
+
const validateDiagnosticScalarFields = (schema, issues, path, componentName) => {
|
|
540
|
+
if (schema.sourceName !== undefined && typeof schema.sourceName !== 'string') {
|
|
541
|
+
pushIssue(issues, {
|
|
542
|
+
code: 'node_source_name_invalid',
|
|
543
|
+
componentName,
|
|
544
|
+
message: 'Schema sourceName 必须是字符串',
|
|
545
|
+
path: `${path}.sourceName`,
|
|
546
|
+
severity: 'error',
|
|
547
|
+
});
|
|
548
|
+
}
|
|
549
|
+
if (schema.hidden !== undefined && typeof schema.hidden !== 'boolean') {
|
|
550
|
+
pushIssue(issues, {
|
|
551
|
+
code: 'node_hidden_invalid',
|
|
552
|
+
componentName,
|
|
553
|
+
message: 'Schema hidden 必须是布尔值',
|
|
554
|
+
path: `${path}.hidden`,
|
|
555
|
+
severity: 'error',
|
|
556
|
+
});
|
|
557
|
+
}
|
|
558
|
+
};
|
|
559
|
+
const validateDiagnosticChildren = (schema, issues, seenIds, path, knownComponentNames, editableComponentNames, componentName) => {
|
|
560
|
+
if (schema.children === undefined)
|
|
561
|
+
return;
|
|
562
|
+
if (!Array.isArray(schema.children)) {
|
|
563
|
+
pushIssue(issues, {
|
|
564
|
+
code: 'node_children_invalid',
|
|
565
|
+
componentName,
|
|
566
|
+
message: 'Schema children 必须是数组',
|
|
567
|
+
path: `${path}.children`,
|
|
568
|
+
severity: 'error',
|
|
569
|
+
});
|
|
570
|
+
return;
|
|
571
|
+
}
|
|
572
|
+
schema.children.forEach((child, index) => {
|
|
573
|
+
validateTreeSchemaNodeForDiagnostics(child, issues, seenIds, `${path}.children.${index}`, knownComponentNames, editableComponentNames);
|
|
574
|
+
});
|
|
575
|
+
};
|
|
576
|
+
const validateTreeSchemaNodeForDiagnostics = (schema, issues, seenIds, path, knownComponentNames, editableComponentNames) => {
|
|
577
|
+
if (!isRecord(schema)) {
|
|
578
|
+
pushIssue(issues, {
|
|
579
|
+
code: 'tree_node_not_object',
|
|
580
|
+
message: 'Schema 节点必须是对象',
|
|
581
|
+
path,
|
|
582
|
+
severity: 'error',
|
|
583
|
+
});
|
|
584
|
+
return;
|
|
585
|
+
}
|
|
586
|
+
const componentName = validateDiagnosticComponentName(schema, issues, path, knownComponentNames, editableComponentNames);
|
|
587
|
+
validateDiagnosticNodeId(schema, issues, seenIds, path, componentName);
|
|
588
|
+
validateDiagnosticNodeProps(schema, issues, path, componentName);
|
|
589
|
+
validateDiagnosticScalarFields(schema, issues, path, componentName);
|
|
590
|
+
validateDiagnosticChildren(schema, issues, seenIds, path, knownComponentNames, editableComponentNames, componentName);
|
|
591
|
+
};
|
|
592
|
+
export const validateTreeSchema = (schema, options) => {
|
|
593
|
+
const issues = [];
|
|
594
|
+
try {
|
|
595
|
+
assertSafeSchemaObjectKeys(schema);
|
|
596
|
+
}
|
|
597
|
+
catch (error) {
|
|
598
|
+
pushIssue(issues, {
|
|
599
|
+
code: 'unsafe_schema_key',
|
|
600
|
+
message: error instanceof Error ? error.message : 'Schema 包含不安全字段',
|
|
601
|
+
path: 'Schema',
|
|
602
|
+
severity: 'error',
|
|
603
|
+
});
|
|
604
|
+
return createValidationResult(issues);
|
|
605
|
+
}
|
|
606
|
+
validateTreeSchemaNodeForDiagnostics(schema, issues, new Set(), 'Schema', getKnownComponentNameSet(options), getEditableComponentNameSet(options));
|
|
607
|
+
issues.push(...validateDesignableSchemaVersion(schema));
|
|
608
|
+
if (options?.includeSecurityDiagnostics !== false) {
|
|
609
|
+
issues.push(...validateSchemaSecurity(schema));
|
|
610
|
+
}
|
|
611
|
+
return createValidationResult(issues);
|
|
612
|
+
};
|
|
613
|
+
export const stringifySchemaForExport = (schema, space = 2) => {
|
|
614
|
+
const schemaText = JSON.stringify(schema, null, space);
|
|
615
|
+
if (typeof schemaText !== 'string') {
|
|
616
|
+
throw new Error('当前画布 Schema 为空,无法导出 JSON');
|
|
617
|
+
}
|
|
618
|
+
return schemaText;
|
|
619
|
+
};
|
|
620
|
+
export const createSchemaFingerprint = (getSchema) => {
|
|
621
|
+
try {
|
|
622
|
+
return stringifySchemaForExport(getSchema(), 0);
|
|
623
|
+
}
|
|
624
|
+
catch {
|
|
625
|
+
return undefined;
|
|
626
|
+
}
|
|
627
|
+
};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
const UNSAFE_STYLE_VALUE_PATTERN = /\b(?:javascript\s*:|expression\s*\()/i;
|
|
2
|
+
const STYLE_URL_PATTERN = /url\(\s*(?:"([^"]*)"|'([^']*)'|([^)]*))\s*\)/gi;
|
|
3
|
+
const SAFE_STYLE_URL_PATTERN = /^(?:https?:\/\/|\/(?!\/)|\.{1,2}\/|blob:|data:image\/(?:png|jpe?g|gif|webp);base64,)/i;
|
|
4
|
+
const decodeCSSUrlEscapes = (value) => (value.replace(/\\([0-9a-f]{1,6}\s?|.)/gi, (_, escape) => {
|
|
5
|
+
const hex = escape.trim();
|
|
6
|
+
if (/^[0-9a-f]{1,6}$/i.test(hex)) {
|
|
7
|
+
const codePoint = Number.parseInt(hex, 16);
|
|
8
|
+
try {
|
|
9
|
+
return String.fromCodePoint(codePoint);
|
|
10
|
+
}
|
|
11
|
+
catch {
|
|
12
|
+
return '';
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
return escape;
|
|
16
|
+
}));
|
|
17
|
+
const normalizeStyleUrlForProtocolCheck = (url) => (decodeCSSUrlEscapes(url)
|
|
18
|
+
.trim()
|
|
19
|
+
.split('')
|
|
20
|
+
.filter(char => char > ' ' && char !== String.fromCharCode(127))
|
|
21
|
+
.join(''));
|
|
22
|
+
export const isSafeStyleUrl = (url) => (SAFE_STYLE_URL_PATTERN.test(normalizeStyleUrlForProtocolCheck(url)));
|
|
23
|
+
export const hasUnsafeStyleValue = (value) => {
|
|
24
|
+
if (UNSAFE_STYLE_VALUE_PATTERN.test(normalizeStyleUrlForProtocolCheck(value)))
|
|
25
|
+
return true;
|
|
26
|
+
STYLE_URL_PATTERN.lastIndex = 0;
|
|
27
|
+
let matched = STYLE_URL_PATTERN.exec(value);
|
|
28
|
+
while (matched) {
|
|
29
|
+
const url = matched[1] ?? matched[2] ?? matched[3] ?? '';
|
|
30
|
+
if (!isSafeStyleUrl(url))
|
|
31
|
+
return true;
|
|
32
|
+
matched = STYLE_URL_PATTERN.exec(value);
|
|
33
|
+
}
|
|
34
|
+
return false;
|
|
35
|
+
};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { ISchemaTransformer } from "../types.js";
|
|
2
|
+
export interface IFormilySchemaNode {
|
|
3
|
+
items?: IFormilySchemaNode | IFormilySchemaNode[];
|
|
4
|
+
type?: string;
|
|
5
|
+
title?: string;
|
|
6
|
+
properties?: Record<string, IFormilySchemaNode>;
|
|
7
|
+
'x-component'?: string;
|
|
8
|
+
'x-component-props'?: Record<string, unknown>;
|
|
9
|
+
'x-designable-id'?: string;
|
|
10
|
+
'x-index'?: number;
|
|
11
|
+
[key: string]: unknown;
|
|
12
|
+
}
|
|
13
|
+
export interface IFormilySchemaDocument {
|
|
14
|
+
type: 'object';
|
|
15
|
+
properties?: Record<string, IFormilySchemaNode>;
|
|
16
|
+
[key: string]: unknown;
|
|
17
|
+
}
|
|
18
|
+
export interface IFormilySchemaPackage {
|
|
19
|
+
form?: Record<string, unknown>;
|
|
20
|
+
schema: IFormilySchemaDocument;
|
|
21
|
+
}
|
|
22
|
+
export type IFormilySchemaInput = IFormilySchemaDocument | IFormilySchemaPackage;
|
|
23
|
+
export interface IFormilySchemaTransformerOptions {
|
|
24
|
+
rootComponentName?: string;
|
|
25
|
+
componentTypes?: Record<string, string>;
|
|
26
|
+
componentDecorators?: Record<string, string | false>;
|
|
27
|
+
componentNameAliases?: Record<string, string>;
|
|
28
|
+
componentNameByType?: Record<string, string>;
|
|
29
|
+
arrayItemComponentName?: string;
|
|
30
|
+
fieldNamePrefix?: string;
|
|
31
|
+
}
|
|
32
|
+
export declare function assertFormilySchemaNode(schema: unknown, path?: string): asserts schema is IFormilySchemaNode;
|
|
33
|
+
export declare function assertFormilySchemaDocument(schema: unknown): asserts schema is IFormilySchemaDocument;
|
|
34
|
+
export declare const createFormilySchemaTransformer: (options?: IFormilySchemaTransformerOptions) => ISchemaTransformer<IFormilySchemaInput>;
|