Skip to content

Commit 6dd9780

Browse files
committed
fix(lib): 修复anyOf下多级对象初始值计算错误问题
fix #57
1 parent 8f9da99 commit 6dd9780

18 files changed

+45
-36
lines changed

packages/lib/utils/schema/getDefaultFormState.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,11 @@ function computeDefaults(
9696
includeUndefinedValues
9797
));
9898
} else if ('oneOf' in schema) {
99-
const matchSchema = schema.oneOf[getMatchingOption(formData, schema.oneOf, rootSchema)];
99+
const matchSchema = retrieveSchema(
100+
schema.oneOf[getMatchingOption(formData, schema.oneOf, rootSchema)],
101+
rootSchema,
102+
formData
103+
);
100104
if (schema.properties && matchSchema.properties) {
101105
// 对象 oneOf 需要合并原属性和 oneOf 属性
102106
const mergeSchema = mergeObjects(schema, matchSchema);
@@ -106,7 +110,12 @@ function computeDefaults(
106110
schema = matchSchema;
107111
}
108112
} else if ('anyOf' in schema) {
109-
const matchSchema = schema.anyOf[getMatchingOption(formData, schema.anyOf, rootSchema)];
113+
const matchSchema = retrieveSchema(
114+
schema.anyOf[getMatchingOption(formData, schema.anyOf, rootSchema)],
115+
rootSchema,
116+
formData
117+
);
118+
110119
if (schema.properties && matchSchema.properties) {
111120
// 对象 anyOf 需要合并原属性和 anyOf 属性
112121
const mergeSchema = mergeObjects(schema, matchSchema);
@@ -116,7 +125,6 @@ function computeDefaults(
116125
schema = matchSchema;
117126
}
118127
}
119-
120128
// Not defaults defined for this node, fallback to generic typed ones.
121129
if (typeof defaults === 'undefined') {
122130
defaults = schema.default;

packages/lib/utils/schema/validate.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Ajv from 'ajv';
22
import i18n from '../i18n';
3+
import retrieveSchema from './retriev';
34

45
import {
56
isObject, deepEquals
@@ -245,7 +246,7 @@ export function isValid(schema, data) {
245246
export function getMatchingOption(formData, options, rootSchema, haveAllFields = false) {
246247
// eslint-disable-next-line no-plusplus
247248
for (let i = 0; i < options.length; i++) {
248-
const option = options[i];
249+
const option = retrieveSchema(options[i], rootSchema, formData);
249250

250251
// If the schema describes an object then we need to add slightly more
251252
// strict matching to the schema, because unless the schema uses the

packages/lib/vue2/vue2-form-element/dist/vueJsonSchemaForm.esm.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9070,7 +9070,7 @@ function getMatchingOption(formData, options, rootSchema) {
90709070

90719071
// eslint-disable-next-line no-plusplus
90729072
for (var i = 0; i < options.length; i++) {
9073-
var option = options[i]; // If the schema describes an object then we need to add slightly more
9073+
var option = retrieveSchema(options[i], rootSchema, formData); // If the schema describes an object then we need to add slightly more
90749074
// strict matching to the schema, because unless the schema uses the
90759075
// "requires" keyword, an object will match the schema as long as it
90769076
// doesn't have matching keys with a conflicting type. To do this we use an
@@ -9213,7 +9213,7 @@ function computeDefaults(_schema, parentDefaults, rootSchema) {
92139213
return computeDefaults(itemSchema, Array.isArray(parentDefaults) ? parentDefaults[idx] : undefined, rootSchema, formData, includeUndefinedValues);
92149214
});
92159215
} else if ('oneOf' in schema) {
9216-
var matchSchema = schema.oneOf[getMatchingOption(formData, schema.oneOf, rootSchema)];
9216+
var matchSchema = retrieveSchema(schema.oneOf[getMatchingOption(formData, schema.oneOf, rootSchema)], rootSchema, formData);
92179217

92189218
if (schema.properties && matchSchema.properties) {
92199219
// 对象 oneOf 需要合并原属性和 oneOf 属性
@@ -9224,7 +9224,7 @@ function computeDefaults(_schema, parentDefaults, rootSchema) {
92249224
schema = matchSchema;
92259225
}
92269226
} else if ('anyOf' in schema) {
9227-
var _matchSchema = schema.anyOf[getMatchingOption(formData, schema.anyOf, rootSchema)];
9227+
var _matchSchema = retrieveSchema(schema.anyOf[getMatchingOption(formData, schema.anyOf, rootSchema)], rootSchema, formData);
92289228

92299229
if (schema.properties && _matchSchema.properties) {
92309230
// 对象 anyOf 需要合并原属性和 anyOf 属性

packages/lib/vue2/vue2-form-element/dist/vueJsonSchemaForm.esm.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/lib/vue2/vue2-form-element/dist/vueJsonSchemaForm.umd.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9078,7 +9078,7 @@
90789078

90799079
// eslint-disable-next-line no-plusplus
90809080
for (var i = 0; i < options.length; i++) {
9081-
var option = options[i]; // If the schema describes an object then we need to add slightly more
9081+
var option = retrieveSchema(options[i], rootSchema, formData); // If the schema describes an object then we need to add slightly more
90829082
// strict matching to the schema, because unless the schema uses the
90839083
// "requires" keyword, an object will match the schema as long as it
90849084
// doesn't have matching keys with a conflicting type. To do this we use an
@@ -9221,7 +9221,7 @@
92219221
return computeDefaults(itemSchema, Array.isArray(parentDefaults) ? parentDefaults[idx] : undefined, rootSchema, formData, includeUndefinedValues);
92229222
});
92239223
} else if ('oneOf' in schema) {
9224-
var matchSchema = schema.oneOf[getMatchingOption(formData, schema.oneOf, rootSchema)];
9224+
var matchSchema = retrieveSchema(schema.oneOf[getMatchingOption(formData, schema.oneOf, rootSchema)], rootSchema, formData);
92259225

92269226
if (schema.properties && matchSchema.properties) {
92279227
// 对象 oneOf 需要合并原属性和 oneOf 属性
@@ -9232,7 +9232,7 @@
92329232
schema = matchSchema;
92339233
}
92349234
} else if ('anyOf' in schema) {
9235-
var _matchSchema = schema.anyOf[getMatchingOption(formData, schema.anyOf, rootSchema)];
9235+
var _matchSchema = retrieveSchema(schema.anyOf[getMatchingOption(formData, schema.anyOf, rootSchema)], rootSchema, formData);
92369236

92379237
if (schema.properties && _matchSchema.properties) {
92389238
// 对象 anyOf 需要合并原属性和 anyOf 属性

packages/lib/vue2/vue2-form-element/dist/vueJsonSchemaForm.umd.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/lib/vue2/vue2-form-iview3/dist/vue2-form-iview3.esm.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9070,7 +9070,7 @@ function getMatchingOption(formData, options, rootSchema) {
90709070

90719071
// eslint-disable-next-line no-plusplus
90729072
for (var i = 0; i < options.length; i++) {
9073-
var option = options[i]; // If the schema describes an object then we need to add slightly more
9073+
var option = retrieveSchema(options[i], rootSchema, formData); // If the schema describes an object then we need to add slightly more
90749074
// strict matching to the schema, because unless the schema uses the
90759075
// "requires" keyword, an object will match the schema as long as it
90769076
// doesn't have matching keys with a conflicting type. To do this we use an
@@ -9213,7 +9213,7 @@ function computeDefaults(_schema, parentDefaults, rootSchema) {
92139213
return computeDefaults(itemSchema, Array.isArray(parentDefaults) ? parentDefaults[idx] : undefined, rootSchema, formData, includeUndefinedValues);
92149214
});
92159215
} else if ('oneOf' in schema) {
9216-
var matchSchema = schema.oneOf[getMatchingOption(formData, schema.oneOf, rootSchema)];
9216+
var matchSchema = retrieveSchema(schema.oneOf[getMatchingOption(formData, schema.oneOf, rootSchema)], rootSchema, formData);
92179217

92189218
if (schema.properties && matchSchema.properties) {
92199219
// 对象 oneOf 需要合并原属性和 oneOf 属性
@@ -9224,7 +9224,7 @@ function computeDefaults(_schema, parentDefaults, rootSchema) {
92249224
schema = matchSchema;
92259225
}
92269226
} else if ('anyOf' in schema) {
9227-
var _matchSchema = schema.anyOf[getMatchingOption(formData, schema.anyOf, rootSchema)];
9227+
var _matchSchema = retrieveSchema(schema.anyOf[getMatchingOption(formData, schema.anyOf, rootSchema)], rootSchema, formData);
92289228

92299229
if (schema.properties && _matchSchema.properties) {
92309230
// 对象 anyOf 需要合并原属性和 anyOf 属性

packages/lib/vue2/vue2-form-iview3/dist/vue2-form-iview3.esm.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/lib/vue2/vue2-form-iview3/dist/vue2-form-iview3.umd.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9078,7 +9078,7 @@
90789078

90799079
// eslint-disable-next-line no-plusplus
90809080
for (var i = 0; i < options.length; i++) {
9081-
var option = options[i]; // If the schema describes an object then we need to add slightly more
9081+
var option = retrieveSchema(options[i], rootSchema, formData); // If the schema describes an object then we need to add slightly more
90829082
// strict matching to the schema, because unless the schema uses the
90839083
// "requires" keyword, an object will match the schema as long as it
90849084
// doesn't have matching keys with a conflicting type. To do this we use an
@@ -9221,7 +9221,7 @@
92219221
return computeDefaults(itemSchema, Array.isArray(parentDefaults) ? parentDefaults[idx] : undefined, rootSchema, formData, includeUndefinedValues);
92229222
});
92239223
} else if ('oneOf' in schema) {
9224-
var matchSchema = schema.oneOf[getMatchingOption(formData, schema.oneOf, rootSchema)];
9224+
var matchSchema = retrieveSchema(schema.oneOf[getMatchingOption(formData, schema.oneOf, rootSchema)], rootSchema, formData);
92259225

92269226
if (schema.properties && matchSchema.properties) {
92279227
// 对象 oneOf 需要合并原属性和 oneOf 属性
@@ -9232,7 +9232,7 @@
92329232
schema = matchSchema;
92339233
}
92349234
} else if ('anyOf' in schema) {
9235-
var _matchSchema = schema.anyOf[getMatchingOption(formData, schema.anyOf, rootSchema)];
9235+
var _matchSchema = retrieveSchema(schema.anyOf[getMatchingOption(formData, schema.anyOf, rootSchema)], rootSchema, formData);
92369236

92379237
if (schema.properties && _matchSchema.properties) {
92389238
// 对象 anyOf 需要合并原属性和 anyOf 属性

packages/lib/vue2/vue2-form-iview3/dist/vue2-form-iview3.umd.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)