Skip to main content
On this pageFunctions

FieldValidation/Rule

Functions

email

functionsource
/** Creates a `Rule` that checks if a string is a valid email format. */
(message: RuleMessage<string>): Rule<string>

endsWith

functionsource
/** Creates a `Rule` that checks if a string ends with a specified suffix. */
(
  suffix: string,
  message?: RuleMessage<string>
): Rule<string>

equals

functionsource
/** Creates a `Rule` that checks if a string exactly matches an expected value. */
(
  expected: string,
  message?: RuleMessage<string>
): Rule<string>

fromSchema

functionsource
/**
 * Creates a `Rule` that passes when the value decodes through `schema`.
 * 
 *  Use it to reuse a Schema you already maintain (a domain codec, a refined or
 *  branded type you decode to on submit) as a field rule, so the rule stays in
 *  sync with the schema instead of duplicating its logic. It does nothing a
 *  custom rule can't, so prefer the dedicated rules for plain checks. Decoding
 *  is synchronous: the schema must decode without running an effect.
 */
<A, I>(
  schema: Codec<A, I>,
  message: RuleMessage<I>
): Rule<I>

includes

functionsource
/** Creates a `Rule` that checks if a string contains a specified substring. */
(
  substring: string,
  message?: RuleMessage<string>
): Rule<string>

maxItems

functionsource
/** Creates a `Rule` that checks an array holds at most `max` items. */
(
  max: number,
  message?: RuleMessage<readonly Array<unknown>>
): Rule<readonly Array<unknown>>

maxLength

functionsource
/** Creates a `Rule` that checks if a string does not exceed a maximum length. */
(
  max: number,
  message?: RuleMessage<string>
): Rule<string>

minItems

functionsource
/** Creates a `Rule` that checks an array holds at least `min` items. */
(
  min: number,
  message?: RuleMessage<readonly Array<unknown>>
): Rule<readonly Array<unknown>>

minLength

functionsource
/** Creates a `Rule` that checks if a string meets a minimum length. */
(
  min: number,
  message?: RuleMessage<string>
): Rule<string>

oneOf

functionsource
/** Creates a `Rule` that checks if a string is one of a specified set of allowed values. */
(
  values: readonly Array<string>,
  message?: RuleMessage<string>
): Rule<string>

pattern

functionsource
/** Creates a `Rule` that checks if a string matches a regular expression. */
(
  regex: RegExp,
  message: RuleMessage<string>
): Rule<string>

resolveMessage

functionsource
/** Resolves a `RuleMessage` to a concrete string, applying it to the value when the message is a function. */
<A>(
  message: RuleMessage<A>,
  value: A
): string

startsWith

functionsource
/** Creates a `Rule` that checks if a string begins with a specified prefix. */
(
  prefix: string,
  message?: RuleMessage<string>
): Rule<string>

url

functionsource
/**
 * Creates a `Rule` that checks if a string is a valid URL format.
 * 
 *  By default the URL must include an `http://` or `https://` protocol.
 *  Pass `{ requireProtocol: false }` to accept bare domains.
 */
(options: Readonly<{
  message: RuleMessage<string>
  requireProtocol: boolean
}>): Rule<string>

Types

Rule

typesource
/** A tuple of a predicate and error message used for field validation. */
type Rule = readonly [Predicate.Predicate<A>, RuleMessage<A>]

RuleMessage

typesource
/** An error message for a rule: either a static string, or a function that receives the invalid value. */
type RuleMessage = string | (value: A) => string

Stay in the update loop.

New releases, patterns, and the occasional deep dive.


Built with Foldkit.

© 2026 Devin Jameson