sentinal

package module
v2.0.10 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 22, 2019 License: MIT Imports: 7 Imported by: 3

README

Go Report Card GoDoc GoCover

Sentinal

Data Validation Library In Go

Basic Example

type user struct {
    age int
}

func main() {

    schema := map[string]map[string]string{
        "age" : map[string]string{
            "min" : "14",
            "max" : "100",
        },
    }

    data := user{15}
    valid, msg, err := sentinal.Validate(user, schema)
    // valid will be true

    data := user{10}
    valid, msg, err = sentinal.Validate(user, schema)
    // valid will be false
    // msg = {"age" : ["min is 14"]}

    data := user{101}
    valid, msg, err = sentinal.Validate(user, schema)
    // valid will be false
    // msg = {"age" : ["max is 100"]}
}

Using YAML

schema.yaml

age:
  max: "100" // make sure every value is string
  min: "14"

main.go

func main() {
    data := user{101}
    valid, msg, err = sentinal.ValidateWithYAML(
        user, "schema.yaml", []string{"/"})
    // provide file name and as many file paths where the file could be
    // valid will be false
    // msg = {"age" : ["max is 100"]}
}

Custom Validation Functions

type user struct {
    name string
}

func isName(value reflect.Value, validationData string) (bool, string, error) {
    // value is the reflect.Value of the field.
    // validationData is the data provided in struct tag, which in this case is true.
    
    //SOME LOGIC HERE

    /*
    first thing to return is if the data is valid.
    second thing to return is a string message when data is invalid.
    This helps you to see what was invalid.
    third thing to return is an error if any.
    */

    return true, "", nil
}


func main() {
    data := user{"Name"}

    schema := map[string]map[string]string{
        "name" : map[string]string{
            "isName" : "true", // isName is custom function
        },
    }


    sentinal.Validate(data, schema, map[string]func(reflect.Value, string) (bool, string, error){
        "isName" : isName,
    })
}


Functions List

max

This will check if value in struct is <= than value provided in struct tag.

schema := map[string]map[string]string{
    "fieldName" : map[string]string{
        "max" : "8",
    },
}
maxExclusive

This will check if value in struct is < than value provided in struct tag.

schema := map[string]map[string]string{
    "fieldName" : map[string]string{
        "maxExclusive" : "8",
    },
}
min

This will check if value in struct is >= than value provided in struct tag.

schema := map[string]map[string]string{
    "fieldName" : map[string]string{
        "min" : "8",
    },
}
minExclusive

This will check if value in struct is > than value provided in struct tag, which in this case is 15.5. This would also work with floats.

schema := map[string]map[string]string{
    "fieldName" : map[string]string{
        "minExclusive" : "8",
    },
}
From

Checks if a value is in a list. Note - there should not be a space between commas.

schema := map[string]map[string]string{
    "fieldName" : map[string]string{
        "from" : "value1,value2,value3",
    },
}
notFrom

Checks if a value is not in a list. Note - there should not be a space between commas.

schema := map[string]map[string]string{
    "fieldName" : map[string]string{
        "notFrom" : "value1,value2,value3",
    },
}
notEmpty

Checks if a field is not empty. Works with any data type.

schema := map[string]map[string]string{
    "fieldName" : map[string]string{
        "notEmpty" : "true",
    },
}
maxLength

Checks if length of data is less than max. Works with any data type.

schema := map[string]map[string]string{
    "fieldName" : map[string]string{
        "maxLen" : "8",
    },
}
minLength

Checks if length of data is greater than min. Works with any data type.

schema := map[string]map[string]string{
    "fieldName" : map[string]string{
        "minLen" : "8",
    },
}
contains

Checks if a string contains certain items. Seperated by comma

schema := map[string]map[string]string{
    "fieldName" : map[string]string{
        "contains" : "test,test2",
    },
}
notContains

Checks if a string does not contain certain items. Seperated by comma

schema := map[string]map[string]string{
    "fieldName" : map[string]string{
        "notContains" : " ,test2",
    },
}
isEmail

Checks if field is an email.

schema := map[string]map[string]string{
    "fieldName" : map[string]string{
        "isEmail" : "true",
    },
}
hasUpperCase

Checks if field has upper case characters.

schema := map[string]map[string]string{
    "fieldName" : map[string]string{
        "hasUpperCase" : "true",
    },
}
notHasUpperCase

Checks if field does not have upper case characters.

schema := map[string]map[string]string{
    "fieldName" : map[string]string{
        "notHasUpperCase" : "true",
    },
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidType = errors.New("Invalid Data Type")

ErrInvalidType occurs when data is of invalid type

Functions

func Validate

func Validate(
	object interface{},
	schema map[string]map[string]string,
	customFunctionsArg ...map[string]func(reflect.Value, string) (bool, string, error)) (
	bool,
	map[string][]string,
	error)

Validate is used to validate an object with a golang defined schema

func ValidateFields added in v2.0.10

func ValidateFields(
	object interface{},
	schema map[string]map[string]string,
	customFunctionsArg ...map[string]func(reflect.Value, string) (bool, string, error)) (
	bool,
	map[string][]string,
	error)

ValidateFields is used to only validate non nill fields in an object

func ValidateFieldsWithYAML added in v2.0.10

func ValidateFieldsWithYAML(
	object interface{},
	schemaFile string,
	schemaPaths []string,
	customFunctionsArg ...map[string]func(reflect.Value, string) (bool, string, error)) (
	bool,
	map[string][]string,
	error)

ValidateFieldsWithYAML is used to validate non nill fields in an object with yaml schema

func ValidateWithYAML added in v2.0.8

func ValidateWithYAML(
	object interface{},
	schemaFile string,
	schemaPaths []string,
	customFunctionsArg ...map[string]func(reflect.Value, string) (bool, string, error)) (
	bool,
	map[string][]string,
	error)

ValidateWithYAML is used to validate an object with yaml schema

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL