Skip to content

Commit

Permalink
fix(lib): 修复anyOf下多级对象初始值计算错误问题
Browse files Browse the repository at this point in the history
fix #57
  • Loading branch information
lljj-x committed Apr 10, 2021
1 parent 8f9da99 commit 6dd9780
Show file tree
Hide file tree
Showing 18 changed files with 45 additions and 36 deletions.
14 changes: 11 additions & 3 deletions packages/lib/utils/schema/getDefaultFormState.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,11 @@ function computeDefaults(
includeUndefinedValues
));
} else if ('oneOf' in schema) {
const matchSchema = schema.oneOf[getMatchingOption(formData, schema.oneOf, rootSchema)];
const matchSchema = retrieveSchema(
schema.oneOf[getMatchingOption(formData, schema.oneOf, rootSchema)],
rootSchema,
formData
);
if (schema.properties && matchSchema.properties) {
// 对象 oneOf 需要合并原属性和 oneOf 属性
const mergeSchema = mergeObjects(schema, matchSchema);
Expand All @@ -106,7 +110,12 @@ function computeDefaults(
schema = matchSchema;
}
} else if ('anyOf' in schema) {
const matchSchema = schema.anyOf[getMatchingOption(formData, schema.anyOf, rootSchema)];
const matchSchema = retrieveSchema(
schema.anyOf[getMatchingOption(formData, schema.anyOf, rootSchema)],
rootSchema,
formData
);

if (schema.properties && matchSchema.properties) {
// 对象 anyOf 需要合并原属性和 anyOf 属性
const mergeSchema = mergeObjects(schema, matchSchema);
Expand All @@ -116,7 +125,6 @@ function computeDefaults(
schema = matchSchema;
}
}

// Not defaults defined for this node, fallback to generic typed ones.
if (typeof defaults === 'undefined') {
defaults = schema.default;
Expand Down
3 changes: 2 additions & 1 deletion packages/lib/utils/schema/validate.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Ajv from 'ajv';
import i18n from '../i18n';
import retrieveSchema from './retriev';

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

// If the schema describes an object then we need to add slightly more
// strict matching to the schema, because unless the schema uses the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9070,7 +9070,7 @@ function getMatchingOption(formData, options, rootSchema) {

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

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

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

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -9078,7 +9078,7 @@

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

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

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

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -9070,7 +9070,7 @@ function getMatchingOption(formData, options, rootSchema) {

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

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

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

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -9078,7 +9078,7 @@

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

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

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

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions packages/lib/vue3/vue3-form-ant/dist/vue3-form-ant.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -9068,7 +9068,7 @@ function getMatchingOption(formData, options, rootSchema) {

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

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

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

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions packages/lib/vue3/vue3-form-ant/dist/vue3-form-ant.umd.js
Original file line number Diff line number Diff line change
Expand Up @@ -9072,7 +9072,7 @@

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

if (schema.properties && matchSchema.properties) {
// 对象 oneOf 需要合并原属性和 oneOf 属性
Expand All @@ -9226,7 +9226,7 @@
schema = matchSchema;
}
} else if ('anyOf' in schema) {
var _matchSchema = schema.anyOf[getMatchingOption(formData, schema.anyOf, rootSchema)];
var _matchSchema = retrieveSchema(schema.anyOf[getMatchingOption(formData, schema.anyOf, rootSchema)], rootSchema, formData);

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

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -9114,7 +9114,7 @@ function getMatchingOption(formData, options, rootSchema) {

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

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

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

0 comments on commit 6dd9780

Please sign in to comment.