2026-10-08 18:49:15 -04:00
|
|
|
type TextValidator = ({ value }: { value: string }) => string | undefined;
|
|
|
|
|
|
|
|
|
|
/** A text field validator that requires a value no longer than `max` characters. */
|
|
|
|
|
export const requiredWithMaxLength =
|
2026-11-23 14:16:00 +00:00
|
|
|
(max: number, missing: () => string, tooLong: () => string): TextValidator =>
|
2026-10-08 18:49:15 -04:00
|
|
|
({ value }): string | undefined => {
|
|
|
|
|
if (value.length < 1) {
|
2026-11-23 14:16:00 +00:00
|
|
|
return missing();
|
2026-10-08 18:49:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (value.length > max) {
|
2026-11-23 14:16:00 +00:00
|
|
|
return tooLong();
|
2026-10-08 18:49:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return undefined;
|
|
|
|
|
};
|