Skip to content

Commit 5c06294

Browse files
committed
fix(lib): 修复anyOf嵌套object 可能丢失部分校验规则的问题
1 parent 44bcb44 commit 5c06294

File tree

3 files changed

+84
-38
lines changed
  • packages/lib
    • utils
    • vue2/vue2-core/src/fields/combiningSchemas/SelectLinkageField
    • vue3/vue3-core/src/fields/combiningSchemas/SelectLinkageField

3 files changed

+84
-38
lines changed

packages/lib/utils/utils.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,8 @@ export const genId = (function genIdFn() {
218218

219219
// 空对象
220220
export function isEmptyObject(obj) {
221+
if (!obj) return true;
222+
221223
for (const key in obj) {
222224
if (Object.prototype.hasOwnProperty.call(obj, key)) {
223225
return false;

packages/lib/vue2/vue2-core/src/fields/combiningSchemas/SelectLinkageField/index.js

Lines changed: 49 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -168,33 +168,36 @@ export default {
168168
const { curNodePath } = this.$props;
169169
const pathClassName = nodePath2ClassName(curNodePath);
170170

171-
// object 需要保持原有属性,如果存在原有属性这里单独渲染
172-
let originVNode = null;
171+
// is object
173172
const isTypeObject = (this.schema.type === 'object' || this.schema.properties);
174-
if (isTypeObject && !isEmptyObject(this.schema.properties)) {
175-
const origSchema = Object.assign({}, this.schema);
176-
delete origSchema[this.combiningType];
177-
178-
originVNode = h(SchemaField, {
179-
key: `origin_${this.combiningType}`,
180-
class: {
181-
[`${this.combiningType}_originBox`]: true,
182-
[`${pathClassName}-originBox`]: true
183-
},
184-
props: {
185-
...this.$props,
186-
schema: origSchema,
187-
// needValidFieldGroup: false // 单独校验,这里无需处理
188-
}
189-
});
190-
}
191173

192174
// 选择附加的节点
193175
const childrenVNodeList = [this.getSelectBoxVNode()];
194176

195-
// 当前选中的 oneOf 附加的节点
177+
// 当前option内容
196178
let curSelectSchema = this.selectList[this.curSelectIndex];
179+
180+
// 当前选中节点合并schema
197181
if (curSelectSchema) {
182+
const {
183+
// eslint-disable-next-line no-unused-vars
184+
properties,
185+
// eslint-disable-next-line no-unused-vars
186+
[this.combiningType]: combiningType,
187+
// eslint-disable-next-line no-unused-vars
188+
[`${this.combiningType}Select`]: combiningTypeSelect,
189+
...parentSchema
190+
} = this.schema;
191+
192+
// 合并父级schema
193+
curSelectSchema = Object.assign({}, parentSchema, curSelectSchema);
194+
}
195+
196+
// object类型但没有附加属性
197+
const isObjectEmptyAttachProperties = isTypeObject && isEmptyObject(curSelectSchema && curSelectSchema.properties);
198+
199+
// 当前选中的 oneOf 附加节点 VNode
200+
if (curSelectSchema && !isObjectEmptyAttachProperties) {
198201
// 覆盖父级的属性
199202
const {
200203
// eslint-disable-next-line no-unused-vars
@@ -252,6 +255,32 @@ export default {
252255
);
253256
}
254257

258+
// object 需要保持原有属性,如果存在原有属性这里单独渲染
259+
let originVNode = null;
260+
if (isTypeObject && !isEmptyObject(this.schema.properties)) {
261+
const {
262+
// eslint-disable-next-line no-unused-vars
263+
title, description, properties, ...optionSchema
264+
} = curSelectSchema;
265+
266+
// object 原始项渲染也需要合并anyOf的内容
267+
const origSchema = Object.assign({}, this.schema, optionSchema);
268+
delete origSchema[this.combiningType];
269+
270+
originVNode = h(SchemaField, {
271+
key: `origin_${this.combiningType}`,
272+
class: {
273+
[`${this.combiningType}_originBox`]: true,
274+
[`${pathClassName}-originBox`]: true
275+
},
276+
props: {
277+
...this.$props,
278+
schema: origSchema,
279+
// needValidFieldGroup: false // 单独校验,这里无需处理
280+
}
281+
});
282+
}
283+
255284
// oneOf 校验 VNode
256285
childrenVNodeList.push(
257286
h(Widget, {

packages/lib/vue3/vue3-core/src/fields/combiningSchemas/SelectLinkageField/index.js

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -159,30 +159,16 @@ export default {
159159
const { curNodePath } = props;
160160
const pathClassName = nodePath2ClassName(curNodePath);
161161

162-
// object 需要保持原有属性,如果存在原有属性这里单独渲染
163-
let originVNode = null;
162+
// is object
164163
const isTypeObject = (props.schema.type === 'object' || props.schema.properties);
165-
if (isTypeObject && !isEmptyObject(props.schema.properties)) {
166-
const origSchema = Object.assign({}, props.schema);
167-
delete origSchema[props.combiningType];
168-
169-
originVNode = h(SchemaField, {
170-
key: `origin_${props.combiningType}`,
171-
class: {
172-
[`${props.combiningType}_originBox`]: true,
173-
[`${pathClassName}-originBox`]: true
174-
},
175-
...props,
176-
schema: origSchema,
177-
// needValidFieldGroup: false // 单独校验,这里无需处理
178-
});
179-
}
180164

181165
// 选择附加的节点
182166
const childrenVNodeList = [getSelectBoxVNode()];
183167

184-
// 当前选中的 oneOf 附加的节点
168+
// 当前option内容
185169
let curSelectSchema = props.selectList[curSelectIndex.value];
170+
171+
// 当前选中节点合并schema
186172
if (curSelectSchema) {
187173
// 覆盖父级的属性
188174
const {
@@ -196,7 +182,12 @@ export default {
196182
} = props.schema;
197183

198184
curSelectSchema = Object.assign({}, parentSchema, curSelectSchema);
185+
}
199186

187+
// object类型但没有附加属性
188+
const isObjectEmptyAttachProperties = isTypeObject && isEmptyObject(curSelectSchema && curSelectSchema.properties);
189+
190+
if (curSelectSchema && !isObjectEmptyAttachProperties) {
200191
// 当前节点的ui err配置,用来支持所有选项的统一配置
201192
// 取出 oneOf anyOf 同级配置,然后再合并到 当前选中的schema中
202193
const userUiOptions = filterObject(getUiOptions({
@@ -239,6 +230,30 @@ export default {
239230
);
240231
}
241232

233+
// object 需要保持原有属性,如果存在原有属性这里单独渲染
234+
let originVNode = null;
235+
if (isTypeObject && !isEmptyObject(props.schema.properties)) {
236+
const {
237+
// eslint-disable-next-line no-unused-vars
238+
title, description, properties, ...optionSchema
239+
} = curSelectSchema;
240+
241+
// object 原始项渲染也需要合并anyOf的内容
242+
const origSchema = Object.assign({}, props.schema, optionSchema);
243+
delete origSchema[props.combiningType];
244+
245+
originVNode = h(SchemaField, {
246+
key: `origin_${props.combiningType}`,
247+
class: {
248+
[`${props.combiningType}_originBox`]: true,
249+
[`${pathClassName}-originBox`]: true
250+
},
251+
...props,
252+
schema: origSchema,
253+
// needValidFieldGroup: false // 单独校验,这里无需处理
254+
});
255+
}
256+
242257
// oneOf 校验 VNode
243258
childrenVNodeList.push(
244259
h(Widget, {

0 commit comments

Comments
 (0)