/beaut

Primary LanguageGoApache License 2.0Apache-2.0

beaut: Convenient handling for validated types

Go Reference Sourcegraph

In certain situations, you've already done some validation on a type. For example, you may have checked that a path is an absolute path, or that a string is valid UTF-8. However, since there is no standard library support for representing such types, maybe you continue passing that value around as a string or a []byte. This has a few downsides:

  • Reduced dev velocity: It is not clear to the reader which operations are safe to perform, and editing the code becomes harder as it is unclear which invariants must be upheld.
  • Poor debugging experience: Failures may appear much further downstream in case validation was not done on all code paths.
  • Performance cost: Out of defensiveness, different parts of the code may perform the same validation redundantly.

Following the Parse, Don't Validate mantra, it is useful to have a dedicated type to represent the validation, so that it is clearer to the reader which operations are safe to perform and which operations require extra care.

This library exposes a few types such as UTF8String, UTF8Bytes, AbsolutePath and RelativePath to represent common validations.