Skip to content

Commit 67842ea

Browse files
Add warning for zod4 importing + expand mastodon posts pattern matching
1 parent 5c3a285 commit 67842ea

5 files changed

Lines changed: 21 additions & 6 deletions

File tree

.changeset/mastodon-status-urls.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@fujocoded/zod-transform-socials": patch
3+
---
4+
5+
- mastodon match now accepts post urls in addition to profile mathes (e.g. urls like `/@user/112050937942786185`).
6+
- warn at import time when the default (zod 3) entry is used but zod 4 is installed (for example an astro 6 upgrade that forgot to switch to `/zod4`).

zod-transform-socials/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ For example, putting a custom icon on a personal homepage that would
8080
otherwise resolve to `"custom"` with no icon:
8181
8282
```yaml
83+
name: ms boba
8384
contacts:
8485
- url: https://essentialrandomness.com
8586
icon: simple-icons:firefox
@@ -91,14 +92,14 @@ Drop `SocialLinks` into a content collection (or any other Zod object):
9192
9293
```ts
9394
import { SocialLinks } from "@fujocoded/zod-transform-socials";
94-
// zod import,
95+
// zod import depends on version
9596
9697
export const teamCollection = defineCollection({
9798
type: "data",
9899
schema: (tools) =>
99100
z.object({
100101
name: z.name(),
101-
// contacts is
102+
// contacts will contain your list of social URLs
102103
contacts: SocialLinks,
103104
});
104105
});

zod-transform-socials/src/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ import {
1212
} from "./transform.ts";
1313
import * as z from "zod";
1414

15+
// Zod 4 adds a top-level `toJSONSchema`; Zod 3 doesn't. If we see it, the
16+
// consumer (likely Astro 6) is on Zod 4 and should be using the /zod4 entry.
17+
if (typeof (z as { toJSONSchema?: unknown }).toJSONSchema === "function") {
18+
console.warn(
19+
"[@fujocoded/zod-transform-socials] Zod 4 is installed but you imported the Zod 3 entry. Use `@fujocoded/zod-transform-socials/zod4` instead.",
20+
);
21+
}
22+
1523
const createSocialsSchema = (urlSchema: z.ZodString): z.ZodType<SocialsInput> =>
1624
z.union([
1725
urlSchema,

zod-transform-socials/src/social-links.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export type { ProfileMatch };
88
*/
99
const DOMAIN_PATTERNS = {
1010
mastodon: (domain: string) => ({
11-
match: `https?://${escapeForRegex(domain)}/@([a-z0-9-_]+)/?`,
11+
match: `https?://${escapeForRegex(domain)}/@([a-z0-9-_]+)(?:/.*)?`,
1212
group: 1,
1313
}),
1414
} as const satisfies Record<string, (domain: string) => ProfileMatch>;

zod-transform-socials/src/zod4.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
} from "./transform.ts";
1616
import * as z from "zod/v4";
1717

18-
const createSocialsSchema = (urlSchema: z.ZodString) =>
18+
const createSocialsSchema = (urlSchema: z.ZodType<string>) =>
1919
z.union([
2020
urlSchema,
2121
z.object({
@@ -35,15 +35,15 @@ const createSocialLinksSchema = (
3535
.transform((socialUrls) => socialUrls.map(transformSocial))
3636
.default([]);
3737

38-
export const urlSchema = z.string().url();
38+
export const urlSchema = z.url();
3939
export const SocialsSchema = createSocialsSchema(urlSchema);
4040
export const SocialLinks = createSocialLinksSchema(
4141
SocialsSchema,
4242
transformSocial,
4343
);
4444

4545
export const createSocialsTransformer = (config: CreateSocialsConfig = {}) => {
46-
const SocialsSchema = createSocialsSchema(z.string().url());
46+
const SocialsSchema = createSocialsSchema(z.url());
4747
const { transformSocial, socialLinks } = createTransformSocial(config);
4848
const SocialLinks = createSocialLinksSchema(SocialsSchema, transformSocial);
4949

0 commit comments

Comments
 (0)