Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions packages/utils/src/schema/retrieveSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,9 +412,24 @@ export function retrieveSchemaInternal<
return [...(allOf as S[]), restOfSchema as S];
}
try {
const withContainsSchemas = [] as S[];
const withoutContainsSchemas = [] as S[];
resolvedSchema.allOf?.forEach((s) => {
if (typeof s === 'object' && s.contains) {
withContainsSchemas.push(s as S);
} else {
withoutContainsSchemas.push(s as S);
}
});
if (withContainsSchemas.length) {
resolvedSchema = { ...resolvedSchema, allOf: withoutContainsSchemas };
}
resolvedSchema = mergeAllOf(resolvedSchema, {
deep: false,
} as Options) as S;
if (withContainsSchemas.length) {
resolvedSchema.allOf = withContainsSchemas;
}
} catch (e) {
console.warn('could not merge subschemas in allOf:\n', e);
const { allOf, ...resolvedSchemaWithoutAllOf } = resolvedSchema;
Expand Down
75 changes: 75 additions & 0 deletions packages/utils/test/schema/retrieveSchemaTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,81 @@ export default function retrieveSchemaTest(testValidator: TestValidatorType) {
type: 'string',
});
});
it('should not merge `allOf.contains` schemas', () => {
// https://github.com/rjsf-team/react-jsonschema-form/issues/2923#issuecomment-1946034240
const schema: RJSFSchema = {
type: 'array',
items: {
type: 'object',
properties: {
a: {
type: 'string',
},
},
},
allOf: [
{
maxItems: 5,
},
{
contains: {
type: 'object',
properties: {
a: {
pattern: '1',
},
},
},
},
{
contains: {
type: 'object',
properties: {
a: {
pattern: '2',
},
},
},
},
],
};
const rootSchema: RJSFSchema = { definitions: {} };
const formData = {};
expect(retrieveSchema(testValidator, schema, rootSchema, formData)).toEqual({
type: 'array',
items: {
type: 'object',
properties: {
a: {
type: 'string',
},
},
},
maxItems: 5,
allOf: [
{
contains: {
type: 'object',
properties: {
a: {
pattern: '1',
},
},
},
},
{
contains: {
type: 'object',
properties: {
a: {
pattern: '2',
},
},
},
},
],
});
});
it('should not merge incompatible types', () => {
const schema: RJSFSchema = {
allOf: [{ type: 'string' }, { type: 'boolean' }],
Expand Down