The validation process is built around functional options and passing values by specific typed arguments. A common way
to use validation is to call the validator.Validate method and pass the argument option with the list of validation
constraints.
err := validator.Validate(context.Background(), validation.String("", it.IsNotBlank()))
fmt.Println(err)
// Output:
// violation: This value should not be blank.List of common validation arguments:
validation.Nil()- passes result of comparison to nil to test against nil constraints;validation.Bool()- passes boolean value;validation.NilBool()- passes boolean pointer value;validation.Number[T]()- passes generic numeric value;validation.NilNumber[T]()- passes generic numeric pointer value;validation.String()- passes string value;validation.NilString()- passes string pointer value;validation.Countable()- passes result oflen()to test against constraints based on count of the elements;validation.Time()- passestime.Timevalue;validation.NilTime()- passestime.Timepointer value;validation.EachNumber[T]()- passes slice of generic numbers to test each of the element against numeric constraints;validation.EachString()- passes slice of strings to test each of the element against string constraints;validation.Valid()- passesValidatablevalue to run embedded validation;validation.ValidSlice[T]()- passes slice of[]Validatablevalue to run embedded validation on each of the elements;validation.ValidMap[T]()- passesmap[string]Validatablevalue to run embedded validation on each of the elements;validation.Comparable[T]()- passes generic comparable value to test against comparable constraints;validation.NilComparable[T]()- passes generic comparable pointer value to test against comparable constraints;validation.Comparables[T]()- passes generic slice of comparable values (can be used to check for uniqueness of the elements);validation.Slice[T]()- passes generic slice to test against [SliceConstraint] list (e.g. uniqueness by key viait.HasUniqueValuesBy());validation.SliceProperty[T]()- same as [Slice] with property name in violation path;validation.Check()- passes result of any boolean expression;validation.CheckNoViolations()- passeserrorto check err for violations, can be used for embedded validation.
For single value validation, you can use shorthand versions of the validation method:
validator.ValidateBool()- shorthand forvalidator.Bool();validator.ValidateInt()- shorthand forvalidation.Number[int]();validator.ValidateFloat()- shorthand forvalidation.Number[float64]();validator.ValidateString()- shorthand forvalidation.String();validator.ValidateStrings()- shorthand forvalidation.Comparables[[]string]();validator.ValidateCountable()- shorthand forvalidation.Countable();validator.ValidateTime()- shorthand forvalidation.Time();validator.ValidateEachString()- shorthand forvalidation.EachString();validator.ValidateIt()- shorthand forvalidation.Valid().
See usage examples in the documentation.
There are two ways to use the validator service. You can build your instance of validator service by
using validation.NewValidator() or use singleton service from package github.com/muonsoft/validation/validator.
Example of creating a new instance of the validator service:
// import "github.com/muonsoft/validation"
validator, err := validation.NewValidator(
validation.DefaultLanguage(language.Russian), // passing default language of translations
validation.Translations(russian.Messages), // setting up custom or built-in translations
validation.SetViolationFactory(userViolationFactory), // if you want to override creation of violations
)
// don't forget to check for errors
if err != nil {
fmt.Println(err)
}If you want to use a singleton service make sure to set up your configuration once during the initialization of your application.
// import "github.com/muonsoft/validation/validator"
err := validator.SetUp(
validation.DefaultLanguage(language.Russian), // passing default language of translations
validation.Translations(russian.Messages), // setting up custom or built-in translations
validation.SetViolationFactory(userViolationFactory), // if you want to override creation of violations
)
// don't forget to check for errors
if err != nil {
fmt.Println(err)
}