nit: extract component zod obj

This commit is contained in:
bholmesdev 2023-03-06 09:56:18 -05:00
parent 7ea9414a3b
commit aff11bbdfd

View file

@ -21,7 +21,7 @@ export function createAstroNode(
node: RenderableTreeNode, node: RenderableTreeNode,
components: Record<string, AstroInstance['default']> = {} components: Record<string, AstroInstance['default']> = {}
): AstroNode { ): AstroNode {
components = validateComponents(components); components = validateComponentsProp(components);
if (typeof node === 'string' || typeof node === 'number') { if (typeof node === 'string' || typeof node === 'number') {
return String(node); return String(node);
@ -53,10 +53,7 @@ export function createAstroNode(
} }
} }
function validateComponents(components: Record<string, AstroInstance['default']>) { const componentsPropValidator = z.record(
try {
return z
.record(
z z
.string() .string()
.min(1, 'Invalid `components` prop. Component names cannot be empty!') .min(1, 'Invalid `components` prop. Component names cannot be empty!')
@ -69,8 +66,11 @@ function validateComponents(components: Record<string, AstroInstance['default']>
}) })
), ),
z.any() z.any()
) );
.parse(components);
function validateComponentsProp(components: Record<string, AstroInstance['default']>) {
try {
return componentsPropValidator.parse(components);
} catch (e) { } catch (e) {
throw new MarkdocError({ throw new MarkdocError({
message: message: