43 lines
1.2 KiB
TypeScript
43 lines
1.2 KiB
TypeScript
import { string, type ValueSchema } from "@wrnexus/blocks";
|
|
|
|
// Brand fields are semantic values, never arbitrary CSS declarations.
|
|
export function brandValue(
|
|
defaultValue: string,
|
|
accepts: (value: string) => boolean,
|
|
): ValueSchema<string> {
|
|
const base = string({ default: defaultValue, max: 160 });
|
|
return {
|
|
...base,
|
|
parse(value, path = "value") {
|
|
const result = base.parse(value, path);
|
|
if (!result.ok) return result;
|
|
return accepts(result.value)
|
|
? result
|
|
: {
|
|
ok: false,
|
|
errors: [
|
|
{
|
|
code: "email.brand",
|
|
message: "Unsupported brand style value.",
|
|
severity: "error",
|
|
path,
|
|
},
|
|
],
|
|
};
|
|
},
|
|
};
|
|
}
|
|
|
|
export const brandColor = (fallback: string) =>
|
|
brandValue(fallback, (value) =>
|
|
/^(#[0-9a-f]{3}|#[0-9a-f]{6}|#[0-9a-f]{8}|transparent)$/i.test(value),
|
|
);
|
|
export const brandFont = brandValue(
|
|
"Arial, sans-serif",
|
|
(value) => /^[a-z0-9 ,'"-]+$/i.test(value) && /[a-z]/i.test(value),
|
|
);
|
|
export const brandWidth = brandValue(
|
|
"600",
|
|
(value) => /^\d{2,4}$/.test(value) && Number(value) >= 240 && Number(value) <= 1200,
|
|
);
|