-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/ntuscse/website into merch-…
…toggle/ivan
- Loading branch information
Showing
11 changed files
with
148 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,24 @@ | ||
# more info: https://docs.github.com/en/repositories/managing-your-repositorys-settings-and-features/customizing-your-repository/about-code-owners | ||
|
||
* @realdyllon | ||
* @chanbakjsd | ||
|
||
/.github/** @realdyllon @aryans1204 | ||
/deployment/** @realdyllon @aryans1204 | ||
/.github/** @chanbakjsd @aryans1204 | ||
/deployment/** @chanbakjsd @aryans1204 | ||
|
||
/apps/cms/** @jamiegoh | ||
/apps/cms/** @jack-thant | ||
/apps/merch/** @chanbakjsd | ||
/apps/web/** @xJQx | ||
/apps/web/** @jack-thant | ||
/apps/challenges/** @BoonHianLim | ||
/apps/web/features/merch/** @chanbakjsd | ||
/apps/web/pages/merch/** @chanbakjsd | ||
|
||
/packages/eslint-custom-config/** @realdyllon | ||
/packages/nodelogger/** @realdyllon | ||
/packages/schemas/** @realdyllon | ||
/packages/schemas/lib/cms.graphql @jamiegoh | ||
/packages/tsconfig/** @realdyllon | ||
/packages/types/** @realdyllon | ||
/packages/types/lib/cms.ts @jamiegoh | ||
/packages/eslint-custom-config/** @chanbakjsd | ||
/packages/nodelogger/** @chanbakjsd | ||
/packages/schemas/** @chanbakjsd | ||
/packages/schemas/lib/cms.graphql @jack-thant | ||
/packages/tsconfig/** @chanbakjsd | ||
/packages/types/** @chanbakjsd | ||
/packages/types/lib/cms.ts @jack-thant | ||
/packages/types/lib/merch.ts @chanbakjsd | ||
/packages/ui/** @xJQx | ||
/packages/ui/merch/** @chanbakjsd | ||
/packages/ui/** @chanbakjsd | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { Field } from "payload/types"; | ||
import { z } from "zod"; | ||
|
||
/** | ||
* Converts Zod field with given type to a corresponding Payload field. | ||
* @param name Name of the Zod field to convert. | ||
* @param zodType Data type of the field to convert. | ||
* @returns Payload Field definition suitable for use in Payload collections. | ||
*/ | ||
function toPayloadZodField( | ||
name: string, | ||
zodType: z.ZodFirstPartySchemaTypes | ||
): Field { | ||
const required = zodType.isOptional() ? {} : { required: true }; | ||
const field = { | ||
name: name, | ||
...required, | ||
}; | ||
|
||
// zod types are matched by type name as matching with instanceof breaks bundler | ||
switch (zodType._def.typeName) { | ||
case z.ZodFirstPartyTypeKind.ZodString: | ||
return { ...field, type: "text" }; | ||
case z.ZodFirstPartyTypeKind.ZodNumber: | ||
return { ...field, type: "number" }; | ||
break; | ||
case z.ZodFirstPartyTypeKind.ZodArray: | ||
return { | ||
...field, | ||
type: "array", | ||
// convert nested type stored in array | ||
fields: toPayloadZod( | ||
(zodType as z.ZodArray<z.ZodObject<z.ZodRawShape>>).element | ||
), | ||
}; | ||
case z.ZodFirstPartyTypeKind.ZodNativeEnum: | ||
return { | ||
...field, | ||
type: "select", | ||
// unpack enum entries as select options | ||
// typescript encodes enums are encoded as bidirectional dictionary | ||
// with both entries from option -> value and value -> option | ||
// use zod parsing to select only the options -> value entries | ||
options: Object.entries((zodType as z.ZodNativeEnum<z.EnumLike>).enum) | ||
.filter(([_, right]) => zodType.safeParse(right).success) | ||
.map(([option, value]) => { | ||
return { label: option, value: `${value}` }; | ||
}), | ||
}; | ||
|
||
case z.ZodFirstPartyTypeKind.ZodOptional: | ||
return { | ||
...toPayloadZodField( | ||
name, | ||
(zodType as z.ZodOptional<z.ZodTypeAny>).unwrap() | ||
), | ||
// override nested field required true with false | ||
...field, | ||
}; | ||
|
||
default: | ||
throw new Error( | ||
`Unable to convert unsupported Zod type: ${JSON.stringify( | ||
zodType, | ||
null, | ||
2 | ||
)}` | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* Converts Zod Object into Payload field definitions. | ||
* @param zodObject Zod Object to convert. | ||
* @returns List of Payload fields corresponding to the fields of the Zod object. | ||
*/ | ||
export function toPayloadZod(zodObject: z.ZodObject<z.ZodRawShape>): Field[] { | ||
return Object.entries(zodObject.shape).map(([name, zodType]) => | ||
toPayloadZodField(name, zodType) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.