import * as v from 'valibot'; /** Validate to Mastodon's date format, or use the current date. */ const dateSchema = z.string().datetime({ offset: true }).catch(new Date().toUTCString()); /** Validates individual items in an array, dropping any that aren't valid. */ const filteredArray = (schema: T) => z.any().array().catch([]) .transform((arr) => ( arr.map((item) => { const parsed = schema.safeParse(item); return parsed.success ? parsed.data : undefined; }).filter((item): item is v.InferOutput => Boolean(item)) )); /** Validates the string as an emoji. */ const emojiSchema = v.pipe(v.string(), v.emoji()); /** MIME schema, eg `image/png`. */ const mimeSchema = v.pipe(v.string(), v.regex(/^\w+\/[-+.\w]+$/)); /** valibot schema to force the value into an object, if it isn't already. */ const coerceObject = (shape: T) => v.pipe( v.any(), v.transform((input) => typeof input === 'object' ? input : {}), v.object(shape), ); export { filteredArray, emojiSchema, dateSchema, mimeSchema, coerceObject };