Skip to content

Commit 0fde1d0

Browse files
committed
Add slantNodeWithFix schema
1 parent 6eb7b0f commit 0fde1d0

1 file changed

Lines changed: 47 additions & 36 deletions

File tree

packages/api-graph/src/private/schemas/colorNode.ts

Lines changed: 47 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import {
22
array,
3+
looseObject,
34
minLength,
45
null_,
56
objectWithRest,
67
optional,
78
parse,
89
pipe,
910
string,
11+
transform,
1012
union,
1113
type ErrorMessage,
1214
type InferOutput,
@@ -72,6 +74,49 @@ type SlantNode = InferOutput<ObjectSchema<ReturnType<typeof slantNode>['entries'
7274
[key: string]: unknown;
7375
};
7476

77+
function slantNodeWithFix() {
78+
return pipe(
79+
looseObject({}),
80+
transform(node => {
81+
const propertyMap = new Map<string, readonly (Literal | NodeReference)[]>();
82+
let context: string | undefined;
83+
let id: string | undefined;
84+
85+
for (const [key, value] of Object.entries(node)) {
86+
const parsedValue = parse(
87+
union([array(union([literal(), nodeReference()])), nodeReference(), literal(), null_()]),
88+
value
89+
);
90+
91+
switch (key) {
92+
case '@context':
93+
context = parse(string(), value);
94+
break;
95+
96+
case '@id':
97+
id = parse(string(), value);
98+
break;
99+
100+
default: {
101+
const slantedValue = Array.isArray(parsedValue)
102+
? parsedValue
103+
: Object.freeze(parsedValue === null ? [] : [parsedValue]);
104+
105+
slantedValue.length && propertyMap.set(key, slantedValue);
106+
107+
break;
108+
}
109+
}
110+
}
111+
112+
return parse(
113+
slantNode(),
114+
Object.fromEntries([...(context ? [['@context', context]] : []), ['@id', id], ...Array.from(propertyMap)])
115+
);
116+
})
117+
);
118+
}
119+
75120
/**
76121
* Put our opinions into the node.
77122
*
@@ -96,42 +141,8 @@ type SlantNode = InferOutput<ObjectSchema<ReturnType<typeof slantNode>['entries'
96141
* @returns An opinionated node object which conforms to JSON-LD specification.
97142
*/
98143
function colorNode(node: FlatNodeObject | SlantNode): SlantNode {
99-
const propertyMap = new Map<string, readonly (Literal | NodeReference)[]>();
100-
let context: string | undefined;
101-
let id: string | undefined;
102-
103-
for (const [key, value] of Object.entries(node)) {
104-
const parsedValue = parse(
105-
union([array(union([literal(), nodeReference()])), nodeReference(), literal(), null_()]),
106-
value
107-
);
108-
109-
switch (key) {
110-
case '@context':
111-
context = parse(string(), value);
112-
break;
113-
114-
case '@id':
115-
id = parse(string(), value);
116-
break;
117-
118-
default: {
119-
const slantedValue = Array.isArray(parsedValue)
120-
? parsedValue
121-
: Object.freeze(parsedValue === null ? [] : [parsedValue]);
122-
123-
slantedValue.length && propertyMap.set(key, slantedValue);
124-
125-
break;
126-
}
127-
}
128-
}
129-
130-
return parse(
131-
slantNode(),
132-
Object.fromEntries([...(context ? [['@context', context]] : []), ['@id', id], ...Array.from(propertyMap)])
133-
);
144+
return parse(slantNodeWithFix(), node);
134145
}
135146

136147
export default colorNode;
137-
export { slantNode, type SlantNode };
148+
export { slantNode, slantNodeWithFix, type SlantNode };

0 commit comments

Comments
 (0)