Skip to content

Commit 080f235

Browse files
committed
refactoring: introduce componentSpecFromYaml
1 parent 304f7b9 commit 080f235

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/services/componentService.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import {
3636
saveComponent,
3737
type UserComponent,
3838
} from "@/utils/localforage";
39+
import { componentSpecFromYaml } from "@/utils/yaml";
3940

4041
export interface ExistingAndNewComponent {
4142
existingComponent: UserComponent | undefined;
@@ -445,7 +446,7 @@ async function hydrateFromPartialContentfulComponentReference(
445446
: component.text;
446447

447448
const spec = isTextOnlyComponentReference(component)
448-
? (yaml.load(component.text) as ComponentSpec)
449+
? componentSpecFromYaml(component.text)
449450
: component.spec;
450451

451452
if (!text || !spec) {

src/utils/yaml.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import type { ComponentSpec } from "./componentSpec";
1+
import yaml from "js-yaml";
2+
3+
import { type ComponentSpec, isValidComponentSpec } from "./componentSpec";
24
import { componentSpecToText } from "./componentStore";
35

46
const copyToYaml = (
@@ -14,4 +16,16 @@ const copyToYaml = (
1416
);
1517
};
1618

19+
export function componentSpecFromYaml(yamlText: string): ComponentSpec {
20+
const loadedSpec = yaml.load(yamlText);
21+
if (typeof loadedSpec !== "object" || loadedSpec === null) {
22+
throw new Error("Invalid component specification format");
23+
}
24+
if (!isValidComponentSpec(loadedSpec)) {
25+
throw new Error("Invalid component specification format");
26+
}
27+
28+
return loadedSpec;
29+
}
30+
1731
export default copyToYaml;

0 commit comments

Comments
 (0)