1663623480
In today's post we will learn about 10 Popular Golang Libraries for Validation.
What is Validation?
Validation is a simple concept to understand but difficult to put into practice.
Validation is the recognition and acceptance of another persons internal experience as being valid. Emotional validation is distinguished from emotional invalidation, in which your own or another persons emotional experiences are rejected, ignored, or judged. Self-validation is the recognition and acknowledgement of your own internal experience.
Validation does not mean agreeing with or supporting feelings or thoughts. Validating does not mean love. You can validate someone you don’t like even though you probably wouldn’t want to.
Table of contents:
Go Struct and Field validation, including Cross Field, Cross Struct, Map, Slice and Array diving.
Package validator implements value validations for structs and individual fields based on tags.
It has the following unique features:
Use go get.
go get github.com/go-playground/validator/v10
Then import the validator package into your own code.
import "github.com/go-playground/validator/v10"
Validation functions return type error
They return type error to avoid the issue discussed in the following, where err is always != nil:
Validator returns only InvalidValidationError for bad validation input, nil or ValidationErrors as type error; so, in your code all you need to do is check if the error returned is not nil, and if it's not check if error is InvalidValidationError ( if necessary, most of the time it isn't ) type cast it to type ValidationErrors like so:
err := validate.Struct(mystruct)
validationErrors := err.(validator.ValidationErrors)
Please see https://pkg.go.dev/github.com/go-playground/validator/v10 for detailed usage docs.
Run on MacBook Pro (15-inch, 2017) go version go1.10.2 darwin/amd64
goos: darwin
goarch: amd64
pkg: github.com/go-playground/validator
BenchmarkFieldSuccess-8 20000000 83.6 ns/op 0 B/op 0 allocs/op
BenchmarkFieldSuccessParallel-8 50000000 26.8 ns/op 0 B/op 0 allocs/op
BenchmarkFieldFailure-8 5000000 291 ns/op 208 B/op 4 allocs/op
BenchmarkFieldFailureParallel-8 20000000 107 ns/op 208 B/op 4 allocs/op
BenchmarkFieldArrayDiveSuccess-8 2000000 623 ns/op 201 B/op 11 allocs/op
BenchmarkFieldArrayDiveSuccessParallel-8 10000000 237 ns/op 201 B/op 11 allocs/op
BenchmarkFieldArrayDiveFailure-8 2000000 859 ns/op 412 B/op 16 allocs/op
BenchmarkFieldArrayDiveFailureParallel-8 5000000 335 ns/op 413 B/op 16 allocs/op
BenchmarkFieldMapDiveSuccess-8 1000000 1292 ns/op 432 B/op 18 allocs/op
BenchmarkFieldMapDiveSuccessParallel-8 3000000 467 ns/op 432 B/op 18 allocs/op
BenchmarkFieldMapDiveFailure-8 1000000 1082 ns/op 512 B/op 16 allocs/op
BenchmarkFieldMapDiveFailureParallel-8 5000000 425 ns/op 512 B/op 16 allocs/op
BenchmarkFieldMapDiveWithKeysSuccess-8 1000000 1539 ns/op 480 B/op 21 allocs/op
BenchmarkFieldMapDiveWithKeysSuccessParallel-8 3000000 613 ns/op 480 B/op 21 allocs/op
BenchmarkFieldMapDiveWithKeysFailure-8 1000000 1413 ns/op 721 B/op 21 allocs/op
BenchmarkFieldMapDiveWithKeysFailureParallel-8 3000000 575 ns/op 721 B/op 21 allocs/op
BenchmarkFieldCustomTypeSuccess-8 10000000 216 ns/op 32 B/op 2 allocs/op
BenchmarkFieldCustomTypeSuccessParallel-8 20000000 82.2 ns/op 32 B/op 2 allocs/op
BenchmarkFieldCustomTypeFailure-8 5000000 274 ns/op 208 B/op 4 allocs/op
BenchmarkFieldCustomTypeFailureParallel-8 20000000 116 ns/op 208 B/op 4 allocs/op
BenchmarkFieldOrTagSuccess-8 2000000 740 ns/op 16 B/op 1 allocs/op
BenchmarkFieldOrTagSuccessParallel-8 3000000 474 ns/op 16 B/op 1 allocs/op
BenchmarkFieldOrTagFailure-8 3000000 471 ns/op 224 B/op 5 allocs/op
BenchmarkFieldOrTagFailureParallel-8 3000000 414 ns/op 224 B/op 5 allocs/op
BenchmarkStructLevelValidationSuccess-8 10000000 213 ns/op 32 B/op 2 allocs/op
BenchmarkStructLevelValidationSuccessParallel-8 20000000 91.8 ns/op 32 B/op 2 allocs/op
BenchmarkStructLevelValidationFailure-8 3000000 473 ns/op 304 B/op 8 allocs/op
BenchmarkStructLevelValidationFailureParallel-8 10000000 234 ns/op 304 B/op 8 allocs/op
BenchmarkStructSimpleCustomTypeSuccess-8 5000000 385 ns/op 32 B/op 2 allocs/op
BenchmarkStructSimpleCustomTypeSuccessParallel-8 10000000 161 ns/op 32 B/op 2 allocs/op
BenchmarkStructSimpleCustomTypeFailure-8 2000000 640 ns/op 424 B/op 9 allocs/op
BenchmarkStructSimpleCustomTypeFailureParallel-8 5000000 318 ns/op 440 B/op 10 allocs/op
BenchmarkStructFilteredSuccess-8 2000000 597 ns/op 288 B/op 9 allocs/op
BenchmarkStructFilteredSuccessParallel-8 10000000 266 ns/op 288 B/op 9 allocs/op
BenchmarkStructFilteredFailure-8 3000000 454 ns/op 256 B/op 7 allocs/op
BenchmarkStructFilteredFailureParallel-8 10000000 214 ns/op 256 B/op 7 allocs/op
BenchmarkStructPartialSuccess-8 3000000 502 ns/op 256 B/op 6 allocs/op
BenchmarkStructPartialSuccessParallel-8 10000000 225 ns/op 256 B/op 6 allocs/op
BenchmarkStructPartialFailure-8 2000000 702 ns/op 480 B/op 11 allocs/op
BenchmarkStructPartialFailureParallel-8 5000000 329 ns/op 480 B/op 11 allocs/op
BenchmarkStructExceptSuccess-8 2000000 793 ns/op 496 B/op 12 allocs/op
BenchmarkStructExceptSuccessParallel-8 10000000 193 ns/op 240 B/op 5 allocs/op
BenchmarkStructExceptFailure-8 2000000 639 ns/op 464 B/op 10 allocs/op
BenchmarkStructExceptFailureParallel-8 5000000 300 ns/op 464 B/op 10 allocs/op
BenchmarkStructSimpleCrossFieldSuccess-8 3000000 417 ns/op 72 B/op 3 allocs/op
BenchmarkStructSimpleCrossFieldSuccessParallel-8 10000000 163 ns/op 72 B/op 3 allocs/op
BenchmarkStructSimpleCrossFieldFailure-8 2000000 645 ns/op 304 B/op 8 allocs/op
BenchmarkStructSimpleCrossFieldFailureParallel-8 5000000 285 ns/op 304 B/op 8 allocs/op
BenchmarkStructSimpleCrossStructCrossFieldSuccess-8 3000000 588 ns/op 80 B/op 4 allocs/op
BenchmarkStructSimpleCrossStructCrossFieldSuccessParallel-8 10000000 221 ns/op 80 B/op 4 allocs/op
BenchmarkStructSimpleCrossStructCrossFieldFailure-8 2000000 868 ns/op 320 B/op 9 allocs/op
BenchmarkStructSimpleCrossStructCrossFieldFailureParallel-8 5000000 337 ns/op 320 B/op 9 allocs/op
BenchmarkStructSimpleSuccess-8 5000000 260 ns/op 0 B/op 0 allocs/op
BenchmarkStructSimpleSuccessParallel-8 20000000 90.6 ns/op 0 B/op 0 allocs/op
BenchmarkStructSimpleFailure-8 2000000 619 ns/op 424 B/op 9 allocs/op
BenchmarkStructSimpleFailureParallel-8 5000000 296 ns/op 424 B/op 9 allocs/op
BenchmarkStructComplexSuccess-8 1000000 1454 ns/op 128 B/op 8 allocs/op
BenchmarkStructComplexSuccessParallel-8 3000000 579 ns/op 128 B/op 8 allocs/op
BenchmarkStructComplexFailure-8 300000 4140 ns/op 3041 B/op 53 allocs/op
BenchmarkStructComplexFailureParallel-8 1000000 2127 ns/op 3041 B/op 53 allocs/op
BenchmarkOneof-8 10000000 140 ns/op 0 B/op 0 allocs/op
BenchmarkOneofParallel-8 20000000 70.1 ns/op 0 B/op 0 allocs/op
🎈 A lightweight struct validator for Go.
go get github.com/guiferpa/gody/v2
package main
import (
"encoding/json"
"fmt"
"net/http"
gody "github.com/guiferpa/gody/v2"
"github.com/guiferpa/gody/v2/rule"
)
type RequestBody struct {
Name string `json:"name" validate:"not_empty"`
Age int `json:"age" validate:"min=21"`
}
func HTTPHandler(v *gody.Validator) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var body RequestBody
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
...
}
defer r.Body.Close()
if isValidated, err := v.Validate(body); err != nil {
...
}
})
}
func main() {
validator := gody.NewValidator()
validator.AddRules(rule.NotEmpty, rule.Min)
port := ":3000"
http.ListenAndServe(port, HTTPHandler(validator))
}
There are others ways to valid a struct, take a look on functions below:
gody.RawValidate(interface{}, string, []gody.Rule) (bool, error)
gody.Validate(interface{}, []gody.Rule) (bool, error)
gody.RawDefaultValidate(interface{}, string, []gody.Rule) (bool, error)
gody.DefaultValidate(interface{}, []gody.Rule) (bool, error)
Fast, tag-based validation for structs.
package main
import (
"fmt"
"log"
"strings"
"github.com/twharmon/govalid"
)
type Post struct {
// ID has no constraints
ID int
// Title is required, must be at least 3 characters long, cannot be
// more than 20 characters long, and must match ^[a-zA-Z ]+$
Title string `govalid:"req|min:3|max:20|regex:^[a-zA-Z ]+$"`
// Body is not required, cannot be more than 10000 charachers,
// and must be "fun" (a custom rule defined below).
Body string `govalid:"max:10000|fun"`
// Category is not required, but if not zero value ("") it must be
// either "announcement" or "bookreview".
Category string `govalid:"in:announcement,bookreview"`
}
var v = govalid.New()
func main() {
// Add Custom validation to the struct `Post`
v.AddCustom(Post{}, func(val interface{}) string {
post := val.(*Post)
if post.Category != "" && !strings.Contains(post.Body, post.Category) {
return fmt.Sprintf("Body must contain %s", post.Category)
}
return ""
})
// Add custom string "fun" that can be used on any string field
// in any struct.
v.AddCustomStringRule("fun", func(field string, value string) string {
if float64(strings.Count(value, "!")) / float64(utf8.RuneCountInString(value)) > 0.001 {
return ""
}
return fmt.Sprintf("%s must contain more exclamation marks", field)
})
p := Post{
ID: 5,
Title: "Hi",
Body: "Hello world!",
Category: "announcement",
}
vio, err := v.Violations(&p)
if err != nil {
log.Fatalln(err)
}
fmt.Println(vio)
}
BenchmarkValidatorStringReqInvalid 267.3 ns/op 48 B/op 3 allocs/op
BenchmarkValidatorStringReqValid 92.35 ns/op 16 B/op 1 allocs/op
BenchmarkValidatorsVariety 1484 ns/op 297 B/op 15 allocs/op
Make a pull request.
Validators and sanitizers for strings, numerics, slices and structs.
Make sure that Go is installed on your computer. Type the following command in your terminal:
go get github.com/asaskevich/govalidator
or you can get specified release of the package with gopkg.in
:
go get gopkg.in/asaskevich/govalidator.v10
After it the package is ready to use.
Add following line in your *.go
file:
import "github.com/asaskevich/govalidator"
If you are unhappy to use long govalidator
, you can do something like this:
import (
valid "github.com/asaskevich/govalidator"
)
SetFieldsRequiredByDefault
causes validation to fail when struct fields do not include validations or are not explicitly marked as exempt (using valid:"-"
or valid:"email,optional"
). A good place to activate this is a package init function or the main() function.
SetNilPtrAllowedByRequired
causes validation to pass when struct fields marked by required
are set to nil. This is disabled by default for consistency, but some packages that need to be able to determine between nil
and zero value
state can use this. If disabled, both nil
and zero
values cause validation errors.
import "github.com/asaskevich/govalidator"
func init() {
govalidator.SetFieldsRequiredByDefault(true)
}
Here's some code to explain it:
// this struct definition will fail govalidator.ValidateStruct() (and the field values do not matter):
type exampleStruct struct {
Name string ``
Email string `valid:"email"`
}
// this, however, will only fail when Email is empty or an invalid email address:
type exampleStruct2 struct {
Name string `valid:"-"`
Email string `valid:"email"`
}
// lastly, this will only fail when Email is an invalid email address but not when it's empty:
type exampleStruct2 struct {
Name string `valid:"-"`
Email string `valid:"email,optional"`
}
Custom validator function signature
A context was added as the second parameter, for structs this is the object being validated – this makes dependent validation possible.
import "github.com/asaskevich/govalidator"
// old signature
func(i interface{}) bool
// new signature
func(i interface{}, o interface{}) bool
Adding a custom validator
This was changed to prevent data races when accessing custom validators.
import "github.com/asaskevich/govalidator"
// before
govalidator.CustomTypeTagMap["customByteArrayValidator"] = func(i interface{}, o interface{}) bool {
// ...
}
// after
govalidator.CustomTypeTagMap.Set("customByteArrayValidator", func(i interface{}, o interface{}) bool {
// ...
})
Validate Golang request data with simple rules. Highly inspired by Laravel's request validation.
Install the package using
$ go get github.com/thedevsaddam/govalidator
// or
$ go get gopkg.in/thedevsaddam/govalidator.v1
To use the package import it in your *.go
code
import "github.com/thedevsaddam/govalidator"
// or
import "gopkg.in/thedevsaddam/govalidator.v1"
Validate form-data
, x-www-form-urlencoded
and query params
package main
import (
"encoding/json"
"fmt"
"net/http"
"github.com/thedevsaddam/govalidator"
)
func handler(w http.ResponseWriter, r *http.Request) {
rules := govalidator.MapData{
"username": []string{"required", "between:3,8"},
"email": []string{"required", "min:4", "max:20", "email"},
"web": []string{"url"},
"phone": []string{"digits:11"},
"agree": []string{"bool"},
"dob": []string{"date"},
}
messages := govalidator.MapData{
"username": []string{"required:আপনাকে অবশ্যই ইউজারনেম দিতে হবে", "between:ইউজারনেম অবশ্যই ৩-৮ অক্ষর হতে হবে"},
"phone": []string{"digits:ফোন নাম্বার অবশ্যই ১১ নম্বারের হতে হবে"},
}
opts := govalidator.Options{
Request: r, // request object
Rules: rules, // rules map
Messages: messages, // custom message map (Optional)
RequiredDefault: true, // all the field to be pass the rules
}
v := govalidator.New(opts)
e := v.Validate()
err := map[string]interface{}{"validationError": e}
w.Header().Set("Content-type", "application/json")
json.NewEncoder(w).Encode(err)
}
func main() {
http.HandleFunc("/", handler)
fmt.Println("Listening on port: 9000")
http.ListenAndServe(":9000", nil)
}
Send request to the server using curl or postman: curl GET "http://localhost:9000?web=&phone=&zip=&dob=&agree="
Jio is a json schema validator similar to joi.
Parameter validation in Golang is really a cursing problem. Defining tags on structs is not easy to extend rules, handwritten validation code makes logic code cumbersome, and the initial zero value of the struct field will also interfere with the validation.
jio tries validate json raw data before deserialization to avoid these problems. Defining validation rules as Schema is easy to read and easy to extend (Inspired by Hapi.js joi library). Rules within Schema can be validated in the order of registration, and context can be used to exchange data between rules, and can access other field data even within a single rule, etc.
jio provides a flexible enough way to make your validation simple and efficient!
package main
import (
"log"
"github.com/faceair/jio"
)
func main() {
data := []byte(`{
"debug": "on",
"window": {
"title": "Sample Widget",
"size": [500, 500]
}
}`)
_, err := jio.ValidateJSON(&data, jio.Object().Keys(jio.K{
"debug": jio.Bool().Truthy("on").Required(),
"window": jio.Object().Keys(jio.K{
"title": jio.String().Min(3).Max(18),
"size": jio.Array().Items(jio.Number().Integer()).Length(2).Required(),
}).Without("name", "title").Required(),
}))
if err != nil {
panic(err)
}
log.Printf("%s", data) // {"debug":true,"window":{"size":[500,500],"title":"Sample Widget"}}
}
The above schema defines the following constraints:
debug
on
string instead of true
window
name
and title
title
size
Take chi as an example, the other frameworks are similar.
package main
import (
"io/ioutil"
"net/http"
"github.com/faceair/jio"
"github.com/go-chi/chi"
)
func main() {
r := chi.NewRouter()
r.Route("/people", func(r chi.Router) {
r.With(jio.ValidateBody(jio.Object().Keys(jio.K{
"name": jio.String().Min(3).Max(10).Required(),
"age": jio.Number().Integer().Min(0).Max(100).Required(),
"phone": jio.String().Regex(`^1[34578]\d{9}$`).Required(),
}), jio.DefaultErrorHandler)).Post("/", func(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
panic(err)
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
w.Write(body)
})
})
http.ListenAndServe(":8080", r)
}
The second parameter of jio.ValidateBody
is called for error handling when the validation fails.
Supports validation of various data types (structs, strings, maps, slices, etc.) with configurable and extensible validation rules specified in usual code constructs instead of struct tags.
ozzo-validation is a Go package that provides configurable and extensible data validation capabilities. It has the following features:
Validatable
interface.sql.Valuer
interface (e.g. sql.NullString
).For an example on how this library is used in an application, please refer to go-rest-api which is a starter kit for building RESTful APIs in Go.
Go 1.13 or above.
The ozzo-validation package mainly includes a set of validation rules and two validation methods. You use validation rules to describe how a value should be considered valid, and you call either validation.Validate()
or validation.ValidateStruct()
to validate the value.
Run the following command to install the package:
go get github.com/go-ozzo/ozzo-validation
For a simple value, such as a string or an integer, you may use validation.Validate()
to validate it. For example,
package main
import (
"fmt"
"github.com/go-ozzo/ozzo-validation/v4"
"github.com/go-ozzo/ozzo-validation/v4/is"
)
func main() {
data := "example"
err := validation.Validate(data,
validation.Required, // not empty
validation.Length(5, 100), // length between 5 and 100
is.URL, // is a valid URL
)
fmt.Println(err)
// Output:
// must be a valid URL
}
The method validation.Validate()
will run through the rules in the order that they are listed. If a rule fails the validation, the method will return the corresponding error and skip the rest of the rules. The method will return nil if the value passes all validation rules.
For a struct value, you usually want to check if its fields are valid. For example, in a RESTful application, you may unmarshal the request payload into a struct and then validate the struct fields. If one or multiple fields are invalid, you may want to get an error describing which fields are invalid. You can use validation.ValidateStruct()
to achieve this purpose. A single struct can have rules for multiple fields, and a field can be associated with multiple rules. For example,
type Address struct {
Street string
City string
State string
Zip string
}
func (a Address) Validate() error {
return validation.ValidateStruct(&a,
// Street cannot be empty, and the length must between 5 and 50
validation.Field(&a.Street, validation.Required, validation.Length(5, 50)),
// City cannot be empty, and the length must between 5 and 50
validation.Field(&a.City, validation.Required, validation.Length(5, 50)),
// State cannot be empty, and must be a string consisting of two letters in upper case
validation.Field(&a.State, validation.Required, validation.Match(regexp.MustCompile("^[A-Z]{2}$"))),
// State cannot be empty, and must be a string consisting of five digits
validation.Field(&a.Zip, validation.Required, validation.Match(regexp.MustCompile("^[0-9]{5}$"))),
)
}
a := Address{
Street: "123",
City: "Unknown",
State: "Virginia",
Zip: "12345",
}
err := a.Validate()
fmt.Println(err)
// Output:
// Street: the length must be between 5 and 50; State: must be in a valid format.
Note that when calling validation.ValidateStruct
to validate a struct, you should pass to the method a pointer to the struct instead of the struct itself. Similarly, when calling validation.Field
to specify the rules for a struct field, you should use a pointer to the struct field.
When the struct validation is performed, the fields are validated in the order they are specified in ValidateStruct
. And when each field is validated, its rules are also evaluated in the order they are associated with the field. If a rule fails, an error is recorded for that field, and the validation will continue with the next field.
A norms and conventions validator for Terraform.
This tool will help you ensure that a terraform folder answer to your norms and conventions rules. This can be really useful in several cases :
Features:
outputs.tf
)..tf
files are present.⚠️ Terraform 0.12+ is supported only by the versions 2.0.0 and higher.
Please find the full documentation here (ReadTheDocs).
Go package for data validation and filtering. support validate Map, Struct, Request(Form, JSON, url.Values, Uploaded Files) data and more features.
validate
is a generic Go data validate and filter tool library.
Map
, Struct
, Request
(Form
, JSON
, url.Values
, UploadedFile
) datahttp.Request
automatically collects data based on the request Content-Type
valuev.StringRule("tags.*", "required|string")
message
, label
tags in structen
, zh-CN
, zh-TW
validate
in any frameworks, such as Gin, Echo, Chi and morevalidate.Val("xyz@mail.com", "required|email")
Inspired the projects albrow/forms and asaskevich/govalidator and inhere/php-validate. Thank you very much
Use the validate
tag of the structure, you can quickly config a structure.
Field translations and error messages for structs can be quickly configured using the message
and label
tags.
json
tag by defaultmessage
taglabel
tagpackage main
import (
"fmt"
"time"
"github.com/gookit/validate"
)
// UserForm struct
type UserForm struct {
Name string `validate:"required|min_len:7" message:"required:{field} is required" label:"User Name"`
Email string `validate:"email" message:"email is invalid" label:"User Email"`
Age int `validate:"required|int|min:1|max:99" message:"int:age must int|min:age min value is 1"`
CreateAt int `validate:"min:1"`
Safe int `validate:"-"`
UpdateAt time.Time `validate:"required" message:"update time is required"`
Code string `validate:"customValidator"`
// ExtInfo nested struct
ExtInfo struct{
Homepage string `validate:"required" label:"Home Page"`
CityName string
} `validate:"required" label:"Home Page"`
}
// CustomValidator custom validator in the source struct.
func (f UserForm) CustomValidator(val string) bool {
return len(val) == 4
}
This package provides a framework for writing validations for Go applications. It does provide you with few validators, but if you need others you can easly build them.
$ go get github.com/gobuffalo/validate
Using validate is pretty easy, just define some Validator
objects and away you go.
Here is a pretty simple example:
package main
import (
"log"
v "github.com/gobuffalo/validate"
)
type User struct {
Name string
Email string
}
func (u *User) IsValid(errors *v.Errors) {
if u.Name == "" {
errors.Add("name", "Name must not be blank!")
}
if u.Email == "" {
errors.Add("email", "Email must not be blank!")
}
}
func main() {
u := User{Name: "", Email: ""}
errors := v.Validate(&u)
log.Println(errors.Errors)
// map[name:[Name must not be blank!] email:[Email must not be blank!]]
}
In the previous example I wrote a single Validator
for the User
struct. To really get the benefit of using go-validator, as well as the Go language, I would recommend creating distinct validators for each thing you want to validate, that way they can be run concurrently.
package main
import (
"fmt"
"log"
"strings"
v "github.com/gobuffalo/validate"
)
type User struct {
Name string
Email string
}
type PresenceValidator struct {
Field string
Value string
}
func (v *PresenceValidator) IsValid(errors *v.Errors) {
if v.Value == "" {
errors.Add(strings.ToLower(v.Field), fmt.Sprintf("%s must not be blank!", v.Field))
}
}
func main() {
u := User{Name: "", Email: ""}
errors := v.Validate(&PresenceValidator{"Email", u.Email}, &PresenceValidator{"Name", u.Name})
log.Println(errors.Errors)
// map[name:[Name must not be blank!] email:[Email must not be blank!]]
}
That's really it. Pretty simple and straight-forward Just a nice clean framework for writing your own validators. Use in good health.
Thank you for following this article.
Golang Microservices: Validations
1663623480
In today's post we will learn about 10 Popular Golang Libraries for Validation.
What is Validation?
Validation is a simple concept to understand but difficult to put into practice.
Validation is the recognition and acceptance of another persons internal experience as being valid. Emotional validation is distinguished from emotional invalidation, in which your own or another persons emotional experiences are rejected, ignored, or judged. Self-validation is the recognition and acknowledgement of your own internal experience.
Validation does not mean agreeing with or supporting feelings or thoughts. Validating does not mean love. You can validate someone you don’t like even though you probably wouldn’t want to.
Table of contents:
Go Struct and Field validation, including Cross Field, Cross Struct, Map, Slice and Array diving.
Package validator implements value validations for structs and individual fields based on tags.
It has the following unique features:
Use go get.
go get github.com/go-playground/validator/v10
Then import the validator package into your own code.
import "github.com/go-playground/validator/v10"
Validation functions return type error
They return type error to avoid the issue discussed in the following, where err is always != nil:
Validator returns only InvalidValidationError for bad validation input, nil or ValidationErrors as type error; so, in your code all you need to do is check if the error returned is not nil, and if it's not check if error is InvalidValidationError ( if necessary, most of the time it isn't ) type cast it to type ValidationErrors like so:
err := validate.Struct(mystruct)
validationErrors := err.(validator.ValidationErrors)
Please see https://pkg.go.dev/github.com/go-playground/validator/v10 for detailed usage docs.
Run on MacBook Pro (15-inch, 2017) go version go1.10.2 darwin/amd64
goos: darwin
goarch: amd64
pkg: github.com/go-playground/validator
BenchmarkFieldSuccess-8 20000000 83.6 ns/op 0 B/op 0 allocs/op
BenchmarkFieldSuccessParallel-8 50000000 26.8 ns/op 0 B/op 0 allocs/op
BenchmarkFieldFailure-8 5000000 291 ns/op 208 B/op 4 allocs/op
BenchmarkFieldFailureParallel-8 20000000 107 ns/op 208 B/op 4 allocs/op
BenchmarkFieldArrayDiveSuccess-8 2000000 623 ns/op 201 B/op 11 allocs/op
BenchmarkFieldArrayDiveSuccessParallel-8 10000000 237 ns/op 201 B/op 11 allocs/op
BenchmarkFieldArrayDiveFailure-8 2000000 859 ns/op 412 B/op 16 allocs/op
BenchmarkFieldArrayDiveFailureParallel-8 5000000 335 ns/op 413 B/op 16 allocs/op
BenchmarkFieldMapDiveSuccess-8 1000000 1292 ns/op 432 B/op 18 allocs/op
BenchmarkFieldMapDiveSuccessParallel-8 3000000 467 ns/op 432 B/op 18 allocs/op
BenchmarkFieldMapDiveFailure-8 1000000 1082 ns/op 512 B/op 16 allocs/op
BenchmarkFieldMapDiveFailureParallel-8 5000000 425 ns/op 512 B/op 16 allocs/op
BenchmarkFieldMapDiveWithKeysSuccess-8 1000000 1539 ns/op 480 B/op 21 allocs/op
BenchmarkFieldMapDiveWithKeysSuccessParallel-8 3000000 613 ns/op 480 B/op 21 allocs/op
BenchmarkFieldMapDiveWithKeysFailure-8 1000000 1413 ns/op 721 B/op 21 allocs/op
BenchmarkFieldMapDiveWithKeysFailureParallel-8 3000000 575 ns/op 721 B/op 21 allocs/op
BenchmarkFieldCustomTypeSuccess-8 10000000 216 ns/op 32 B/op 2 allocs/op
BenchmarkFieldCustomTypeSuccessParallel-8 20000000 82.2 ns/op 32 B/op 2 allocs/op
BenchmarkFieldCustomTypeFailure-8 5000000 274 ns/op 208 B/op 4 allocs/op
BenchmarkFieldCustomTypeFailureParallel-8 20000000 116 ns/op 208 B/op 4 allocs/op
BenchmarkFieldOrTagSuccess-8 2000000 740 ns/op 16 B/op 1 allocs/op
BenchmarkFieldOrTagSuccessParallel-8 3000000 474 ns/op 16 B/op 1 allocs/op
BenchmarkFieldOrTagFailure-8 3000000 471 ns/op 224 B/op 5 allocs/op
BenchmarkFieldOrTagFailureParallel-8 3000000 414 ns/op 224 B/op 5 allocs/op
BenchmarkStructLevelValidationSuccess-8 10000000 213 ns/op 32 B/op 2 allocs/op
BenchmarkStructLevelValidationSuccessParallel-8 20000000 91.8 ns/op 32 B/op 2 allocs/op
BenchmarkStructLevelValidationFailure-8 3000000 473 ns/op 304 B/op 8 allocs/op
BenchmarkStructLevelValidationFailureParallel-8 10000000 234 ns/op 304 B/op 8 allocs/op
BenchmarkStructSimpleCustomTypeSuccess-8 5000000 385 ns/op 32 B/op 2 allocs/op
BenchmarkStructSimpleCustomTypeSuccessParallel-8 10000000 161 ns/op 32 B/op 2 allocs/op
BenchmarkStructSimpleCustomTypeFailure-8 2000000 640 ns/op 424 B/op 9 allocs/op
BenchmarkStructSimpleCustomTypeFailureParallel-8 5000000 318 ns/op 440 B/op 10 allocs/op
BenchmarkStructFilteredSuccess-8 2000000 597 ns/op 288 B/op 9 allocs/op
BenchmarkStructFilteredSuccessParallel-8 10000000 266 ns/op 288 B/op 9 allocs/op
BenchmarkStructFilteredFailure-8 3000000 454 ns/op 256 B/op 7 allocs/op
BenchmarkStructFilteredFailureParallel-8 10000000 214 ns/op 256 B/op 7 allocs/op
BenchmarkStructPartialSuccess-8 3000000 502 ns/op 256 B/op 6 allocs/op
BenchmarkStructPartialSuccessParallel-8 10000000 225 ns/op 256 B/op 6 allocs/op
BenchmarkStructPartialFailure-8 2000000 702 ns/op 480 B/op 11 allocs/op
BenchmarkStructPartialFailureParallel-8 5000000 329 ns/op 480 B/op 11 allocs/op
BenchmarkStructExceptSuccess-8 2000000 793 ns/op 496 B/op 12 allocs/op
BenchmarkStructExceptSuccessParallel-8 10000000 193 ns/op 240 B/op 5 allocs/op
BenchmarkStructExceptFailure-8 2000000 639 ns/op 464 B/op 10 allocs/op
BenchmarkStructExceptFailureParallel-8 5000000 300 ns/op 464 B/op 10 allocs/op
BenchmarkStructSimpleCrossFieldSuccess-8 3000000 417 ns/op 72 B/op 3 allocs/op
BenchmarkStructSimpleCrossFieldSuccessParallel-8 10000000 163 ns/op 72 B/op 3 allocs/op
BenchmarkStructSimpleCrossFieldFailure-8 2000000 645 ns/op 304 B/op 8 allocs/op
BenchmarkStructSimpleCrossFieldFailureParallel-8 5000000 285 ns/op 304 B/op 8 allocs/op
BenchmarkStructSimpleCrossStructCrossFieldSuccess-8 3000000 588 ns/op 80 B/op 4 allocs/op
BenchmarkStructSimpleCrossStructCrossFieldSuccessParallel-8 10000000 221 ns/op 80 B/op 4 allocs/op
BenchmarkStructSimpleCrossStructCrossFieldFailure-8 2000000 868 ns/op 320 B/op 9 allocs/op
BenchmarkStructSimpleCrossStructCrossFieldFailureParallel-8 5000000 337 ns/op 320 B/op 9 allocs/op
BenchmarkStructSimpleSuccess-8 5000000 260 ns/op 0 B/op 0 allocs/op
BenchmarkStructSimpleSuccessParallel-8 20000000 90.6 ns/op 0 B/op 0 allocs/op
BenchmarkStructSimpleFailure-8 2000000 619 ns/op 424 B/op 9 allocs/op
BenchmarkStructSimpleFailureParallel-8 5000000 296 ns/op 424 B/op 9 allocs/op
BenchmarkStructComplexSuccess-8 1000000 1454 ns/op 128 B/op 8 allocs/op
BenchmarkStructComplexSuccessParallel-8 3000000 579 ns/op 128 B/op 8 allocs/op
BenchmarkStructComplexFailure-8 300000 4140 ns/op 3041 B/op 53 allocs/op
BenchmarkStructComplexFailureParallel-8 1000000 2127 ns/op 3041 B/op 53 allocs/op
BenchmarkOneof-8 10000000 140 ns/op 0 B/op 0 allocs/op
BenchmarkOneofParallel-8 20000000 70.1 ns/op 0 B/op 0 allocs/op
🎈 A lightweight struct validator for Go.
go get github.com/guiferpa/gody/v2
package main
import (
"encoding/json"
"fmt"
"net/http"
gody "github.com/guiferpa/gody/v2"
"github.com/guiferpa/gody/v2/rule"
)
type RequestBody struct {
Name string `json:"name" validate:"not_empty"`
Age int `json:"age" validate:"min=21"`
}
func HTTPHandler(v *gody.Validator) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var body RequestBody
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
...
}
defer r.Body.Close()
if isValidated, err := v.Validate(body); err != nil {
...
}
})
}
func main() {
validator := gody.NewValidator()
validator.AddRules(rule.NotEmpty, rule.Min)
port := ":3000"
http.ListenAndServe(port, HTTPHandler(validator))
}
There are others ways to valid a struct, take a look on functions below:
gody.RawValidate(interface{}, string, []gody.Rule) (bool, error)
gody.Validate(interface{}, []gody.Rule) (bool, error)
gody.RawDefaultValidate(interface{}, string, []gody.Rule) (bool, error)
gody.DefaultValidate(interface{}, []gody.Rule) (bool, error)
Fast, tag-based validation for structs.
package main
import (
"fmt"
"log"
"strings"
"github.com/twharmon/govalid"
)
type Post struct {
// ID has no constraints
ID int
// Title is required, must be at least 3 characters long, cannot be
// more than 20 characters long, and must match ^[a-zA-Z ]+$
Title string `govalid:"req|min:3|max:20|regex:^[a-zA-Z ]+$"`
// Body is not required, cannot be more than 10000 charachers,
// and must be "fun" (a custom rule defined below).
Body string `govalid:"max:10000|fun"`
// Category is not required, but if not zero value ("") it must be
// either "announcement" or "bookreview".
Category string `govalid:"in:announcement,bookreview"`
}
var v = govalid.New()
func main() {
// Add Custom validation to the struct `Post`
v.AddCustom(Post{}, func(val interface{}) string {
post := val.(*Post)
if post.Category != "" && !strings.Contains(post.Body, post.Category) {
return fmt.Sprintf("Body must contain %s", post.Category)
}
return ""
})
// Add custom string "fun" that can be used on any string field
// in any struct.
v.AddCustomStringRule("fun", func(field string, value string) string {
if float64(strings.Count(value, "!")) / float64(utf8.RuneCountInString(value)) > 0.001 {
return ""
}
return fmt.Sprintf("%s must contain more exclamation marks", field)
})
p := Post{
ID: 5,
Title: "Hi",
Body: "Hello world!",
Category: "announcement",
}
vio, err := v.Violations(&p)
if err != nil {
log.Fatalln(err)
}
fmt.Println(vio)
}
BenchmarkValidatorStringReqInvalid 267.3 ns/op 48 B/op 3 allocs/op
BenchmarkValidatorStringReqValid 92.35 ns/op 16 B/op 1 allocs/op
BenchmarkValidatorsVariety 1484 ns/op 297 B/op 15 allocs/op
Make a pull request.
Validators and sanitizers for strings, numerics, slices and structs.
Make sure that Go is installed on your computer. Type the following command in your terminal:
go get github.com/asaskevich/govalidator
or you can get specified release of the package with gopkg.in
:
go get gopkg.in/asaskevich/govalidator.v10
After it the package is ready to use.
Add following line in your *.go
file:
import "github.com/asaskevich/govalidator"
If you are unhappy to use long govalidator
, you can do something like this:
import (
valid "github.com/asaskevich/govalidator"
)
SetFieldsRequiredByDefault
causes validation to fail when struct fields do not include validations or are not explicitly marked as exempt (using valid:"-"
or valid:"email,optional"
). A good place to activate this is a package init function or the main() function.
SetNilPtrAllowedByRequired
causes validation to pass when struct fields marked by required
are set to nil. This is disabled by default for consistency, but some packages that need to be able to determine between nil
and zero value
state can use this. If disabled, both nil
and zero
values cause validation errors.
import "github.com/asaskevich/govalidator"
func init() {
govalidator.SetFieldsRequiredByDefault(true)
}
Here's some code to explain it:
// this struct definition will fail govalidator.ValidateStruct() (and the field values do not matter):
type exampleStruct struct {
Name string ``
Email string `valid:"email"`
}
// this, however, will only fail when Email is empty or an invalid email address:
type exampleStruct2 struct {
Name string `valid:"-"`
Email string `valid:"email"`
}
// lastly, this will only fail when Email is an invalid email address but not when it's empty:
type exampleStruct2 struct {
Name string `valid:"-"`
Email string `valid:"email,optional"`
}
Custom validator function signature
A context was added as the second parameter, for structs this is the object being validated – this makes dependent validation possible.
import "github.com/asaskevich/govalidator"
// old signature
func(i interface{}) bool
// new signature
func(i interface{}, o interface{}) bool
Adding a custom validator
This was changed to prevent data races when accessing custom validators.
import "github.com/asaskevich/govalidator"
// before
govalidator.CustomTypeTagMap["customByteArrayValidator"] = func(i interface{}, o interface{}) bool {
// ...
}
// after
govalidator.CustomTypeTagMap.Set("customByteArrayValidator", func(i interface{}, o interface{}) bool {
// ...
})
Validate Golang request data with simple rules. Highly inspired by Laravel's request validation.
Install the package using
$ go get github.com/thedevsaddam/govalidator
// or
$ go get gopkg.in/thedevsaddam/govalidator.v1
To use the package import it in your *.go
code
import "github.com/thedevsaddam/govalidator"
// or
import "gopkg.in/thedevsaddam/govalidator.v1"
Validate form-data
, x-www-form-urlencoded
and query params
package main
import (
"encoding/json"
"fmt"
"net/http"
"github.com/thedevsaddam/govalidator"
)
func handler(w http.ResponseWriter, r *http.Request) {
rules := govalidator.MapData{
"username": []string{"required", "between:3,8"},
"email": []string{"required", "min:4", "max:20", "email"},
"web": []string{"url"},
"phone": []string{"digits:11"},
"agree": []string{"bool"},
"dob": []string{"date"},
}
messages := govalidator.MapData{
"username": []string{"required:আপনাকে অবশ্যই ইউজারনেম দিতে হবে", "between:ইউজারনেম অবশ্যই ৩-৮ অক্ষর হতে হবে"},
"phone": []string{"digits:ফোন নাম্বার অবশ্যই ১১ নম্বারের হতে হবে"},
}
opts := govalidator.Options{
Request: r, // request object
Rules: rules, // rules map
Messages: messages, // custom message map (Optional)
RequiredDefault: true, // all the field to be pass the rules
}
v := govalidator.New(opts)
e := v.Validate()
err := map[string]interface{}{"validationError": e}
w.Header().Set("Content-type", "application/json")
json.NewEncoder(w).Encode(err)
}
func main() {
http.HandleFunc("/", handler)
fmt.Println("Listening on port: 9000")
http.ListenAndServe(":9000", nil)
}
Send request to the server using curl or postman: curl GET "http://localhost:9000?web=&phone=&zip=&dob=&agree="
Jio is a json schema validator similar to joi.
Parameter validation in Golang is really a cursing problem. Defining tags on structs is not easy to extend rules, handwritten validation code makes logic code cumbersome, and the initial zero value of the struct field will also interfere with the validation.
jio tries validate json raw data before deserialization to avoid these problems. Defining validation rules as Schema is easy to read and easy to extend (Inspired by Hapi.js joi library). Rules within Schema can be validated in the order of registration, and context can be used to exchange data between rules, and can access other field data even within a single rule, etc.
jio provides a flexible enough way to make your validation simple and efficient!
package main
import (
"log"
"github.com/faceair/jio"
)
func main() {
data := []byte(`{
"debug": "on",
"window": {
"title": "Sample Widget",
"size": [500, 500]
}
}`)
_, err := jio.ValidateJSON(&data, jio.Object().Keys(jio.K{
"debug": jio.Bool().Truthy("on").Required(),
"window": jio.Object().Keys(jio.K{
"title": jio.String().Min(3).Max(18),
"size": jio.Array().Items(jio.Number().Integer()).Length(2).Required(),
}).Without("name", "title").Required(),
}))
if err != nil {
panic(err)
}
log.Printf("%s", data) // {"debug":true,"window":{"size":[500,500],"title":"Sample Widget"}}
}
The above schema defines the following constraints:
debug
on
string instead of true
window
name
and title
title
size
Take chi as an example, the other frameworks are similar.
package main
import (
"io/ioutil"
"net/http"
"github.com/faceair/jio"
"github.com/go-chi/chi"
)
func main() {
r := chi.NewRouter()
r.Route("/people", func(r chi.Router) {
r.With(jio.ValidateBody(jio.Object().Keys(jio.K{
"name": jio.String().Min(3).Max(10).Required(),
"age": jio.Number().Integer().Min(0).Max(100).Required(),
"phone": jio.String().Regex(`^1[34578]\d{9}$`).Required(),
}), jio.DefaultErrorHandler)).Post("/", func(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
panic(err)
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
w.Write(body)
})
})
http.ListenAndServe(":8080", r)
}
The second parameter of jio.ValidateBody
is called for error handling when the validation fails.
Supports validation of various data types (structs, strings, maps, slices, etc.) with configurable and extensible validation rules specified in usual code constructs instead of struct tags.
ozzo-validation is a Go package that provides configurable and extensible data validation capabilities. It has the following features:
Validatable
interface.sql.Valuer
interface (e.g. sql.NullString
).For an example on how this library is used in an application, please refer to go-rest-api which is a starter kit for building RESTful APIs in Go.
Go 1.13 or above.
The ozzo-validation package mainly includes a set of validation rules and two validation methods. You use validation rules to describe how a value should be considered valid, and you call either validation.Validate()
or validation.ValidateStruct()
to validate the value.
Run the following command to install the package:
go get github.com/go-ozzo/ozzo-validation
For a simple value, such as a string or an integer, you may use validation.Validate()
to validate it. For example,
package main
import (
"fmt"
"github.com/go-ozzo/ozzo-validation/v4"
"github.com/go-ozzo/ozzo-validation/v4/is"
)
func main() {
data := "example"
err := validation.Validate(data,
validation.Required, // not empty
validation.Length(5, 100), // length between 5 and 100
is.URL, // is a valid URL
)
fmt.Println(err)
// Output:
// must be a valid URL
}
The method validation.Validate()
will run through the rules in the order that they are listed. If a rule fails the validation, the method will return the corresponding error and skip the rest of the rules. The method will return nil if the value passes all validation rules.
For a struct value, you usually want to check if its fields are valid. For example, in a RESTful application, you may unmarshal the request payload into a struct and then validate the struct fields. If one or multiple fields are invalid, you may want to get an error describing which fields are invalid. You can use validation.ValidateStruct()
to achieve this purpose. A single struct can have rules for multiple fields, and a field can be associated with multiple rules. For example,
type Address struct {
Street string
City string
State string
Zip string
}
func (a Address) Validate() error {
return validation.ValidateStruct(&a,
// Street cannot be empty, and the length must between 5 and 50
validation.Field(&a.Street, validation.Required, validation.Length(5, 50)),
// City cannot be empty, and the length must between 5 and 50
validation.Field(&a.City, validation.Required, validation.Length(5, 50)),
// State cannot be empty, and must be a string consisting of two letters in upper case
validation.Field(&a.State, validation.Required, validation.Match(regexp.MustCompile("^[A-Z]{2}$"))),
// State cannot be empty, and must be a string consisting of five digits
validation.Field(&a.Zip, validation.Required, validation.Match(regexp.MustCompile("^[0-9]{5}$"))),
)
}
a := Address{
Street: "123",
City: "Unknown",
State: "Virginia",
Zip: "12345",
}
err := a.Validate()
fmt.Println(err)
// Output:
// Street: the length must be between 5 and 50; State: must be in a valid format.
Note that when calling validation.ValidateStruct
to validate a struct, you should pass to the method a pointer to the struct instead of the struct itself. Similarly, when calling validation.Field
to specify the rules for a struct field, you should use a pointer to the struct field.
When the struct validation is performed, the fields are validated in the order they are specified in ValidateStruct
. And when each field is validated, its rules are also evaluated in the order they are associated with the field. If a rule fails, an error is recorded for that field, and the validation will continue with the next field.
A norms and conventions validator for Terraform.
This tool will help you ensure that a terraform folder answer to your norms and conventions rules. This can be really useful in several cases :
Features:
outputs.tf
)..tf
files are present.⚠️ Terraform 0.12+ is supported only by the versions 2.0.0 and higher.
Please find the full documentation here (ReadTheDocs).
Go package for data validation and filtering. support validate Map, Struct, Request(Form, JSON, url.Values, Uploaded Files) data and more features.
validate
is a generic Go data validate and filter tool library.
Map
, Struct
, Request
(Form
, JSON
, url.Values
, UploadedFile
) datahttp.Request
automatically collects data based on the request Content-Type
valuev.StringRule("tags.*", "required|string")
message
, label
tags in structen
, zh-CN
, zh-TW
validate
in any frameworks, such as Gin, Echo, Chi and morevalidate.Val("xyz@mail.com", "required|email")
Inspired the projects albrow/forms and asaskevich/govalidator and inhere/php-validate. Thank you very much
Use the validate
tag of the structure, you can quickly config a structure.
Field translations and error messages for structs can be quickly configured using the message
and label
tags.
json
tag by defaultmessage
taglabel
tagpackage main
import (
"fmt"
"time"
"github.com/gookit/validate"
)
// UserForm struct
type UserForm struct {
Name string `validate:"required|min_len:7" message:"required:{field} is required" label:"User Name"`
Email string `validate:"email" message:"email is invalid" label:"User Email"`
Age int `validate:"required|int|min:1|max:99" message:"int:age must int|min:age min value is 1"`
CreateAt int `validate:"min:1"`
Safe int `validate:"-"`
UpdateAt time.Time `validate:"required" message:"update time is required"`
Code string `validate:"customValidator"`
// ExtInfo nested struct
ExtInfo struct{
Homepage string `validate:"required" label:"Home Page"`
CityName string
} `validate:"required" label:"Home Page"`
}
// CustomValidator custom validator in the source struct.
func (f UserForm) CustomValidator(val string) bool {
return len(val) == 4
}
This package provides a framework for writing validations for Go applications. It does provide you with few validators, but if you need others you can easly build them.
$ go get github.com/gobuffalo/validate
Using validate is pretty easy, just define some Validator
objects and away you go.
Here is a pretty simple example:
package main
import (
"log"
v "github.com/gobuffalo/validate"
)
type User struct {
Name string
Email string
}
func (u *User) IsValid(errors *v.Errors) {
if u.Name == "" {
errors.Add("name", "Name must not be blank!")
}
if u.Email == "" {
errors.Add("email", "Email must not be blank!")
}
}
func main() {
u := User{Name: "", Email: ""}
errors := v.Validate(&u)
log.Println(errors.Errors)
// map[name:[Name must not be blank!] email:[Email must not be blank!]]
}
In the previous example I wrote a single Validator
for the User
struct. To really get the benefit of using go-validator, as well as the Go language, I would recommend creating distinct validators for each thing you want to validate, that way they can be run concurrently.
package main
import (
"fmt"
"log"
"strings"
v "github.com/gobuffalo/validate"
)
type User struct {
Name string
Email string
}
type PresenceValidator struct {
Field string
Value string
}
func (v *PresenceValidator) IsValid(errors *v.Errors) {
if v.Value == "" {
errors.Add(strings.ToLower(v.Field), fmt.Sprintf("%s must not be blank!", v.Field))
}
}
func main() {
u := User{Name: "", Email: ""}
errors := v.Validate(&PresenceValidator{"Email", u.Email}, &PresenceValidator{"Name", u.Name})
log.Println(errors.Errors)
// map[name:[Name must not be blank!] email:[Email must not be blank!]]
}
That's really it. Pretty simple and straight-forward Just a nice clean framework for writing your own validators. Use in good health.
Thank you for following this article.
Golang Microservices: Validations
1594769515
Data validation and sanitization is a very important thing from security point of view for a web application. We can not rely on user’s input. In this article i will let you know how to validate mobile phone number in laravel with some examples.
if we take some user’s information in our application, so usually we take phone number too. And if validation on the mobile number field is not done, a user can put anything in the mobile number field and without genuine phone number, this data would be useless.
Since we know that mobile number can not be an alpha numeric or any alphabates aand also it should be 10 digit number. So here in this examples we will add 10 digit number validation in laravel application.
We will aalso see the uses of regex in the validation of mobile number. So let’s do it with two different way in two examples.
In this first example we will write phone number validation in HomeController where we will processs user’s data.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
class HomeController extends Controller
{
/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('createUser');
}
/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$request->validate([
'name' => 'required',
'phone' => 'required|digits:10',
'email' => 'required|email|unique:users'
]);
$input = $request->all();
$user = User::create($input);
return back()->with('success', 'User created successfully.');
}
}
In this second example, we will use regex for user’s mobile phone number validation before storing user data in our database. Here, we will write the validation in Homecontroller like below.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
use Validator;
class HomeController extends Controller
{
/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('createUser');
}
/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$request->validate([
'name' => 'required',
'phone' => 'required|regex:/^([0-9\s\-\+\(\)]*)$/|min:10',
'email' => 'required|email|unique:users'
]);
$input = $request->all();
$user = User::create($input);
return back()->with('success', 'User created successfully.');
}
}
#laravel #laravel phone number validation #laravel phone validation #laravel validation example #mobile phone validation in laravel #phone validation with regex #validate mobile in laravel
1666770774
In this Python tutorial for beginners, we learn about Variables in Python. Variables are containers for storing data values. A Python variable is a symbolic name that is a reference or pointer to an object.
Code in GitHub: https://github.com/AlexTheAnalyst/PythonYouTubeSeries/blob/main/Python%20Basics%20101%20-%20Variables.ipynb
Python has no command for declaring a variable.
A variable is created the moment you first assign a value to it.
x = 5
y = "John"
print(x)
print(y)
Variables do not need to be declared with any particular type, and can even change type after they have been set.
x = 4 # x is of type int
x = "Sally" # x is now of type str
print(x)
If you want to specify the data type of a variable, this can be done with casting.
x = str(3) # x will be '3'
y = int(3) # y will be 3
z = float(3) # z will be 3.0
You can get the data type of a variable with the type()
function.
x = 5
y = "John"
print(type(x))
print(type(y))
String variables can be declared either by using single or double quotes:
x = "John"
# is the same as
x = 'John'
Variable names are case-sensitive.
This will create two variables:
a = 4
A = "Sally"
#A will not overwrite a
A Python variable is a reserved memory location to store values. In other words, a variable in a python program gives data to the computer for processing.
Every value in Python has a datatype. Different data types in Python are Numbers, List, Tuple, Strings, Dictionary, etc. Variables in Python can be declared by any name or even alphabets like a, aa, abc, etc.
In this tutorial, we will learn,
Let see an example. We will define variable in Python and declare it as “a” and print it.
a=100
print (a)
You can re-declare Python variables even after you have declared once.
Here we have Python declare variable initialized to f=0.
Later, we re-assign the variable f to value “guru99”
Python 2 Example
# Declare a variable and initialize it
f = 0
print f
# re-declaring the variable works
f = 'guru99'
print f
Python 3 Example
# Declare a variable and initialize it
f = 0
print(f)
# re-declaring the variable works
f = 'guru99'
print(f)
Let’s see whether you can concatenate different data types like string and number together. For example, we will concatenate “Guru” with the number “99”.
Unlike Java, which concatenates number with string without declaring number as string, while declaring variables in Python requires declaring the number as string otherwise it will show a TypeError
For the following code, you will get undefined output –
a="Guru"
b = 99
print a+b
Once the integer is declared as string, it can concatenate both “Guru” + str(“99”)= “Guru99” in the output.
a="Guru"
b = 99
print(a+str(b))
There are two types of variables in Python, Global variable and Local variable. When you want to use the same variable for rest of your program or module you declare it as a global variable, while if you want to use the variable in a specific function or method, you use a local variable while Python variable declaration.
Let’s understand this Python variable types with the difference between local and global variables in the below program.
Python 2 Example
# Declare a variable and initialize it
f = 101
print f
# Global vs. local variables in functions
def someFunction():
# global f
f = 'I am learning Python'
print f
someFunction()
print f
Python 3 Example
# Declare a variable and initialize it
f = 101
print(f)
# Global vs. local variables in functions
def someFunction():
# global f
f = 'I am learning Python'
print(f)
someFunction()
print(f)
While Python variable declaration using the keyword global, you can reference the global variable inside a function.
We changed the value of “f” inside the function. Once the function call is over, the changed value of the variable “f” persists. At line 12, when we again, print the value of “f” is it displays the value “changing global variable”
Python 2 Example
f = 101;
print f
# Global vs.local variables in functions
def someFunction():
global f
print f
f = "changing global variable"
someFunction()
print f
Python 3 Example
f = 101;
print(f)
# Global vs.local variables in functions
def someFunction():
global f
print(f)
f = "changing global variable"
someFunction()
print(f)
You can also delete Python variables using the command del “variable name”.
In the below example of Python delete variable, we deleted variable f, and when we proceed to print it, we get error “variable name is not defined” which means you have deleted the variable.
Example of Python delete variable or Python clear variable :
f = 11;
print(f)
del f
print(f)
To delete a variable, it uses keyword “del”.
A variable is a fundamental concept in any programming language. It is a reserved memory location that stores and manipulates data. This tutorial on Python variables will help you learn more about what they are, the different data types of variables, the rules for naming variables in Python. You will also perform some basic operations on numbers and strings. We’ll use Jupyter Notebook to implement the Python codes.
Variables are entities of a program that holds a value. Here is an example of a variable:
x=100
In the below diagram, the box holds a value of 100 and is named as x. Therefore, the variable is x, and the data it holds is the value.
The data type for a variable is the type of data it holds.
In the above example, x is holding 100, which is a number, and the data type of x is a number.
In Python, there are three types of numbers: Integer, Float, and Complex.
Integers are numbers without decimal points. Floats are numbers with decimal points. Complex numbers have real parts and imaginary parts.
Another data type that is very different from a number is called a string, which is a collection of characters.
Let’s see a variable with an integer data type:
x=100
To check the data type of x, use the type() function:
type(x)
Python allows you to assign variables while performing arithmetic operations.
x=654*6734
type(x)
To display the output of the variable, use the print() function.
print(x) #It gives the product of the two numbers
Now, let’s see an example of a floating-point number:
x=3.14
print(x)
type(x) #Here the type the variable is float
Strings are declared within a single or double quote.
x=’Simplilearn’
print(x)
x=” Simplilearn.”
print(x)
type(x)
In all of the examples above, we only assigned a single value to the variables. Python has specific data types or objects that hold a collection of values, too. A Python List is one such example.
Here is an example of a list:
x=[14,67,9]
print(x)
type(x)
You can extract the values from the list using the index position method. In lists, the first element index position starts at zero, the second element at one, the third element at two, and so on.
To extract the first element from the list x:
print(x[0])
To extract the third element from the list x:
print(x[2])
Lists are mutable objects, which means you can change the values in a list once they are declared.
x[2]=70 #Reassigning the third element in the list to 70
print(x)
Earlier, the elements in the list had [14, 67, 9]. Now, they have [14, 67, 70].
Tuples are a type of Python object that holds a collection of value, which is ordered and immutable. Unlike a list that uses a square bracket, tuples use parentheses.
x=(4,8,6)
print(x)
type(x)
Similar to lists, tuples can also be extracted with the index position method.
print(x[1]) #Give the element present at index 1, i.e. 8
If you want to change any value in a tuple, it will throw an error. Once you have stored the values in a variable for a tuple, it remains the same.
When we deal with files, we need a variable that points to it, called file pointers. The advantage of having file pointers is that when you need to perform various operations on a file, instead of providing the file’s entire path location or name every time, you can assign it to a particular variable and use that instead.
Here is how you can assign a variable to a file:
x=open(‘C:/Users/Simplilearn/Downloads/JupyterNotebook.ipynb’,’r’)
type(x)
Suppose you want to assign values to multiple variables. Instead of having multiple lines of code for each variable, you can assign it in a single line of code.
(x, y, z)=5, 10, 5
The following line code results in an error because the number of values assigned doesn’t match with the number of variables declared.
If you want to assign the same value to multiple variables, use the following syntax:
x=y=z=1
Now, let's look at the various rules for naming a variable.
1. A variable name must begin with a letter of the alphabet or an underscore(_)
Example:
abc=100 #valid syntax
_abc=100 #valid syntax
3a=10 #invalid syntax
@abc=10 #invalid syntax
. The first character can be followed by letters, numbers or underscores.
Example:
a100=100 #valid
_a984_=100 #valid
a9967$=100 #invalid
xyz-2=100 #invalid
Python variable names are case sensitive.
Example:
a100 is different from A100.
a100=100
A100=200
Reserved words cannot be used as variable names.
Example:
break, class, try, continue, while, if
break=10
class=5
try=100
Python is more effective and more comfortable to perform when you use arithmetic operations.
The following is an example of adding the values of two variables and storing them in a third variable:
x=20
y=10
result=x+y
print(result)
Similarly, we can perform subtraction as well.
result=x-y
print(result)
Additionally, to perform multiplication and division, try the following lines of code:
result=x*y
print(result)
result=x/y
print(result)
As you can see, in the case of division, the result is not an integer, but a float value. To get the result of the division in integers, use “//” — the integer division.
The division of two numbers gives you the quotient. To get the remainder, use the modulo (%) operator.
Now that we know how to perform arithmetic operations on numbers let us look at some operations that can be performed on string variables.
var = ‘Simplilearn’
You can extract each character from the variable using the index position. Similar to lists and tuples, the first element position starts at index zero, the second element index at one, and so on.
print(var[0]) #Gives the character at index 0, i.e. S
print(var[4]) #Gives the character at index 4, i.e. l
If you want to extract a range of characters from the string variable, you can use a colon (:) and provide the range between the ones you want to receive values from. The last index is always excluded. Therefore, you should always provide one plus the number of characters you want to fetch.
print(var[0:3]) #This will extract the first three characters from zero, first, and second index.
The same operation can be performed by excluding the starting index.
print(var[:3])
The following example prints the values from the fifth location until the end of the string.
Let’s see what happens when you try to print the following:
print(var[0:20]) #Prints the entire string, although the string does not have 20 characters.
To print the length of a string, use the len() function.
len(var)
Let’s see how you can extract characters from two strings and generate a new string.
var1 = “It’s Sunday”
var2 = “Have a great day”
The new string should say, “It’s a great Sunday” and be stored in var3.
var3 = var1[:5] + var2[5:13] + var1[5:]
print(var3)
Get prepared for your next career as a professional Python programmer with the Python Certification Training Course. Click to enroll now!
I hope this blog helped you learn the concepts of Python variables. After reading this blog, you may have learned more about what a variable is, rules for declaring a variable, how to perform arithmetic operations on variables, and how to extract elements from numeric and string variables using the index position.
#python #programming
1666082925
This tutorialvideo on 'Arrays in Python' will help you establish a strong hold on all the fundamentals in python programming language. Below are the topics covered in this video:
1:15 What is an array?
2:53 Is python list same as an array?
3:48 How to create arrays in python?
7:19 Accessing array elements
9:59 Basic array operations
- 10:33 Finding the length of an array
- 11:44 Adding Elements
- 15:06 Removing elements
- 18:32 Array concatenation
- 20:59 Slicing
- 23:26 Looping
Python Array Tutorial – Define, Index, Methods
In this article, you'll learn how to use Python arrays. You'll see how to define them and the different methods commonly used for performing operations on them.
The artcile covers arrays that you create by importing the array module
. We won't cover NumPy arrays here.
Let's get started!
Arrays are a fundamental data structure, and an important part of most programming languages. In Python, they are containers which are able to store more than one item at the same time.
Specifically, they are an ordered collection of elements with every value being of the same data type. That is the most important thing to remember about Python arrays - the fact that they can only hold a sequence of multiple items that are of the same type.
Lists are one of the most common data structures in Python, and a core part of the language.
Lists and arrays behave similarly.
Just like arrays, lists are an ordered sequence of elements.
They are also mutable and not fixed in size, which means they can grow and shrink throughout the life of the program. Items can be added and removed, making them very flexible to work with.
However, lists and arrays are not the same thing.
Lists store items that are of various data types. This means that a list can contain integers, floating point numbers, strings, or any other Python data type, at the same time. That is not the case with arrays.
As mentioned in the section above, arrays store only items that are of the same single data type. There are arrays that contain only integers, or only floating point numbers, or only any other Python data type you want to use.
Lists are built into the Python programming language, whereas arrays aren't. Arrays are not a built-in data structure, and therefore need to be imported via the array module
in order to be used.
Arrays of the array module
are a thin wrapper over C arrays, and are useful when you want to work with homogeneous data.
They are also more compact and take up less memory and space which makes them more size efficient compared to lists.
If you want to perform mathematical calculations, then you should use NumPy arrays by importing the NumPy package. Besides that, you should just use Python arrays when you really need to, as lists work in a similar way and are more flexible to work with.
In order to create Python arrays, you'll first have to import the array module
which contains all the necassary functions.
There are three ways you can import the array module
:
import array
at the top of the file. This includes the module array
. You would then go on to create an array using array.array()
.import array
#how you would create an array
array.array()
array.array()
all the time, you could use import array as arr
at the top of the file, instead of import array
alone. You would then create an array by typing arr.array()
. The arr
acts as an alias name, with the array constructor then immediately following it.import array as arr
#how you would create an array
arr.array()
from array import *
, with *
importing all the functionalities available. You would then create an array by writing the array()
constructor alone.from array import *
#how you would create an array
array()
Once you've imported the array module
, you can then go on to define a Python array.
The general syntax for creating an array looks like this:
variable_name = array(typecode,[elements])
Let's break it down:
variable_name
would be the name of the array.typecode
specifies what kind of elements would be stored in the array. Whether it would be an array of integers, an array of floats or an array of any other Python data type. Remember that all elements should be of the same data type.elements
that would be stored in the array, with each element being separated by a comma. You can also create an empty array by just writing variable_name = array(typecode)
alone, without any elements.Below is a typecode table, with the different typecodes that can be used with the different data types when defining Python arrays:
TYPECODE | C TYPE | PYTHON TYPE | SIZE |
---|---|---|---|
'b' | signed char | int | 1 |
'B' | unsigned char | int | 1 |
'u' | wchar_t | Unicode character | 2 |
'h' | signed short | int | 2 |
'H' | unsigned short | int | 2 |
'i' | signed int | int | 2 |
'I' | unsigned int | int | 2 |
'l' | signed long | int | 4 |
'L' | unsigned long | int | 4 |
'q' | signed long long | int | 8 |
'Q' | unsigned long long | int | 8 |
'f' | float | float | 4 |
'd' | double | float | 8 |
Tying everything together, here is an example of how you would define an array in Python:
import array as arr
numbers = arr.array('i',[10,20,30])
print(numbers)
#output
#array('i', [10, 20, 30])
Let's break it down:
import array as arr
.numbers
array.arr.array()
because of import array as arr
.array()
constructor, we first included i
, for signed integer. Signed integer means that the array can include positive and negative values. Unsigned integer, with H
for example, would mean that no negative values are allowed.Keep in mind that if you tried to include values that were not of i
typecode, meaning they were not integer values, you would get an error:
import array as arr
numbers = arr.array('i',[10.0,20,30])
print(numbers)
#output
#Traceback (most recent call last):
# File "/Users/dionysialemonaki/python_articles/demo.py", line 14, in <module>
# numbers = arr.array('i',[10.0,20,30])
#TypeError: 'float' object cannot be interpreted as an integer
In the example above, I tried to include a floating point number in the array. I got an error because this is meant to be an integer array only.
Another way to create an array is the following:
from array import *
#an array of floating point values
numbers = array('d',[10.0,20.0,30.0])
print(numbers)
#output
#array('d', [10.0, 20.0, 30.0])
The example above imported the array module
via from array import *
and created an array numbers
of float data type. This means that it holds only floating point numbers, which is specified with the 'd'
typecode.
To find out the exact number of elements contained in an array, use the built-in len()
method.
It will return the integer number that is equal to the total number of elements in the array you specify.
import array as arr
numbers = arr.array('i',[10,20,30])
print(len(numbers))
#output
# 3
In the example above, the array contained three elements – 10, 20, 30
– so the length of numbers
is 3
.
Each item in an array has a specific address. Individual items are accessed by referencing their index number.
Indexing in Python, and in all programming languages and computing in general, starts at 0
. It is important to remember that counting starts at 0
and not at 1
.
To access an element, you first write the name of the array followed by square brackets. Inside the square brackets you include the item's index number.
The general syntax would look something like this:
array_name[index_value_of_item]
Here is how you would access each individual element in an array:
import array as arr
numbers = arr.array('i',[10,20,30])
print(numbers[0]) # gets the 1st element
print(numbers[1]) # gets the 2nd element
print(numbers[2]) # gets the 3rd element
#output
#10
#20
#30
Remember that the index value of the last element of an array is always one less than the length of the array. Where n
is the length of the array, n - 1
will be the index value of the last item.
Note that you can also access each individual element using negative indexing.
With negative indexing, the last element would have an index of -1
, the second to last element would have an index of -2
, and so on.
Here is how you would get each item in an array using that method:
import array as arr
numbers = arr.array('i',[10,20,30])
print(numbers[-1]) #gets last item
print(numbers[-2]) #gets second to last item
print(numbers[-3]) #gets first item
#output
#30
#20
#10
You can find out an element's index number by using the index()
method.
You pass the value of the element being searched as the argument to the method, and the element's index number is returned.
import array as arr
numbers = arr.array('i',[10,20,30])
#search for the index of the value 10
print(numbers.index(10))
#output
#0
If there is more than one element with the same value, the index of the first instance of the value will be returned:
import array as arr
numbers = arr.array('i',[10,20,30,10,20,30])
#search for the index of the value 10
#will return the index number of the first instance of the value 10
print(numbers.index(10))
#output
#0
You've seen how to access each individual element in an array and print it out on its own.
You've also seen how to print the array, using the print()
method. That method gives the following result:
import array as arr
numbers = arr.array('i',[10,20,30])
print(numbers)
#output
#array('i', [10, 20, 30])
What if you want to print each value one by one?
This is where a loop comes in handy. You can loop through the array and print out each value, one-by-one, with each loop iteration.
For this you can use a simple for
loop:
import array as arr
numbers = arr.array('i',[10,20,30])
for number in numbers:
print(number)
#output
#10
#20
#30
You could also use the range()
function, and pass the len()
method as its parameter. This would give the same result as above:
import array as arr
values = arr.array('i',[10,20,30])
#prints each individual value in the array
for value in range(len(values)):
print(values[value])
#output
#10
#20
#30
To access a specific range of values inside the array, use the slicing operator, which is a colon :
.
When using the slicing operator and you only include one value, the counting starts from 0
by default. It gets the first item, and goes up to but not including the index number you specify.
import array as arr
#original array
numbers = arr.array('i',[10,20,30])
#get the values 10 and 20 only
print(numbers[:2]) #first to second position
#output
#array('i', [10, 20])
When you pass two numbers as arguments, you specify a range of numbers. In this case, the counting starts at the position of the first number in the range, and up to but not including the second one:
import array as arr
#original array
numbers = arr.array('i',[10,20,30])
#get the values 20 and 30 only
print(numbers[1:3]) #second to third position
#output
#rray('i', [20, 30])
Arrays are mutable, which means they are changeable. You can change the value of the different items, add new ones, or remove any you don't want in your program anymore.
Let's see some of the most commonly used methods which are used for performing operations on arrays.
You can change the value of a specific element by speficying its position and assigning it a new value:
import array as arr
#original array
numbers = arr.array('i',[10,20,30])
#change the first element
#change it from having a value of 10 to having a value of 40
numbers[0] = 40
print(numbers)
#output
#array('i', [40, 20, 30])
To add one single value at the end of an array, use the append()
method:
import array as arr
#original array
numbers = arr.array('i',[10,20,30])
#add the integer 40 to the end of numbers
numbers.append(40)
print(numbers)
#output
#array('i', [10, 20, 30, 40])
Be aware that the new item you add needs to be the same data type as the rest of the items in the array.
Look what happens when I try to add a float to an array of integers:
import array as arr
#original array
numbers = arr.array('i',[10,20,30])
#add the integer 40 to the end of numbers
numbers.append(40.0)
print(numbers)
#output
#Traceback (most recent call last):
# File "/Users/dionysialemonaki/python_articles/demo.py", line 19, in <module>
# numbers.append(40.0)
#TypeError: 'float' object cannot be interpreted as an integer
But what if you want to add more than one value to the end an array?
Use the extend()
method, which takes an iterable (such as a list of items) as an argument. Again, make sure that the new items are all the same data type.
import array as arr
#original array
numbers = arr.array('i',[10,20,30])
#add the integers 40,50,60 to the end of numbers
#The numbers need to be enclosed in square brackets
numbers.extend([40,50,60])
print(numbers)
#output
#array('i', [10, 20, 30, 40, 50, 60])
And what if you don't want to add an item to the end of an array? Use the insert()
method, to add an item at a specific position.
The insert()
function takes two arguments: the index number of the position the new element will be inserted, and the value of the new element.
import array as arr
#original array
numbers = arr.array('i',[10,20,30])
#add the integer 40 in the first position
#remember indexing starts at 0
numbers.insert(0,40)
print(numbers)
#output
#array('i', [40, 10, 20, 30])
To remove an element from an array, use the remove()
method and include the value as an argument to the method.
import array as arr
#original array
numbers = arr.array('i',[10,20,30])
numbers.remove(10)
print(numbers)
#output
#array('i', [20, 30])
With remove()
, only the first instance of the value you pass as an argument will be removed.
See what happens when there are more than one identical values:
import array as arr
#original array
numbers = arr.array('i',[10,20,30,10,20])
numbers.remove(10)
print(numbers)
#output
#array('i', [20, 30, 10, 20])
Only the first occurence of 10
is removed.
You can also use the pop()
method, and specify the position of the element to be removed:
import array as arr
#original array
numbers = arr.array('i',[10,20,30,10,20])
#remove the first instance of 10
numbers.pop(0)
print(numbers)
#output
#array('i', [20, 30, 10, 20])
And there you have it - you now know the basics of how to create arrays in Python using the array module
. Hopefully you found this guide helpful.
Thanks for reading and happy coding!
#python #programming
1670560264
Learn how to use Python arrays. Create arrays in Python using the array module. You'll see how to define them and the different methods commonly used for performing operations on them.
The artcile covers arrays that you create by importing the array module
. We won't cover NumPy arrays here.
Let's get started!
Arrays are a fundamental data structure, and an important part of most programming languages. In Python, they are containers which are able to store more than one item at the same time.
Specifically, they are an ordered collection of elements with every value being of the same data type. That is the most important thing to remember about Python arrays - the fact that they can only hold a sequence of multiple items that are of the same type.
Lists are one of the most common data structures in Python, and a core part of the language.
Lists and arrays behave similarly.
Just like arrays, lists are an ordered sequence of elements.
They are also mutable and not fixed in size, which means they can grow and shrink throughout the life of the program. Items can be added and removed, making them very flexible to work with.
However, lists and arrays are not the same thing.
Lists store items that are of various data types. This means that a list can contain integers, floating point numbers, strings, or any other Python data type, at the same time. That is not the case with arrays.
As mentioned in the section above, arrays store only items that are of the same single data type. There are arrays that contain only integers, or only floating point numbers, or only any other Python data type you want to use.
Lists are built into the Python programming language, whereas arrays aren't. Arrays are not a built-in data structure, and therefore need to be imported via the array module
in order to be used.
Arrays of the array module
are a thin wrapper over C arrays, and are useful when you want to work with homogeneous data.
They are also more compact and take up less memory and space which makes them more size efficient compared to lists.
If you want to perform mathematical calculations, then you should use NumPy arrays by importing the NumPy package. Besides that, you should just use Python arrays when you really need to, as lists work in a similar way and are more flexible to work with.
In order to create Python arrays, you'll first have to import the array module
which contains all the necassary functions.
There are three ways you can import the array module
:
import array
at the top of the file. This includes the module array
. You would then go on to create an array using array.array()
.import array
#how you would create an array
array.array()
array.array()
all the time, you could use import array as arr
at the top of the file, instead of import array
alone. You would then create an array by typing arr.array()
. The arr
acts as an alias name, with the array constructor then immediately following it.import array as arr
#how you would create an array
arr.array()
from array import *
, with *
importing all the functionalities available. You would then create an array by writing the array()
constructor alone.from array import *
#how you would create an array
array()
Once you've imported the array module
, you can then go on to define a Python array.
The general syntax for creating an array looks like this:
variable_name = array(typecode,[elements])
Let's break it down:
variable_name
would be the name of the array.typecode
specifies what kind of elements would be stored in the array. Whether it would be an array of integers, an array of floats or an array of any other Python data type. Remember that all elements should be of the same data type.elements
that would be stored in the array, with each element being separated by a comma. You can also create an empty array by just writing variable_name = array(typecode)
alone, without any elements.Below is a typecode table, with the different typecodes that can be used with the different data types when defining Python arrays:
TYPECODE | C TYPE | PYTHON TYPE | SIZE |
---|---|---|---|
'b' | signed char | int | 1 |
'B' | unsigned char | int | 1 |
'u' | wchar_t | Unicode character | 2 |
'h' | signed short | int | 2 |
'H' | unsigned short | int | 2 |
'i' | signed int | int | 2 |
'I' | unsigned int | int | 2 |
'l' | signed long | int | 4 |
'L' | unsigned long | int | 4 |
'q' | signed long long | int | 8 |
'Q' | unsigned long long | int | 8 |
'f' | float | float | 4 |
'd' | double | float | 8 |
Tying everything together, here is an example of how you would define an array in Python:
import array as arr
numbers = arr.array('i',[10,20,30])
print(numbers)
#output
#array('i', [10, 20, 30])
Let's break it down:
import array as arr
.numbers
array.arr.array()
because of import array as arr
.array()
constructor, we first included i
, for signed integer. Signed integer means that the array can include positive and negative values. Unsigned integer, with H
for example, would mean that no negative values are allowed.Keep in mind that if you tried to include values that were not of i
typecode, meaning they were not integer values, you would get an error:
import array as arr
numbers = arr.array('i',[10.0,20,30])
print(numbers)
#output
#Traceback (most recent call last):
# File "/Users/dionysialemonaki/python_articles/demo.py", line 14, in <module>
# numbers = arr.array('i',[10.0,20,30])
#TypeError: 'float' object cannot be interpreted as an integer
In the example above, I tried to include a floating point number in the array. I got an error because this is meant to be an integer array only.
Another way to create an array is the following:
from array import *
#an array of floating point values
numbers = array('d',[10.0,20.0,30.0])
print(numbers)
#output
#array('d', [10.0, 20.0, 30.0])
The example above imported the array module
via from array import *
and created an array numbers
of float data type. This means that it holds only floating point numbers, which is specified with the 'd'
typecode.
To find out the exact number of elements contained in an array, use the built-in len()
method.
It will return the integer number that is equal to the total number of elements in the array you specify.
import array as arr
numbers = arr.array('i',[10,20,30])
print(len(numbers))
#output
# 3
In the example above, the array contained three elements – 10, 20, 30
– so the length of numbers
is 3
.
Each item in an array has a specific address. Individual items are accessed by referencing their index number.
Indexing in Python, and in all programming languages and computing in general, starts at 0
. It is important to remember that counting starts at 0
and not at 1
.
To access an element, you first write the name of the array followed by square brackets. Inside the square brackets you include the item's index number.
The general syntax would look something like this:
array_name[index_value_of_item]
Here is how you would access each individual element in an array:
import array as arr
numbers = arr.array('i',[10,20,30])
print(numbers[0]) # gets the 1st element
print(numbers[1]) # gets the 2nd element
print(numbers[2]) # gets the 3rd element
#output
#10
#20
#30
Remember that the index value of the last element of an array is always one less than the length of the array. Where n
is the length of the array, n - 1
will be the index value of the last item.
Note that you can also access each individual element using negative indexing.
With negative indexing, the last element would have an index of -1
, the second to last element would have an index of -2
, and so on.
Here is how you would get each item in an array using that method:
import array as arr
numbers = arr.array('i',[10,20,30])
print(numbers[-1]) #gets last item
print(numbers[-2]) #gets second to last item
print(numbers[-3]) #gets first item
#output
#30
#20
#10
You can find out an element's index number by using the index()
method.
You pass the value of the element being searched as the argument to the method, and the element's index number is returned.
import array as arr
numbers = arr.array('i',[10,20,30])
#search for the index of the value 10
print(numbers.index(10))
#output
#0
If there is more than one element with the same value, the index of the first instance of the value will be returned:
import array as arr
numbers = arr.array('i',[10,20,30,10,20,30])
#search for the index of the value 10
#will return the index number of the first instance of the value 10
print(numbers.index(10))
#output
#0
You've seen how to access each individual element in an array and print it out on its own.
You've also seen how to print the array, using the print()
method. That method gives the following result:
import array as arr
numbers = arr.array('i',[10,20,30])
print(numbers)
#output
#array('i', [10, 20, 30])
What if you want to print each value one by one?
This is where a loop comes in handy. You can loop through the array and print out each value, one-by-one, with each loop iteration.
For this you can use a simple for
loop:
import array as arr
numbers = arr.array('i',[10,20,30])
for number in numbers:
print(number)
#output
#10
#20
#30
You could also use the range()
function, and pass the len()
method as its parameter. This would give the same result as above:
import array as arr
values = arr.array('i',[10,20,30])
#prints each individual value in the array
for value in range(len(values)):
print(values[value])
#output
#10
#20
#30
To access a specific range of values inside the array, use the slicing operator, which is a colon :
.
When using the slicing operator and you only include one value, the counting starts from 0
by default. It gets the first item, and goes up to but not including the index number you specify.
import array as arr
#original array
numbers = arr.array('i',[10,20,30])
#get the values 10 and 20 only
print(numbers[:2]) #first to second position
#output
#array('i', [10, 20])
When you pass two numbers as arguments, you specify a range of numbers. In this case, the counting starts at the position of the first number in the range, and up to but not including the second one:
import array as arr
#original array
numbers = arr.array('i',[10,20,30])
#get the values 20 and 30 only
print(numbers[1:3]) #second to third position
#output
#rray('i', [20, 30])
Arrays are mutable, which means they are changeable. You can change the value of the different items, add new ones, or remove any you don't want in your program anymore.
Let's see some of the most commonly used methods which are used for performing operations on arrays.
You can change the value of a specific element by speficying its position and assigning it a new value:
import array as arr
#original array
numbers = arr.array('i',[10,20,30])
#change the first element
#change it from having a value of 10 to having a value of 40
numbers[0] = 40
print(numbers)
#output
#array('i', [40, 20, 30])
To add one single value at the end of an array, use the append()
method:
import array as arr
#original array
numbers = arr.array('i',[10,20,30])
#add the integer 40 to the end of numbers
numbers.append(40)
print(numbers)
#output
#array('i', [10, 20, 30, 40])
Be aware that the new item you add needs to be the same data type as the rest of the items in the array.
Look what happens when I try to add a float to an array of integers:
import array as arr
#original array
numbers = arr.array('i',[10,20,30])
#add the integer 40 to the end of numbers
numbers.append(40.0)
print(numbers)
#output
#Traceback (most recent call last):
# File "/Users/dionysialemonaki/python_articles/demo.py", line 19, in <module>
# numbers.append(40.0)
#TypeError: 'float' object cannot be interpreted as an integer
But what if you want to add more than one value to the end an array?
Use the extend()
method, which takes an iterable (such as a list of items) as an argument. Again, make sure that the new items are all the same data type.
import array as arr
#original array
numbers = arr.array('i',[10,20,30])
#add the integers 40,50,60 to the end of numbers
#The numbers need to be enclosed in square brackets
numbers.extend([40,50,60])
print(numbers)
#output
#array('i', [10, 20, 30, 40, 50, 60])
And what if you don't want to add an item to the end of an array? Use the insert()
method, to add an item at a specific position.
The insert()
function takes two arguments: the index number of the position the new element will be inserted, and the value of the new element.
import array as arr
#original array
numbers = arr.array('i',[10,20,30])
#add the integer 40 in the first position
#remember indexing starts at 0
numbers.insert(0,40)
print(numbers)
#output
#array('i', [40, 10, 20, 30])
To remove an element from an array, use the remove()
method and include the value as an argument to the method.
import array as arr
#original array
numbers = arr.array('i',[10,20,30])
numbers.remove(10)
print(numbers)
#output
#array('i', [20, 30])
With remove()
, only the first instance of the value you pass as an argument will be removed.
See what happens when there are more than one identical values:
import array as arr
#original array
numbers = arr.array('i',[10,20,30,10,20])
numbers.remove(10)
print(numbers)
#output
#array('i', [20, 30, 10, 20])
Only the first occurence of 10
is removed.
You can also use the pop()
method, and specify the position of the element to be removed:
import array as arr
#original array
numbers = arr.array('i',[10,20,30,10,20])
#remove the first instance of 10
numbers.pop(0)
print(numbers)
#output
#array('i', [20, 30, 10, 20])
And there you have it - you now know the basics of how to create arrays in Python using the array module
. Hopefully you found this guide helpful.
You'll start from the basics and learn in an interacitve and beginner-friendly way. You'll also build five projects at the end to put into practice and help reinforce what you learned.
Thanks for reading and happy coding!
Original article source at https://www.freecodecamp.org
#python