Urgent.News

What's breaking now, across thousands of outlets.

Tech

Returning RFC 9457 Problem Details from Go Validation Errors

How to turn struct-tag validation failures into a standard "application/problem+json" response — the RFC 9457 format — with one method call, no hand-rolled error envelope. Most Go APIs invent their own validation error shape. One team returns {"errors": [...]} , another {"field_errors": {...}} , a third just a flat {"error": "message"} and hopes the client parses it. Every one of those is a…

Checker now provides a standard RFC 9457 format response for Go validation errors, simplifying the process of returning structured error information in a consistent way. The RFC 9457 format includes four base members: type, title, status, and detail (instance), with the option for problem-specific extensions. Checker can generate these responses directly from failed struct validation using the CheckErrors.ProblemDetails() method.

This results in a single method call that generates an application/problem+json body with the necessary structure. The output includes a type, title, status, and an invalid-params array listing the fields that failed validation along with their reasons and codes. By using Checker's CheckStruct() method, developers can easily convert validation errors into a properly formatted problem+json response without having to manually construct the envelope.

The defaults for the problem details are set to RFC 9457's own defaults, but these can be overridden if needed. Localized error messages are also supported, with the ability to specify a different locale if desired. Adopting this standard format for error handling in Go APIs can improve interoperability, simplify error handling for clients and API gateways, and reduce the need for custom error shapes across different services.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

99.7% Rejected in 84ms: Why I Stopped Making the Generator Smarter

I wrote a puzzle generator whose acceptance rate is 0.26% . It throws away 99.7% of everything it produces, and that is the design working as intended, not failing.

  • 99.7% rejection rate means only 0.26% of puzzles accepted
  • Generator produces plausible letter combinations, verifier checks solutions
  • Verifier's exhaustive search and unique solution guarantee maintain rejection rates

Catch Bad Validation Tags at Compile Time with checkerlint

Struct tags are just strings — a typo'd checker name, a wrong-typed field, or a renamed cross-field target all compile fine and fail silently at runtime. checkerlint catches all three before you ship.

  • Checkerlint tool checks struct tags at build/lint time
  • Detects unknown checker names, type incompatibility, cross-field targets
  • Prevents runtime errors caused by invalid struct tags

More from Sunday 6 September →