astro/packages/integrations/sitemap/src/schema.ts
Oleksii Tymoshenko 1031c06f9c
feat: improved sitemap (#3579)
* feat: extended sitemap functionality

* docs: del samples

* docs: readme

* feat: new sitemap

* feat: createLinkInHead removed

* docs: updated changeset text

* refactor: 'zod' function() instead of self made refine()

* Revert "refactor: 'zod' function() instead of self made refine()"

This reverts commit 036bac730d.

undo function()
2022-06-16 19:06:48 +00:00

47 lines
1.2 KiB
TypeScript

import { z } from 'zod';
import { changefreqValues } from './constants';
import { SITEMAP_CONFIG_DEFAULTS } from './config-defaults';
const localeKeySchema = () => z.string().min(1);
const isFunction = (fn: any) => fn instanceof Function;
const fnSchema = () =>
z
.any()
.refine((val) => !val || isFunction(val), { message: 'Not a function' })
.optional();
export const SitemapOptionsSchema = z
.object({
filter: fnSchema(),
customPages: z.string().url().array().optional(),
canonicalURL: z.string().url().optional(),
i18n: z
.object({
defaultLocale: localeKeySchema(),
locales: z.record(
localeKeySchema(),
z
.string()
.min(2)
.regex(/^[a-zA-Z\-]+$/gm, {
message: 'Only English alphabet symbols and hyphen allowed',
})
),
})
.refine((val) => !val || val.locales[val.defaultLocale], {
message: '`defaultLocale` must exists in `locales` keys',
})
.optional(),
entryLimit: z.number().nonnegative().default(SITEMAP_CONFIG_DEFAULTS.entryLimit),
serialize: fnSchema(),
changefreq: z.enum(changefreqValues).optional(),
lastmod: z.date().optional(),
priority: z.number().min(0).max(1).optional(),
})
.strict()
.default(SITEMAP_CONFIG_DEFAULTS);