Linting, README update, drone CI recipe update.

This commit is contained in:
Stanislav Nikitin 2019-12-03 21:28:26 +05:00
parent 169b9499fe
commit 50fa32a8ab
No known key found for this signature in database
GPG Key ID: 106900B32F8192EE
5 changed files with 17 additions and 26 deletions

View File

@ -33,8 +33,6 @@ steps:
- name: test-race
image: golang:1.13.4-alpine
environment:
CGO_ENABLED: 0
commands:
- go test -race -test.v ./...
depends_on:
@ -51,8 +49,6 @@ steps:
- name: benchmark-race
image: golang:1.13.4-alpine
environment:
CGO_ENABLED: 0
commands:
- go test -benchmem -run=^$ go.dev.pztrn.name/valiwork -race -bench .
depends_on:

View File

@ -1,5 +1,7 @@
# ValiWork - validation framework
[![Build Status](https://ci.dev.pztrn.name/api/badges/pztrn/valiwork/status.svg)](https://ci.dev.pztrn.name/pztrn/valiwork) [![Discord](https://img.shields.io/discord/632359730089689128)](https://discord.gg/CvUnEpM) ![Keybase XLM](https://img.shields.io/keybase/xlm/pztrn)
ValiWork is a validation framework that provides sane API and ability to write own validators that returns arbitrary things. It is goroutine-safe and fast.
## Default validators
@ -12,6 +14,8 @@ valiwork.InitializeDefaultValidators()
Default validators will return ``error``.
*There are no default validators ATM. Feel free to submit PR with them!*
## Validators registering and namespacing
Default validators using "T_N" scheme, where ``T`` is data type (string, int, int64, etc.) and ``N`` is a validator name (which can be a generic string). Please, use same naming scheme. Example good validators names:

View File

@ -20,7 +20,7 @@ func main() {
//stringToValidate := " I am pretty b@d $tring"
valiwork.RegisterValidator(stringValidatorName, stringValidator)
_ = valiwork.RegisterValidator(stringValidatorName, stringValidator)
}
func stringValidator(thing interface{}, optional ...interface{}) []interface{} {

View File

@ -3,7 +3,6 @@ package valiwork
import (
// stdlib
"log"
"sync"
// local
"go.dev.pztrn.name/valiwork/validators"
@ -11,7 +10,6 @@ import (
var (
registeredValidators map[string]validators.ValidatorFunc
rvMutex sync.RWMutex
)
// nolint
@ -29,9 +27,7 @@ func RegisterValidator(validatorName string, validator validators.ValidatorFunc)
log.Println("Trying to register validator: '" + validatorName + "'...")
}
//rvMutex.RLock()
_, found := registeredValidators[validatorName]
//rvMutex.RUnlock()
if found {
if DEBUG {
@ -41,9 +37,7 @@ func RegisterValidator(validatorName string, validator validators.ValidatorFunc)
return ErrValidatorAlreadyRegistered
}
//rvMutex.Lock()
registeredValidators[validatorName] = validator
//rvMutex.Unlock()
return nil
}
@ -55,9 +49,7 @@ func UnregisterValidator(validatorName string) error {
log.Println("Trying to unregister validator '" + validatorName + "'...")
}
//rvMutex.RLock()
_, found := registeredValidators[validatorName]
//rvMutex.RUnlock()
if !found {
if DEBUG {
@ -67,9 +59,7 @@ func UnregisterValidator(validatorName string) error {
return ErrValidatorNotRegistered
}
//rvMutex.Lock()
delete(registeredValidators, validatorName)
//rvMutex.Unlock()
return nil
}
@ -79,9 +69,7 @@ func UnregisterValidator(validatorName string) error {
func Validate(validatorName string, thing interface{}, optional ...interface{}) []interface{} {
var errs []interface{}
//rvMutex.RLock()
validator, found := registeredValidators[validatorName]
//rvMutex.RUnlock()
if !found {
errs = append(errs, ErrValidatorNotRegistered)

View File

@ -42,6 +42,7 @@ func TestRegisterValidator(t *testing.T) {
for _, testCase := range testCases {
err := RegisterValidator(testCase.ValidatorName, testCase.ValidatorFunc)
if !testCase.ShouldFail {
require.Nil(t, err)
} else {
@ -88,7 +89,7 @@ func TestValidate(t *testing.T) {
testString := " I am test string"
RegisterValidator("string_test1", func(thing interface{}, optional ...interface{}) []interface{} {
_ = RegisterValidator("string_test1", func(thing interface{}, optional ...interface{}) []interface{} {
var errs []interface{}
stringToValidate, ok := thing.(string)
@ -98,7 +99,7 @@ func TestValidate(t *testing.T) {
}
if strings.HasPrefix(stringToValidate, " ") {
errs = append(errs, errors.New("string starts with whitespace, invalid!"))
errs = append(errs, errors.New("string starts with whitespace, invalid"))
}
return errs
@ -116,7 +117,7 @@ func BenchmarkValidate(b *testing.B) {
testString := " I am test $tring"
RegisterValidator("string_test1", func(thing interface{}, optional ...interface{}) []interface{} {
_ = RegisterValidator("string_test1", func(thing interface{}, optional ...interface{}) []interface{} {
var errs []interface{}
stringToValidate, ok := thing.(string)
@ -126,11 +127,11 @@ func BenchmarkValidate(b *testing.B) {
}
if strings.HasPrefix(stringToValidate, " ") {
errs = append(errs, errors.New("string starts with whitespace, invalid!"))
errs = append(errs, errors.New("string starts with whitespace, invalid"))
}
if strings.Contains(stringToValidate, "$") {
errs = append(errs, errors.New("string starts with whitespace, invalid!"))
errs = append(errs, errors.New("string starts with whitespace, invalid"))
}
return errs
@ -150,7 +151,7 @@ func BenchmarkValidateAsync(b *testing.B) {
testString := " I am test $tring"
RegisterValidator("string_test1", func(thing interface{}, optional ...interface{}) []interface{} {
_ = RegisterValidator("string_test1", func(thing interface{}, optional ...interface{}) []interface{} {
var errs []interface{}
stringToValidate, ok := thing.(string)
@ -160,11 +161,11 @@ func BenchmarkValidateAsync(b *testing.B) {
}
if strings.HasPrefix(stringToValidate, " ") {
errs = append(errs, errors.New("string starts with whitespace, invalid!"))
errs = append(errs, errors.New("string starts with whitespace, invalid"))
}
if strings.Contains(stringToValidate, "$") {
errs = append(errs, errors.New("string starts with whitespace, invalid!"))
errs = append(errs, errors.New("string starts with whitespace, invalid"))
}
return errs
@ -214,7 +215,7 @@ func TestUnregisterValidator(t *testing.T) {
func TestUnregisterValidatorNotRegisteredValidator(t *testing.T) {
initializeValidatorsStorage()
err := UnregisterValidator("this is definetely not registered thing")
err := UnregisterValidator("this is definitely not registered thing")
require.NotNil(t, err)
}
@ -232,6 +233,7 @@ func BenchmarkUnregisterValidator(b *testing.B) {
}
b.StartTimer()
for i := 0; i < b.N; i++ {
_ = UnregisterValidator("string_test_validator_" + strconv.Itoa(i))
}
@ -253,6 +255,7 @@ func BenchmarkUnregisterValidatorAsync(b *testing.B) {
var w sync.WaitGroup
b.StartTimer()
for i := 0; i < b.N; i++ {
w.Add(1)