Dev Helpers
← Back

Validate a field with a reusable rule

Validate a field with a reusable rule using TypeScript.

Open this example in the interactive library

Source code

type Fields = { name: string; age: number; subscribed: boolean };
const initial: Fields = { name: "Ada", age: 30, subscribed: false };
console.log(
  ((rule: (s: string) => string | null) => rule(initial.name))((s) =>
    s.trim() ? null : "Required",
  ),
);

Sample output

null