Skip to content

constraint.StringValidEmail

marrow16 edited this page Jan 21, 2023 · 6 revisions

Prev Home Next

Constraint: StringValidEmail

Description

Check that a string contains a valid email address (does not verify the email address!)

Note: By default, this constraint is non-strict - if the value being checked is not a string, this constraint does not fail (unless the Strict field is set)

V8n Tag Abbreviation

stremail

Fields

Field Type Description
DisallowRFC5322 bool when set, disallows email addresses in RFC5322 format (i.e "Barry Gibbs bg@example.com")
AllowIPAddress bool when set, allows email addresses with IP (e.g. "me@[123.0.1.2]")
AllowIPV6 bool when set, allows email addresses with IP v6 (e.g. "me@[2001:db8::68]")
AllowLocal bool when set, allows email addresses with 'local' (e.g. "me@localhost", "me@local", "me@localdomain", "me@[127.0.0.1]", "me@[::1]")
AllowTldOnly bool when set, allows email addresses with only Tld specified (e.g. "me@audi")
AllowGeographicTlds bool when set, allows email addresses with geographic Tlds (e.g. "me@some-company.africa")
AllowGenericTlds bool when set, allows email addresses with generic Tlds (e.g. "me@some.academy")
AllowBrandTlds bool when set, allows email addresses with brand Tlds (e.g. "me@my.audi")
AllowInfraTlds bool when set, allows email addresses with infrastructure Tlds (e.g. "me@arpa")
AllowTestTlds bool when set, allows email addresses with test Tlds and test domains (e.g. "me@example.com", "me@test.com")
AddCountryCodeTlds []string is an optional slice of additional country (and geographic) Tlds to allow
ExcCountryCodeTlds []string is an optional slice of country (and geographic) Tlds to disallow
AddGenericTlds []string is an optional slice of additional generic Tlds to allow (only checked if AllowGenericTlds is also set)
ExcGenericTlds []string is an optional slice of generic Tlds to disallow (only relevant if AllowGenericTlds is also set)
AddBrandTlds []string is an optional slice of additional brand Tlds to allow (only checked if AllowBrandTlds is also set)
ExcBrandTlds []string is an optional slice of brand Tlds to disallow (only relevant if AllowBrandTlds is also set)
AddLocalTlds []string is an optional slice of additional local Tlds to allow (only checked if AllowLocal is also set)
ExcLocalTlds []string is an optional slice of local Tlds to disallow (only relevant if AllowLocal is also set)
Message string the violation message to be used if the constraint fails. If empty, the default message is used
Stop bool when set to true, Stop prevents further validation checks on the property if this constraint fails
Strict bool when set to true, fails if the value being checked is not a correct type

Examples

Programmatic example...
package main

import (
    "fmt"

    "github.com/marrow16/valix"
)

func main() {
    validator := &valix.Validator{
        UseNumber: true,
        Properties: valix.Properties{
            "email": {
                Type: valix.JsonString,
                Constraints: valix.Constraints{
                    &valix.StringValidEmail{},
                },
            },
        },
    }

    ok, violations, _ := validator.ValidateString(`{"email": "me@example.com"}`)
    fmt.Printf("Passed? %v\n", ok)
    for i, v := range violations {
        fmt.Printf("Violation[%d] Message: %s, Property: %s, Path: %s\n", i+1, v.Message, v.Property, v.Path)
    }

    ok, violations, _ = validator.ValidateString(`{"email": "me@xyz"}`)
    fmt.Printf("Passed? %v\n", ok)
    for i, v := range violations {
        fmt.Printf("Violation[%d] Message: %s, Property: %s, Path: %s\n", i+1, v.Message, v.Property, v.Path)
    }

    ok, violations, _ = validator.ValidateString(`{"email": "you@google.com"}`)
    fmt.Printf("Passed? %v\n", ok)
    for i, v := range violations {
        fmt.Printf("Violation[%d] Message: %s, Property: %s, Path: %s\n", i+1, v.Message, v.Property, v.Path)
    }
}

try on go-playground

Struct v8n tag example...
package main

import (
    "fmt"

    "github.com/marrow16/valix"
)

type MyStruct struct {
    Email string `json:"email" v8n:"&stremail"`
}

var validator = valix.MustCompileValidatorFor(MyStruct{})

func main() {
    my := &MyStruct{}

    ok, violations, _ := validator.ValidateStringInto(`{"email": "me@example.com"}`, my)
    fmt.Printf("Passed? %v\n", ok)
    for i, v := range violations {
        fmt.Printf("Violation[%d] Message: %s, Property: %s, Path: %s\n", i+1, v.Message, v.Property, v.Path)
    }

    ok, violations, _ = validator.ValidateStringInto(`{"email": "me@xyz"}`, my)
    fmt.Printf("Passed? %v\n", ok)
    for i, v := range violations {
        fmt.Printf("Violation[%d] Message: %s, Property: %s, Path: %s\n", i+1, v.Message, v.Property, v.Path)
    }

    ok, violations, _ = validator.ValidateStringInto(`{"email": "you@google.com"}`, my)
    fmt.Printf("Passed? %v\n", ok)
    for i, v := range violations {
        fmt.Printf("Violation[%d] Message: %s, Property: %s, Path: %s\n", i+1, v.Message, v.Property, v.Path)
    }
}

try on go-playground

using conditionals to optionally allow test email addresses...

package main

import (
    "fmt"

    "github.com/marrow16/valix"
)

type MyStruct struct {
    Email string `json:"email" v8n:"&set{constraints:[&[!TESTING]stremail,&[TESTING]stremail{AllowTestTlds}],one}"`
}

var validator = valix.MustCompileValidatorFor(MyStruct{})

func main() {
    my := &MyStruct{}

    ok, violations, _ := validator.ValidateStringInto(`{"email": "me@example.com"}`, my)
    fmt.Printf("Passed? %v\n", ok)
    for i, v := range violations {
        fmt.Printf("Violation[%d] Message: %s, Property: %s, Path: %s\n", i+1, v.Message, v.Property, v.Path)
    }

    ok, violations, _ = validator.ValidateStringInto(`{"email": "you@google.com"}`, my)
    fmt.Printf("Passed? %v\n", ok)
    for i, v := range violations {
        fmt.Printf("Violation[%d] Message: %s, Property: %s, Path: %s\n", i+1, v.Message, v.Property, v.Path)
    }

    ok, violations, _ = validator.ValidateStringInto(`{"email": "me@example.com"}`, my, "TESTING")
    fmt.Printf("Passed? %v\n", ok)
    for i, v := range violations {
        fmt.Printf("Violation[%d] Message: %s, Property: %s, Path: %s\n", i+1, v.Message, v.Property, v.Path)
    }

    ok, violations, _ = validator.ValidateStringInto(`{"email": "you@google.com"}`, my, "TESTING")
    fmt.Printf("Passed? %v\n", ok)
    for i, v := range violations {
        fmt.Printf("Violation[%d] Message: %s, Property: %s, Path: %s\n", i+1, v.Message, v.Property, v.Path)
    }
}

try on go-playground

Clone this wiki locally