nit: extract component zod obj
This commit is contained in:
parent
7ea9414a3b
commit
aff11bbdfd
1 changed files with 18 additions and 18 deletions
|
@ -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,24 +53,24 @@ export function createAstroNode(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function validateComponents(components: Record<string, AstroInstance['default']>) {
|
const componentsPropValidator = z.record(
|
||||||
|
z
|
||||||
|
.string()
|
||||||
|
.min(1, 'Invalid `components` prop. Component names cannot be empty!')
|
||||||
|
.refine(
|
||||||
|
(value) => isCapitalized(value),
|
||||||
|
(value) => ({
|
||||||
|
message: `Invalid \`components\` prop: ${JSON.stringify(
|
||||||
|
value
|
||||||
|
)}. Component name must be capitalized. If you want to render HTML elements as components, try using a Markdoc node [TODO: DOCS LINK]`,
|
||||||
|
})
|
||||||
|
),
|
||||||
|
z.any()
|
||||||
|
);
|
||||||
|
|
||||||
|
function validateComponentsProp(components: Record<string, AstroInstance['default']>) {
|
||||||
try {
|
try {
|
||||||
return z
|
return componentsPropValidator.parse(components);
|
||||||
.record(
|
|
||||||
z
|
|
||||||
.string()
|
|
||||||
.min(1, 'Invalid `components` prop. Component names cannot be empty!')
|
|
||||||
.refine(
|
|
||||||
(value) => isCapitalized(value),
|
|
||||||
(value) => ({
|
|
||||||
message: `Invalid \`components\` prop: ${JSON.stringify(
|
|
||||||
value
|
|
||||||
)}. Component name must be capitalized. If you want to render HTML elements as components, try using a Markdoc node [TODO: DOCS LINK]`,
|
|
||||||
})
|
|
||||||
),
|
|
||||||
z.any()
|
|
||||||
)
|
|
||||||
.parse(components);
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
throw new MarkdocError({
|
throw new MarkdocError({
|
||||||
message:
|
message:
|
||||||
|
|
Loading…
Reference in a new issue