Skip to content

Commit 54e086a

Browse files
committed
field validation
1 parent ce70661 commit 54e086a

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

packages/compass-data-modeling/src/components/drawer/field-drawer-content.tsx

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { BSONType } from 'mongodb';
1313
import {
1414
createNewRelationship,
1515
deleteRelationship,
16+
extractFieldsFromSchema,
1617
renameField,
1718
selectCurrentModelFromState,
1819
selectRelationship,
@@ -74,7 +75,15 @@ export function getIsFieldNameValid(
7475
}
7576

7677
const fieldsNamesWithoutCurrent = existingFields
77-
.filter((fieldPath) => !areFieldPathsEqual(fieldPath, currentFieldPath))
78+
.filter(
79+
(fieldPath) =>
80+
fieldPath.length === currentFieldPath.length &&
81+
areFieldPathsEqual(
82+
fieldPath.slice(0, fieldPath.length - 1),
83+
currentFieldPath.slice(0, fieldPath.length - 1)
84+
) &&
85+
!areFieldPathsEqual(fieldPath, currentFieldPath)
86+
)
7887
.map((fieldPath) => fieldPath[fieldPath.length - 1]);
7988

8089
const isDuplicate = fieldsNamesWithoutCurrent.some(
@@ -179,16 +188,19 @@ export default connect(
179188
ownProps: { namespace: string; fieldPath: FieldPath }
180189
) => {
181190
const model = selectCurrentModelFromState(state);
191+
const collectionSchema = model.collections.find(
192+
(collection) => collection.ns === ownProps.namespace
193+
)?.jsonSchema;
194+
if (!collectionSchema) {
195+
throw new Error('Collection not found');
196+
}
182197
return {
183198
types:
184199
getFieldFromSchema({
185-
jsonSchema:
186-
model.collections.find(
187-
(collection) => collection.ns === ownProps.namespace
188-
)?.jsonSchema ?? {},
200+
jsonSchema: collectionSchema,
189201
fieldPath: ownProps.fieldPath,
190202
})?.fieldTypes ?? [],
191-
fieldPaths: [], // TODO(COMPASS-9659): get existing field paths
203+
fieldPaths: extractFieldsFromSchema(collectionSchema),
192204
relationships: model.relationships.filter(({ relationship }) =>
193205
isRelationshipOfAField(
194206
relationship,

packages/compass-data-modeling/src/store/diagram.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
type StaticModel,
1515
} from '../services/data-model-storage';
1616
import { AnalysisProcessActionTypes } from './analysis-process';
17-
import { memoize, update } from 'lodash';
17+
import { memoize } from 'lodash';
1818
import type { DataModelingState, DataModelingThunkAction } from './reducer';
1919
import {
2020
openToast,
@@ -31,9 +31,7 @@ import { collectionToDiagramNode } from '../utils/nodes-and-edges';
3131
import toNS from 'mongodb-ns';
3232
import { traverseSchema, updateSchema } from '../utils/schema-traversal';
3333
import {
34-
areFieldPathsEqual,
3534
isRelationshipInvolvingField,
36-
isRelationshipOfAField,
3735
isSameFieldOrChild,
3836
} from '../utils/utils';
3937

@@ -1018,7 +1016,9 @@ export const selectCurrentModelFromState = (state: DataModelingState) => {
10181016
return selectCurrentModel(selectCurrentDiagramFromState(state).edits);
10191017
};
10201018

1021-
function extractFieldsFromSchema(parentSchema: MongoDBJSONSchema): FieldPath[] {
1019+
export function extractFieldsFromSchema(
1020+
parentSchema: MongoDBJSONSchema
1021+
): FieldPath[] {
10221022
const fields: FieldPath[] = [];
10231023
traverseSchema({
10241024
jsonSchema: parentSchema,

0 commit comments

Comments
 (0)