Skip to content

Commit deecb64

Browse files
Finish validation of config file, and return the object
1 parent ab78d6b commit deecb64

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

src/commands/generate.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import fs from 'fs';
44
import yaml from 'js-yaml';
55
import chalk from 'chalk';
66
import { YamlConfig } from '../typings/Config';
7-
import { routeSchema } from '../utils/routeSchema';
7+
import { validateConfig } from '../utils/routeSchema';
88

99
type Options = {
1010
definition: string;
@@ -49,8 +49,8 @@ export const handler = (argv: Arguments<Options>): void => {
4949
}
5050

5151
const doc = yaml.load(fs.readFileSync(defPath, 'utf8'), { json: true }) as YamlConfig;
52-
const checks = routeSchema.safeParse(doc);
53-
console.log(checks.success);
52+
const config = validateConfig(doc);
53+
//WE HAVE A VALID AND FORMATTED ROUTES OBJECT
5454

5555
} catch (e) {
5656
console.log(chalk.red(e));

src/utils/routeSchema.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { z } from 'zod';
2+
import { YamlConfig } from '../typings/Config';
23

34
interface RouteObject {
45
slug?: string;
@@ -8,13 +9,34 @@ interface RouteObject {
89
}
910

1011
const routeObject: z.Schema<RouteObject> = z.late.object(() => ({
11-
name: z.string().regex(/^[A-Z]([A-Za-z]+)/g, 'Name should be PascalCase with no numbers or special characters'),
12+
name: z.string().regex(/^[A-Z]([A-Za-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-Za-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

Comments
 (0)