2023-07-20 08:42:24 +02:00

24 lines
679 B
TypeScript

import { TextField } from "payload/types";
import { isUndefined } from "../../utils/asserts";
type Props = Omit<TextField, "type">;
const validateSlug = (value?: string) => {
if (isUndefined(value) || value === "") return "This field is required.";
if (!/^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(value)) return "This is not a valid slug.";
return true;
};
export const slugField = ({ admin, ...otherProps }: Props): TextField => ({
...otherProps,
type: "text",
required: true,
unique: true,
validate: validateSlug,
admin: {
description:
"A slug must only include lowercase letters and digits. Instead of spaces you can use dashes.",
...admin,
},
});