On this pageFunctions
FieldValidation/Rule
/** Creates a `Rule` that checks if a string is a valid email format. */
(message: RuleMessage<string>): Rule<string>/** Creates a `Rule` that checks if a string ends with a specified suffix. */
(
suffix: string,
message?: RuleMessage<string>
): Rule<string>/** Creates a `Rule` that checks if a string exactly matches an expected value. */
(
expected: string,
message?: RuleMessage<string>
): Rule<string>/**
* 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>/** Creates a `Rule` that checks if a string contains a specified substring. */
(
substring: string,
message?: RuleMessage<string>
): Rule<string>/** Creates a `Rule` that checks an array holds at most `max` items. */
(
max: number,
message?: RuleMessage<readonly Array<unknown>>
): Rule<readonly Array<unknown>>/** Creates a `Rule` that checks if a string does not exceed a maximum length. */
(
max: number,
message?: RuleMessage<string>
): Rule<string>/** Creates a `Rule` that checks an array holds at least `min` items. */
(
min: number,
message?: RuleMessage<readonly Array<unknown>>
): Rule<readonly Array<unknown>>/** Creates a `Rule` that checks if a string meets a minimum length. */
(
min: number,
message?: RuleMessage<string>
): Rule<string>/** 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>/** Creates a `Rule` that checks if a string matches a regular expression. */
(
regex: RegExp,
message: RuleMessage<string>
): Rule<string>/** 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/** Creates a `Rule` that checks if a string begins with a specified prefix. */
(
prefix: string,
message?: RuleMessage<string>
): Rule<string>/**
* 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>/** A tuple of a predicate and error message used for field validation. */
type Rule = readonly [Predicate.Predicate<A>, RuleMessage<A>]