@nesso-how/schema 0.1.0-alpha.34

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Omar Desogus
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,32 @@
1
+ # @nesso-how/schema
2
+
3
+ Vocabulary-agnostic JSON schema for [Nesso](https://nesso.how) knowledge graph documents.
4
+
5
+ Defines the on-disk envelope (`version`, optional `vocabulary`, `name`, `concepts`, `relations`, optional `meta`) and structural validation on parse. Relation type ids and concept `data` shapes are opaque here — vocabulary packages add typed aliases and extra validation.
6
+
7
+ ## Install
8
+
9
+ ```bash
10
+ npm install @nesso-how/schema
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ```ts
16
+ import { GRAPH_FORMAT_VERSION, deserialize, serialize, type GraphDocument } from '@nesso-how/schema'
17
+
18
+ const json = serialize({
19
+ name: 'My graph',
20
+ vocabulary: { id: '@nesso-how/vocab-learning', version: '0.1.0' },
21
+ concepts: [{ id: 'n1', label: 'Idea', x: 0, y: 0 }],
22
+ relations: [],
23
+ })
24
+ const doc: GraphDocument = deserialize(json)
25
+ // doc.version === GRAPH_FORMAT_VERSION
26
+ ```
27
+
28
+ Vocabulary-specific serialization (e.g. Nesso Learning Vocabulary) lives in [`@nesso-how/vocab-learning`](../vocab-learning/README.md).
29
+
30
+ ## License
31
+
32
+ MIT
@@ -0,0 +1,47 @@
1
+ /** Current on-disk graph document schema version. Bump only when the envelope shape changes. */
2
+ export declare const GRAPH_FORMAT_VERSION: 1;
3
+ export interface GraphConcept<D extends Record<string, unknown> = Record<string, unknown>> {
4
+ id: string;
5
+ label: string;
6
+ x: number;
7
+ y: number;
8
+ data?: D;
9
+ }
10
+ export interface GraphRelation<D extends Record<string, unknown> = Record<string, unknown>> {
11
+ id: string;
12
+ source: string;
13
+ target: string;
14
+ type?: string;
15
+ data?: D;
16
+ }
17
+ export interface GraphDocument<NC extends Record<string, unknown> = Record<string, unknown>, RE extends Record<string, unknown> = Record<string, unknown>, M extends Record<string, unknown> = Record<string, unknown>> {
18
+ /** File format version — always {@link GRAPH_FORMAT_VERSION} for the current schema. */
19
+ version: typeof GRAPH_FORMAT_VERSION;
20
+ /**
21
+ * Vocabulary this graph was built with: its id (e.g. `@nesso-how/vocab-learning`)
22
+ * and normative version. Opaque to schema, which only round-trips the reference.
23
+ */
24
+ vocabulary?: {
25
+ id: string;
26
+ version: string;
27
+ };
28
+ /** Internal graph id (desktop sync); omitted in manual exports. */
29
+ id?: string;
30
+ /** Last save time (Unix ms); desktop sync metadata. */
31
+ updatedAt?: number;
32
+ name: string;
33
+ concepts: GraphConcept<NC>[];
34
+ relations: GraphRelation<RE>[];
35
+ meta?: M;
36
+ }
37
+ /** Graph payload for {@link serialize} — format version is injected by the serializer. */
38
+ export type GraphDocumentInput<NC extends Record<string, unknown> = Record<string, unknown>, RE extends Record<string, unknown> = Record<string, unknown>, M extends Record<string, unknown> = Record<string, unknown>> = Omit<GraphDocument<NC, RE, M>, 'version'>;
39
+ /** Serialize a graph document to JSON with keys in a stable, human-readable order. */
40
+ export declare function serialize<NC extends Record<string, unknown>, RE extends Record<string, unknown>, M extends Record<string, unknown>>(doc: GraphDocumentInput<NC, RE, M>): string;
41
+ /**
42
+ * Parse and structurally validate a graph document. Files are user-editable (and
43
+ * importable from anywhere), so each element is checked before it can reach
44
+ * the store: a concept without a valid id/position crashes the canvas and would
45
+ * then be re-persisted. Vocabulary-specific normalization is the caller's responsibility.
46
+ */
47
+ export declare function deserialize<NC extends Record<string, unknown> = Record<string, unknown>, RE extends Record<string, unknown> = Record<string, unknown>, M extends Record<string, unknown> = Record<string, unknown>>(json: string): GraphDocument<NC, RE, M>;
package/dist/index.js ADDED
@@ -0,0 +1,66 @@
1
+ // SPDX-License-Identifier: MIT
2
+ /** Current on-disk graph document schema version. Bump only when the envelope shape changes. */
3
+ export const GRAPH_FORMAT_VERSION = 1;
4
+ /** Serialize a graph document to JSON with keys in a stable, human-readable order. */
5
+ export function serialize(doc) {
6
+ const { vocabulary, id, updatedAt, name, concepts, relations, meta } = doc;
7
+ return JSON.stringify({
8
+ version: GRAPH_FORMAT_VERSION,
9
+ ...(vocabulary !== undefined && { vocabulary }),
10
+ ...(id !== undefined && { id }),
11
+ ...(updatedAt !== undefined && { updatedAt }),
12
+ name,
13
+ concepts,
14
+ relations,
15
+ ...(meta !== undefined && { meta }),
16
+ }, null, 2);
17
+ }
18
+ function asRecord(value) {
19
+ return typeof value === 'object' && value !== null ? value : null;
20
+ }
21
+ /**
22
+ * Parse and structurally validate a graph document. Files are user-editable (and
23
+ * importable from anywhere), so each element is checked before it can reach
24
+ * the store: a concept without a valid id/position crashes the canvas and would
25
+ * then be re-persisted. Vocabulary-specific normalization is the caller's responsibility.
26
+ */
27
+ export function deserialize(json) {
28
+ const data = JSON.parse(json);
29
+ const root = asRecord(data);
30
+ if (!root || !Array.isArray(root.concepts) || !Array.isArray(root.relations)) {
31
+ throw new Error('Invalid Nesso graph document: missing concepts or relations array');
32
+ }
33
+ if (typeof root.version === 'number' && root.version !== GRAPH_FORMAT_VERSION) {
34
+ throw new Error(`Unsupported Nesso graph document version: ${root.version}`);
35
+ }
36
+ const concepts = root.concepts.map((value, i) => {
37
+ const concept = asRecord(value);
38
+ if (!concept ||
39
+ typeof concept.id !== 'string' ||
40
+ concept.id === '' ||
41
+ typeof concept.label !== 'string' ||
42
+ typeof concept.x !== 'number' ||
43
+ !Number.isFinite(concept.x) ||
44
+ typeof concept.y !== 'number' ||
45
+ !Number.isFinite(concept.y)) {
46
+ throw new Error(`Invalid Nesso graph document: concept ${i} is missing a valid id, label, or position`);
47
+ }
48
+ return concept;
49
+ });
50
+ const relations = root.relations.map((value, i) => {
51
+ const relation = asRecord(value);
52
+ if (!relation ||
53
+ typeof relation.id !== 'string' ||
54
+ typeof relation.source !== 'string' ||
55
+ typeof relation.target !== 'string') {
56
+ throw new Error(`Invalid Nesso graph document: relation ${i} is missing id, source or target`);
57
+ }
58
+ return relation;
59
+ });
60
+ return {
61
+ ...root,
62
+ version: GRAPH_FORMAT_VERSION,
63
+ concepts,
64
+ relations,
65
+ };
66
+ }
package/package.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "@nesso-how/schema",
3
+ "version": "0.1.0-alpha.34",
4
+ "description": "Nesso knowledge graph document schema — JSON serialize/deserialize",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://github.com/nesso-how/nesso",
9
+ "directory": "packages/schema"
10
+ },
11
+ "type": "module",
12
+ "publishConfig": {
13
+ "access": "public"
14
+ },
15
+ "files": [
16
+ "dist"
17
+ ],
18
+ "exports": {
19
+ ".": {
20
+ "types": "./dist/index.d.ts",
21
+ "import": "./dist/index.js"
22
+ }
23
+ },
24
+ "devDependencies": {
25
+ "typescript": "~5.8.3"
26
+ },
27
+ "scripts": {
28
+ "build": "rm -rf dist && tsc",
29
+ "dev": "tsc --watch"
30
+ }
31
+ }