npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

rhf-field-errors-extractor

v1.0.0

Published

Extract a single representative error message from React Hook Form FieldErrors.

Readme

rhf-field-errors-extractor

Extract a single representative error message from React Hook Form FieldErrors.

How To Use

Extract Error Data

const formMethod = useForm();

const handleSubmit = formMethod.handleSubmit(
  (data) => {
    // ...
  },
  (fieldErrors) => {
    const extractor = new FieldErrorExtractor(fieldErrors);
    const errorData = extractor.extract();

    window.alert(errorData.message);
    errorData.element?.focus();
  },
);
  • message: Field error messages registered with React Hook Form
  • element: It can be used if the ref of the field is assigned to the component.

Setting Error Priority

const extractor = new FieldErrorExtractor(fieldErrors);
const errorData = extractor.extract([new MessageExistExtractOrder({ trim: true }), new DomPlaceExtractOrder()]);

window.alert(errorData.message);
errorData.element?.focus();

When you pass the FieldErrorDataOrder array as a parameter to the extract method, it finds the most appropriate error data from FieldErrors. The FieldErrorDataOrder is applied in order, and in the example above, it extracts the error data from the FieldError that has a message and appears first in DOM order.

MessageExistExtractOrder

MessageExistExtractOrder({ trim: boolean })

FieldError that contain a message are prioritized. When comparing undefined and an empty string, the empty string takes precedence. The trim option determines whether to trim the message of a FieldError before comparison.

DomPlaceExtractOrder

DomPlaceExtractOrder()

FieldError that appear earlier in the DOM are given higher priority. FieldError without an assigned ref have the lowest priority.

MatchedNameExtractOrder

MatchedNameExtractOrder<
  TFieldValues extends FieldValues,
  TFieldName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,
>(nameList: Array<TFieldName>, { exact: boolean })

Priority is determined by matching the name of the field where the error occurred. The order of the nameList array defines the priority. If the exact option is enabled, the field name must match exactly to be considered for priority.

Custom Extract Order

class CustomExtractOrder implements FieldErrorDataOrder {
  public compare(data1: FieldErrorData, data2: FieldErrorData): CompareFieldErrorDataResult {
    if (this.isFirstDataSelected()) {
      return CompareFieldErrorDataResult.First;
    }

    if (this.isSecondDataSelected()) {
      return CompareFieldErrorDataResult.Second;
    }

    return CompareFieldErrorDataResult.Equal;
  }
}

const extractor = new FieldErrorExtractor(fieldErrors);
const errorData = extractor.extract([new CustomExtractOrder()]);

You can customize the priority logic by implementing the compare method in a class that extends the FieldErrorDataOrder interface, and passing it to the extract method of FieldErrorExtractor.

Background Knowledge

About React Hook Form FieldErrors

type FieldErrors<T extends FieldValues = FieldValues> = Partial<
  FieldValues extends IsAny<FieldValues> ? any : FieldErrorsImpl<DeepRequired<T>>
> & {
  root?: Record<string, GlobalError> & GlobalError;
};

type FieldErrorsImpl<T extends FieldValues = FieldValues> = {
  [K in keyof T]?: T[K] extends BrowserNativeObject | Blob
    ? FieldError
    : K extends 'root' | `root.${string}`
      ? GlobalError
      : T[K] extends object
        ? Merge<FieldError, FieldErrorsImpl<T[K]>>
        : FieldError;
};

FieldErrors is an object that maps the keys of FieldValues where errors have occurred to their corresponding error data. Depending on the situation, various types of error data may exist.

  • When the value is of type BrowserNativeObject or Blob: FieldError
  • When the key is "root" or a string starting with "root.": GlobalError
  • When the value is an object: Merge<FieldError, FieldErrorsImpl<T[K]>>
  • Other cases: FieldError

If we abstract this from a different perspective, FieldErrorsImpl can be viewed as a tree structure where GlobalError and FieldError act as leaf nodes.

type FieldErrorsImpl = {
  ['root' | `root.${string}`]: GlobalError;
  [Key_Of_No_Chil_FiledValue]: FieldError;
  [Key_Of_Has_Child_FieldValue]: Merge<FieldError, FieldErrorsImpl<Has_Child_FieldValue>>;
};

One important point to note is that, except for the root node (FieldErrors), the parent nodes (Merge<FieldError, FieldErrorsImpl<T[K]>>) inherently possess the properties of leaf nodes (FieldError) due to the sepc of Merge.

type Merge<A, B> = {
  [K in keyof A | keyof B]?: K extends keyof A & keyof B
    ? [A[K], B[K]] extends [object, object]
      ? Merge<A[K], B[K]>
      : A[K] | B[K]
    : K extends keyof A
      ? A[K]
      : K extends keyof B
        ? B[K]
        : never;
};