Today’s random F# code: Regex Active Pattern in API Validation

Active patterns make using a Regex in F# neat: The matched groups are returned in the list after the pattern. Here is an example from our codebase:

Pattern found at Active Patterns – F# | Microsoft Learn:

In this example, I use the regex active pattern to map JSON parsing errors to a Result with typed error results. They are later mapped to HTTP status codes and error messages.

This also works for multiple catch groups:

About the author

Urs Enzler

1 comment

By Urs Enzler

Recent Posts