Skip to content

Commit

Permalink
fix: check for keys in nm relations and more than one key (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenmate authored Jul 7, 2024
1 parent e579f80 commit 9f85f1c
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/components/DiagramEditor/DiagramEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -1702,6 +1702,14 @@ export default function App(props) {
messages.push(
"Hay entidades sin clave primaria.",
);
if (!diagnostics.noEntitiesWithMoreThanOnePK)
messages.push(
"Hay entidades con más de una clave primaria.",
);
if (!diagnostics.noNMRelationsWithPK)
messages.push(
"Hay relaciones N-M con clave primaria.",
);
if (!diagnostics.noUnconnectedRelations)
messages.push("Hay relaciones desconectadas.");
if (!diagnostics.noNotValidCardinalities)
Expand Down
49 changes: 49 additions & 0 deletions src/utils/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ export function validateGraph(graph) {
noRepeatedAttrNames: true,
noEntitiesWithoutAttributes: true,
noEntitiesWithoutPK: true,
noEntitiesWithMoreThanOnePK: true,
noNMRelationsWithPK: true,
noUnconnectedRelations: true,
noNotValidCardinalities: true,
notEmpty: true,
Expand Down Expand Up @@ -40,6 +42,18 @@ export function validateGraph(graph) {
diagnostics.isValid = false;
}

// Check for entities with more than one primary key
if (entitiesWithMoreThanOnePK(graph)) {
diagnostics.noEntitiesWithMoreThanOnePK = false;
diagnostics.isValid = false;
}

// Check for NM relations with a primary key
if (nmRelationsWithPK(graph)) {
diagnostics.noNMRelationsWithPK = false;
diagnostics.isValid = false;
}

// Check for unconnected relations
if (relationsUnconnected(graph)) {
diagnostics.noUnconnectedRelations = false;
Expand Down Expand Up @@ -141,6 +155,41 @@ export function entitiesWithoutPK(graph) {
return false;
}

// True if there is an entity that has two or more keys
export function entitiesWithMoreThanOnePK(graph) {
for (const entity of graph.entities) {
let primaryKeyCount = 0;
for (const attribute of entity.attributes) {
if (attribute.key) {
primaryKeyCount++;
// If more than one primary key is found, return true
if (primaryKeyCount > 1) {
return true;
}
}
}
}
// If no entity with more than one primary key is found, return false
return false;
}

// True if there is an N:M relation that has a key
export function nmRelationsWithPK(graph) {
for (const relation of graph.relations) {
// Check if the relation is of type N:M
if (relation.canHaveAttributes) {
for (const attribute of relation.attributes) {
// If any attribute has key set to true, return true
if (attribute.key) {
return true;
}
}
}
}
// If no N:M relation with a key is found, return false
return false;
}

export function entitiesWithoutAttributes(graph) {
// Check entities
for (const entity of graph.entities) {
Expand Down

0 comments on commit 9f85f1c

Please sign in to comment.