11import { z } from 'zod' ;
2+ import { YamlConfig } from '../typings/Config' ;
23
34interface RouteObject {
45 slug ?: string ;
@@ -8,13 +9,34 @@ interface RouteObject {
89}
910
1011const routeObject : z . Schema < RouteObject > = z . late . object ( ( ) => ( {
11- name : z . string ( ) . regex ( / ^ [ A - Z ] ( [ A - Z a - z ] + ) / g, 'Name should be PascalCase with no numbers or special characters' ) ,
12+ name : z . string ( ) . regex ( / ^ [ A - Z ] ( [ A - Z a - z ] + ) / g, 'Name should be PascalCase with no numbers or special characters' ) ,
1213 type : z . enum ( [ 'basic' , 'layout' , 'pathless' , 'index' ] ) . default ( 'basic' ) ,
1314 slug : z . string ( ) . regex ( / ^ [ a - z ] ( [ A - Z a - z ] + ) / g, 'Slug should be camelCase with no numbers or special characters' ) . optional ( ) ,
1415 children : z . array ( routeObject ) . optional ( )
1516} ) ) ;
1617
17- export const routeSchema = z . object ( {
18+ export const configSchema = z . object ( {
1819 structure : z . enum ( [ 'co-locate' , 'split' ] , ) . default ( 'co-locate' ) ,
1920 routes : z . array ( routeObject )
20- } )
21+ } )
22+
23+ export type ScaffoldConfig = z . infer < typeof configSchema >
24+
25+ export const validateConfig = ( doc : YamlConfig ) : ScaffoldConfig => {
26+ const checks = configSchema . safeParse ( doc ) ;
27+
28+ if ( ! checks . success ) {
29+ throw checks . error . issues . reduce ( ( acc , curr ) => {
30+ let newError = `${ acc } \n` ;
31+ console . log ( JSON . stringify ( curr ) ) ;
32+
33+ curr . path . forEach ( ( path , index ) => {
34+ newError = `${ newError } \n${ new Array ( index + 2 ) . join ( '-' ) } ${ path } `
35+ } ) ;
36+
37+ return `${ newError } : ${ curr . message } ` ;
38+ } , '' )
39+ } ;
40+
41+ return checks . data ;
42+ }
0 commit comments