@@ -2,31 +2,34 @@ import type { StaticImageData } from 'next/image';
22import { z } from 'zod' ;
33import saihajPhoto from './saihaj.webp' ;
44
5- export type Author =
6- | {
7- name : string ;
8- link : `https://${string } `;
9- twitter ?: string ;
10- github ?: string ;
11- avatar : string | StaticImageData ;
12- }
13- | {
14- name : string ;
15- link : `https://${string } `;
16- twitter ?: string ;
17- github : string ;
18- // if the author has no avatar, we'll take it from GitHub
19- avatar ?: string | StaticImageData ;
20- } ;
21-
22- export const Author = z . object ( {
5+ const commonAuthorFields = z . object ( {
236 name : z . string ( ) ,
247 link : z . string ( ) . url ( ) ,
258 twitter : z . string ( ) . optional ( ) ,
26- github : z . string ( ) . optional ( ) ,
27- avatar : z . union ( [ z . string ( ) , z . object ( { } ) ] ) . optional ( ) ,
289} ) ;
2910
11+ export const staticImageDataSchema = z . object ( {
12+ src : z . string ( ) ,
13+ } ) as unknown as z . ZodType < StaticImageData > ;
14+
15+ export const Author = z . intersection (
16+ commonAuthorFields ,
17+ z . union ( [
18+ z . object ( {
19+ // if we have a GitHub handle, we don't require the avatar
20+ avatar : z . union ( [ z . string ( ) , staticImageDataSchema ] ) ,
21+ github : z . string ( ) . optional ( ) ,
22+ } ) ,
23+ z . object ( {
24+ github : z . string ( ) ,
25+ // if the author has no avatar, we'll take it from GitHub
26+ avatar : z . union ( [ z . string ( ) , staticImageDataSchema ] ) . optional ( ) ,
27+ } ) ,
28+ ] ) ,
29+ ) ;
30+
31+ export type Author = z . infer < typeof Author > ;
32+
3033export const authors = {
3134 kamil : {
3235 name : 'Kamil Kisiela' ,
@@ -266,3 +269,6 @@ export type AuthorId = keyof typeof authors;
266269export const AuthorId = z . string ( ) . refine ( ( val ) : val is AuthorId => val in authors , {
267270 message : `AuthorId must be one of: ${ Object . keys ( authors ) . join ( ', ' ) } ` ,
268271} ) ;
272+
273+ export const AuthorOrId = z . union ( [ AuthorId , Author ] ) ;
274+ export type AuthorOrId = z . infer < typeof AuthorOrId > ;
0 commit comments