Skip to content

Commit

Permalink
[ci] format
Browse files Browse the repository at this point in the history
  • Loading branch information
ascorbic authored and astrobot-houston committed Jan 22, 2025
1 parent 8f520f1 commit a90f79f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 20 deletions.
12 changes: 5 additions & 7 deletions packages/astro/src/content/content-layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { promises as fs, existsSync } from 'node:fs';
import PQueue from 'p-queue';
import type { FSWatcher } from 'vite';
import xxhash from 'xxhash-wasm';
import type { z } from 'zod';
import { AstroError, AstroErrorData } from '../core/errors/index.js';
import type { Logger } from '../core/logger/core.js';
import type { AstroSettings } from '../types/astro.js';
Expand All @@ -23,7 +24,6 @@ import {
loaderReturnSchema,
safeStringify,
} from './utils.js';
import type { z } from 'zod';
import { type WrappedWatcher, createWatcherWrapper } from './watcher.js';

export interface ContentLayerOptions {
Expand Down Expand Up @@ -353,17 +353,15 @@ export async function simpleLoader<TData extends { id: string }>(

// Due to this being a union, zod will always throw an "Expected array, received object" error along with the other errors.
// This error is in the second position if the data is an array, and in the first position if the data is an object.
const parseIssue = Array.isArray(unsafeData)
? issue.unionErrors[0]
: issue.unionErrors[1];
const parseIssue = Array.isArray(unsafeData) ? issue.unionErrors[0] : issue.unionErrors[1];

const error = parseIssue.errors[0];
const firstPathItem = error.path[0];

const entry = Array.isArray(unsafeData)
? unsafeData[firstPathItem as number]
const entry = Array.isArray(unsafeData)
? unsafeData[firstPathItem as number]
: unsafeData[firstPathItem as string];

throw new AstroError({
...AstroErrorData.ContentLoaderReturnsInvalidId,
message: AstroErrorData.ContentLoaderReturnsInvalidId.message(context.collection, entry),
Expand Down
28 changes: 16 additions & 12 deletions packages/astro/src/content/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,26 @@ export type ContentLookupMap = {

const entryTypeSchema = z
.object({
id: z
.string({
invalid_type_error: 'Content entry `id` must be a string',
// Default to empty string so we can validate properly in the loader
}),
}).passthrough();
id: z.string({
invalid_type_error: 'Content entry `id` must be a string',
// Default to empty string so we can validate properly in the loader
}),
})
.passthrough();

export const loaderReturnSchema = z.union([
z.array(entryTypeSchema),
z.record(
z.string(),
z.object({
id: z.string({
invalid_type_error: 'Content entry `id` must be a string'
}).optional()
}).passthrough()
z.string(),
z
.object({
id: z
.string({
invalid_type_error: 'Content entry `id` must be a string',
})
.optional(),
})
.passthrough(),
),
]);

Expand Down
2 changes: 1 addition & 1 deletion packages/astro/test/content-collections.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ describe('Content Collections', () => {
} catch (e) {
error = e.message;
}
assert.match(error, /returned an entry with an invalid `id`/);
assert.match(error, /returned an entry with an invalid `id`/);
});
});

Expand Down

0 comments on commit a90f79f

Please sign in to comment.