Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
21 changes: 21 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"editor.formatOnSave": true,
"[javascript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[typescript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[javascriptreact]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[typescriptreact]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[json]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[jsonc]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
}
}
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

75 changes: 75 additions & 0 deletions packages/graph-schema-utils/src/formatters/json/extensions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { describe, test } from "vitest";
import { readFile } from "../../../test/fs.utils.js";
import path from "path";
import { fromJson } from "./index.js";
import { strict as assert } from "node:assert";
import {
hasDuplicateNodeLabelIds,
hasDuplicateNodeObjectTypeIds,
} from "./extensions.js";
import { RootSchemaJsonStruct } from "./types.js";

describe("JSON formatter", () => {
describe("hasDuplicateNodeLabelIds", () => {
test("Identifies duplicated node labels", () => {
const schema = readFile(
path.resolve(__dirname, "./test-schemas/duplicated-nodeLabel-ids.json")
);
const schemaJson = JSON.parse(schema) as RootSchemaJsonStruct;

assert.strictEqual(
hasDuplicateNodeLabelIds(
schemaJson.graphSchemaRepresentation.graphSchema
),
true
);
});
});
describe("hasDuplicateNodeObjectTypeIds", () => {
test("Identifies duplicated node object types", () => {
const schema = readFile(
path.resolve(
__dirname,
"./test-schemas/duplicated-nodeObjectType-ids.json"
)
);
const schemaJson = JSON.parse(schema) as RootSchemaJsonStruct;

assert.strictEqual(
hasDuplicateNodeObjectTypeIds(
schemaJson.graphSchemaRepresentation.graphSchema
),
true
);
});
});
describe("fromJson", () => {
const schemaWithDuplicatedNodeLabels = readFile(
path.resolve(__dirname, "./test-schemas/duplicated-nodeLabel-ids.json")
);
test("Identifies duplicated node labels adn throws an error", () => {
assert.throws(() => fromJson(schemaWithDuplicatedNodeLabels), {
message: "Duplicate node label IDs found in schema",
});
});

test("Identifies duplicated node object types and throws an error", () => {
const schema = readFile(
path.resolve(
__dirname,
"./test-schemas/duplicated-nodeObjectType-ids.json"
)
);
assert.throws(() => fromJson(schema), {
message: "Duplicate node object type IDs found in schema",
});
});

test("Does not throw an error if there are no duplicated ids in node labels or node object types", () => {
const schema = readFile(
path.resolve(__dirname, "./test-schemas/full.json")
);
assert.doesNotThrow(() => fromJson(schema));
});
});
});
37 changes: 37 additions & 0 deletions packages/graph-schema-utils/src/formatters/json/extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
} from "../../model/index.js";
import {
ConstraintJsonStruct,
GraphSchemaJsonStruct,
IndexJsonStruct,
isLookupIndexJsonStruct,
isNodeLabelConstraintJsonStruct,
Expand Down Expand Up @@ -107,12 +108,48 @@ export function fromJson(schema: string): GraphSchema {
return fromJsonStruct(schemaJson);
}

export function hasDuplicateNodeLabelIds(
schema: GraphSchemaJsonStruct
): boolean {
const ids = new Set<string>();

for (const nodeLabel of schema.nodeLabels) {
if (ids.has(nodeLabel.$id)) {
return true;
}
ids.add(nodeLabel.$id);
}

return false;
}

export function hasDuplicateNodeObjectTypeIds(
schema: GraphSchemaJsonStruct
): boolean {
const ids = new Set<string>();

for (const nodeObjectType of schema.nodeObjectTypes) {
if (ids.has(nodeObjectType.$id)) {
return true;
}
ids.add(nodeObjectType.$id);
}

return false;
}

export function fromJsonStruct(schemaJson: RootSchemaJsonStruct): GraphSchema {
const { graphSchema } = schemaJson.graphSchemaRepresentation;
if (hasDuplicateNodeLabelIds(graphSchema)) {
throw new Error("Duplicate node label IDs found in schema");
}
const labels = graphSchema.nodeLabels.map(nodeLabel.create);
const relationshipTypes = graphSchema.relationshipTypes.map(
relationshipType.create
);
if (hasDuplicateNodeObjectTypeIds(graphSchema)) {
throw new Error("Duplicate node object type IDs found in schema");
}
const nodeObjectTypes = graphSchema.nodeObjectTypes.map(
(nodeObjectTypeJson) =>
nodeObjectType.create(nodeObjectTypeJson, (ref) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
{
"graphSchemaRepresentation": {
"version": "1.0.0",
"graphSchema": {
"nodeLabels": [
{
"$id": "nl:0",
"token": "DOCS_CHUNKS_TABLE",
"properties": [
{
"$id": "p:0_0",
"token": "RELATIVE_PATH",
"type": {
"type": "string"
},
"nullable": false
},
{
"$id": "p:0_1",
"token": "SIZE",
"type": {
"type": "integer"
},
"nullable": false
},
{
"$id": "p:0_2",
"token": "FILE_URL",
"type": {
"type": "string"
},
"nullable": false
},
{
"$id": "p:0_3",
"token": "SCOPED_FILE_URL",
"type": {
"type": "string"
},
"nullable": false
},
{
"$id": "p:0_4",
"token": "CHUNK",
"type": {
"type": "string"
},
"nullable": false
}
]
},
{
"$id": "nl:0",
"token": "DOCS_METADATA",
"properties": [
{
"$id": "p:0_0",
"token": "RELATIVE_PATH",
"type": {
"type": "string"
},
"nullable": false
},
{
"$id": "p:0_1",
"token": "MODEL_NAME",
"type": {
"type": "string"
},
"nullable": false
},
{
"$id": "p:0_2",
"token": "SKILLS",
"type": {
"type": "string"
},
"nullable": false
},
{
"$id": "p:0_3",
"token": "DOMAINS",
"type": {
"type": "string"
},
"nullable": false
},
{
"$id": "p:0_4",
"token": "SUMMARY",
"type": {
"type": "string"
},
"nullable": false
}
]
}
],
"relationshipTypes": [],
"nodeObjectTypes": [
{
"$id": "n:0",
"labels": [
{
"$ref": "#nl:0"
}
]
},
{
"$id": "n:0",
"labels": [
{
"$ref": "#nl:0"
}
]
}
],
"relationshipObjectTypes": [],
"constraints": [],
"indexes": []
}
}
}
Loading
Loading