nobl9-go icon indicating copy to clipboard operation
nobl9-go copied to clipboard

feat: Switch to govy for validation

Open nieomylnieja opened this issue 6 months ago • 0 comments

Motivation

Code previously located at internal/validation is now being open-sourced. The new library govy comes with some initial changes to the structure of the code and the API.

Summary

Most of the changes were done via the following script, a few manual steps were required as well. I've also removed unnecessary type annotations while at it.

#! /usr/bin/env bash

old_import="github.com\/nobl9\/nobl9-go\/internal\/validation"
new_import="github.com\/nobl9\/govy\/pkg\/govy"

# List of functions to transfer from govy to rules package generated by:
# rg -N -I 'func.*govy\.Rule' -g '!*_test.go' | \
#   awk '/^func [A-Z]/ {sub(/[\[\(].*/, "", $2); print $2}'

while read -r file; do
  sed -r -i '
  s/'"$old_import"'/'"$new_import"'/g;
  s/validation\./govy\./g;
  s/NewSingleRule/NewRule/g;
  s/SingleRule/Rule/g;
  s/govy\.StringLength/rules\.StringLength/g;
  s/govy\.StringMinLength/rules\.StringMinLength/g;
  s/govy\.StringMaxLength/rules\.StringMaxLength/g;
  s/govy\.SliceLength/rules\.SliceLength/g;
  s/govy\.SliceMinLength/rules\.SliceMinLength/g;
  s/govy\.SliceMaxLength/rules\.SliceMaxLength/g;
  s/govy\.MapLength/rules\.MapLength/g;
  s/govy\.MapMinLength/rules\.MapMinLength/g;
  s/govy\.MapMaxLength/rules\.MapMaxLength/g;
  s/govy\.EqualTo/rules\.EQ/g;
  s/govy\.NotEqualTo/rules\.NEQ/g;
  s/govy\.GreaterThan([\(\[])/rules\.GT\1/g;
  s/govy\.GreaterThanOrEqualTo/rules\.GTE/g;
  s/govy\.LessThan([\(\[])/rules\.LT\1/g;
  s/govy\.LessThanOrEqualTo/rules\.LTE/g;
  s/govy\.DurationPrecision/rules\.DurationPrecision/g;
  s/govy\.SliceUnique/rules\.SliceUnique/g;
  s/govy\.OneOf/rules\.OneOf/g;
  s/govy\.MutuallyExclusive/rules\.MutuallyExclusive/g;
  s/govy\.URL/rules\.URL/g;
  s/govy\.Forbidden/rules\.Forbidden/g;
  s/govy\.StringNotEmpty/rules\.StringNotEmpty/g;
  s/govy\.StringMatchRegexp/rules\.StringMatchRegexp/g;
  s/govy\.StringDenyRegexp/rules\.StringDenyRegexp/g;
  s/govy\.StringIsDNSSubdomain/rules\.StringDNSLabel/g;
  s/govy\.StringUUID/rules\.StringUUID/g;
  s/govy\.StringASCII/rules\.StringASCII/g;
  s/govy\.StringURL/rules\.StringURL/g;
  s/govy\.StringJSON/rules\.StringJSON/g;
  s/govy\.StringContains/rules\.StringContains/g;
  s/govy\.StringStartsWith/rules\.StringStartsWith/g;
  s/govy\.StringEndsWith/rules\.StringEndsWith/g;
  s/govy\.StringIsTitle/rules\.StringIsTitle/g;
  s/govy\.Required/rules\.Required/g;
  s/govy\.SelfHashFunc/rules\.HashFuncSelf/g;
  s/govy\.ErrorCode /rules\.ErrorCode /g;
  s/govy\.ErrorCode(\w+)/rules\.ErrorCode\1/g;
  s/rules\.ErrorCodeStringDescription/validationV1Alpha\.ErrorCodeStringDescription/g;
  s/rules\.ErrorCodeSeparator/govy\.ErrorCodeSeparator/g;
  s/rules\.ErrorCodeTransform/govy\.ErrorCodeTransform/g;
  s/govy\.JoinErrors/errorutils\.JoinErrors/g;
  s/govy\.StringDescription/validationV1Alpha\.StringDescription/g;
  s/rules\.ErrorCodeStringIsDNSSubdomain/rules\.ErrorCodeStringDNSLabel/g;
  ' "$file"
done < <(rg -l "$old_import" -g '!internal/validation/**' -g '!go.mod' -g '!go.sum')

# Followed by:
#  go mod tidy
#  make format
#  go vet ./...

Release Notes

nobl9-go is switching to a brand new validation library, govy. The library was previously part of nobl9-go, but is now being open sourced! Go check it out ;)

nieomylnieja avatar Aug 21 '24 20:08 nieomylnieja