@nesso-how/schema 0.1.0-alpha.39 → 0.1.0-alpha.40

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.
Files changed (2) hide show
  1. package/dist/index.js +58 -25
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -15,8 +15,8 @@ export function serialize(doc) {
15
15
  ...(meta !== undefined && { meta }),
16
16
  }, null, 2);
17
17
  }
18
- function asRecord(value) {
19
- return typeof value === 'object' && value !== null ? value : null;
18
+ function isRecord(value) {
19
+ return typeof value === 'object' && value !== null;
20
20
  }
21
21
  /**
22
22
  * Parse and structurally validate a graph document. Files are user-editable (and
@@ -26,41 +26,74 @@ function asRecord(value) {
26
26
  */
27
27
  export function deserialize(json) {
28
28
  const data = JSON.parse(json);
29
- const root = asRecord(data);
30
- if (!root || !Array.isArray(root.concepts) || !Array.isArray(root.relations)) {
29
+ if (!isRecord(data) || !Array.isArray(data.concepts) || !Array.isArray(data.relations)) {
31
30
  throw new Error('Invalid Nesso graph document: missing concepts or relations array');
32
31
  }
33
- if (typeof root.version === 'number' && root.version !== GRAPH_FORMAT_VERSION) {
34
- throw new Error(`Unsupported Nesso graph document version: ${root.version}`);
32
+ if (typeof data.version === 'number' && data.version !== GRAPH_FORMAT_VERSION) {
33
+ throw new Error(`Unsupported Nesso graph document version: ${data.version}`);
35
34
  }
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)) {
35
+ const rawConcepts = data.concepts;
36
+ const concepts = rawConcepts.map((value, i) => {
37
+ if (!isRecord(value) ||
38
+ typeof value.id !== 'string' ||
39
+ value.id === '' ||
40
+ typeof value.label !== 'string' ||
41
+ typeof value.x !== 'number' ||
42
+ !Number.isFinite(value.x) ||
43
+ typeof value.y !== 'number' ||
44
+ !Number.isFinite(value.y)) {
46
45
  throw new Error(`Invalid Nesso graph document: concept ${i} is missing a valid id, label, or position`);
47
46
  }
48
- return concept;
47
+ const conceptData = value.data;
48
+ return {
49
+ id: value.id,
50
+ label: value.label,
51
+ x: value.x,
52
+ y: value.y,
53
+ ...(conceptData !== undefined && { data: conceptData }),
54
+ };
49
55
  });
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
+ const rawRelations = data.relations;
57
+ const relations = rawRelations.map((value, i) => {
58
+ if (!isRecord(value) ||
59
+ typeof value.id !== 'string' ||
60
+ typeof value.source !== 'string' ||
61
+ typeof value.target !== 'string') {
56
62
  throw new Error(`Invalid Nesso graph document: relation ${i} is missing id, source or target`);
57
63
  }
58
- return relation;
64
+ const relationType = typeof value.type === 'string' ? value.type : undefined;
65
+ const relationData = value.data;
66
+ return {
67
+ id: value.id,
68
+ source: value.source,
69
+ target: value.target,
70
+ ...(relationType !== undefined && { type: relationType }),
71
+ ...(relationData !== undefined && { data: relationData }),
72
+ };
59
73
  });
74
+ const name = typeof data.name === 'string' ? data.name : '';
75
+ const docId = typeof data.id === 'string' ? data.id : undefined;
76
+ const updatedAt = typeof data.updatedAt === 'number' ? data.updatedAt : undefined;
77
+ let vocabulary;
78
+ const vocabValue = data.vocabulary;
79
+ if (isRecord(vocabValue) &&
80
+ typeof vocabValue.id === 'string' &&
81
+ typeof vocabValue.version === 'string') {
82
+ vocabulary = { id: vocabValue.id, version: vocabValue.version };
83
+ }
84
+ let meta;
85
+ const metaValue = data.meta;
86
+ if (metaValue !== undefined) {
87
+ meta = metaValue;
88
+ }
60
89
  return {
61
- ...root,
62
90
  version: GRAPH_FORMAT_VERSION,
91
+ ...(vocabulary !== undefined && { vocabulary }),
92
+ ...(docId !== undefined && { id: docId }),
93
+ ...(updatedAt !== undefined && { updatedAt }),
94
+ name,
63
95
  concepts,
64
96
  relations,
97
+ ...(meta !== undefined && { meta }),
65
98
  };
66
99
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nesso-how/schema",
3
- "version": "0.1.0-alpha.39",
3
+ "version": "0.1.0-alpha.40",
4
4
  "description": "Nesso knowledge graph document schema — JSON serialize/deserialize",
5
5
  "license": "MIT",
6
6
  "repository": {