Skip to content

Commit f1a9036

Browse files
authored
fix(compass-data-modeling): keep required up to date COMPASS-9659 (#7255)
1 parent f8370d2 commit f1a9036

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

packages/compass-data-modeling/src/utils/schema-traversal.spec.tsx

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,23 @@ describe('removeField', function () {
518518
},
519519
});
520520
});
521+
522+
it('clean up required', function () {
523+
const schema = {
524+
bsonType: 'object',
525+
properties: {
526+
name: { bsonType: 'string' },
527+
age: { bsonType: ['string', 'int'] },
528+
},
529+
required: ['name', 'age'],
530+
};
531+
const result = updateSchema({
532+
fieldPath: ['name'],
533+
jsonSchema: schema,
534+
update: 'removeField',
535+
});
536+
expect(result.required).to.deep.equal(['age']);
537+
});
521538
});
522539

523540
describe('nested schema', function () {
@@ -797,6 +814,24 @@ describe('renameField', function () {
797814
},
798815
});
799816
});
817+
818+
it('update required', function () {
819+
const schema = {
820+
bsonType: 'object',
821+
properties: {
822+
name: { bsonType: 'string' },
823+
age: { bsonType: ['string', 'int'] },
824+
},
825+
required: ['name', 'age'],
826+
};
827+
const result = updateSchema({
828+
fieldPath: ['name'],
829+
jsonSchema: schema,
830+
update: 'renameField',
831+
newFieldName: 'newName',
832+
});
833+
expect(result.required).to.deep.equal(['newName', 'age']);
834+
});
800835
});
801836

802837
describe('nested schema', function () {

packages/compass-data-modeling/src/utils/schema-traversal.tsx

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,26 +184,40 @@ const applySchemaUpdate = ({
184184
case 'removeField': {
185185
if (!schema.properties || !schema.properties[fieldName])
186186
throw new Error('Field to remove does not exist');
187-
return {
187+
const newSchema = {
188188
...schema,
189189
properties: Object.fromEntries(
190190
Object.entries(schema.properties).filter(([key]) => key !== fieldName)
191191
),
192192
};
193+
// clean up required if needed
194+
if (newSchema.required && Array.isArray(newSchema.required)) {
195+
newSchema.required = newSchema.required.filter(
196+
(key) => key !== fieldName
197+
);
198+
}
199+
return newSchema;
193200
}
194201
case 'renameField': {
195202
if (!schema.properties || !schema.properties[fieldName])
196203
throw new Error('Field to rename does not exist');
197204
if (!newFieldName)
198205
throw new Error('New field name is required for the rename operation');
199-
return {
206+
const newSchema = {
200207
...schema,
201208
properties: Object.fromEntries(
202209
Object.entries(schema.properties).map(([key, value]) =>
203210
key === fieldName ? [newFieldName, value] : [key, value]
204211
)
205212
),
206213
};
214+
// update required if needed
215+
if (newSchema.required && Array.isArray(newSchema.required)) {
216+
newSchema.required = newSchema.required.map((key) =>
217+
key !== fieldName ? key : newFieldName
218+
);
219+
}
220+
return newSchema;
207221
}
208222
default:
209223
return schema;

0 commit comments

Comments
 (0)