11import contentRaw from "$content/guide.yaml" ;
2- import YAML from "yaml" ;
32
43export const CONTENT = contentRaw as Content ;
54
@@ -11,63 +10,3 @@ export interface Step {
1110}
1211
1312export type Content = Record < string , Step > ;
14-
15- // TODO: An editor/validator that would use this?
16- /** Validates content and returns an error or undefined. */
17- export function getContentError ( content : string ) : string | undefined {
18- let data : Content ;
19-
20- try {
21- data = YAML . parse ( content ) ;
22- } catch ( e ) {
23- // Return yaml error as string
24- if ( e instanceof YAML . YAMLError ) {
25- return e . message ;
26- }
27- // re-throw anything else
28- throw e ;
29- }
30-
31- try {
32- validateData ( data ) ;
33- } catch ( e ) {
34- if ( typeof e === "string" ) return e ;
35- throw e ;
36- }
37- }
38-
39- function validateData ( data : Content ) {
40- if ( typeof data !== "object" || Array . isArray ( data ) ) {
41- throw "Content must be object!" ;
42- }
43-
44- if ( ! ensure ( data , "start" ) ) throw 'Missing "start" step!' ;
45-
46- // validate steps
47- for ( const [ name , step ] of Object . entries ( data ) ) {
48- if ( ! ( "title" in step ) || ! step . title ) {
49- throw `Step '${ name } ' is missing a title!` ;
50- }
51-
52- // validate each option if any
53- if ( "options" in step && step . options ) {
54- for ( let i = 0 ; i < step . options . length ; i ++ ) {
55- const option = step . options [ i ] ;
56-
57- if ( ! ensure ( option , "label" ) ) {
58- throw `Option ${ i } of step '${ name } ' is missing a label!` ;
59- }
60- if ( ! ensure ( option , "target" ) ) {
61- throw `Option ${ i } ('${ option . label } ') of step '${ name } ' is missing a target!` ;
62- }
63- if ( ! ensure ( data , option . target ) ) {
64- throw `Option ${ i } ('${ option . label } ') of step '${ name } ' is targeting step '${ option . target } ', which does not exist!` ;
65- }
66- }
67- }
68- }
69- }
70-
71- function ensure ( container : Record < string , unknown > , what : string ) {
72- return what in container && ! ! container [ what ] ;
73- }
0 commit comments