sentinal

package module
v2.0.2+incompatible Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2019 License: MIT Imports: 5 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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"]}
}

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",
    },
}

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]functionType) (
	bool,
	map[string][]string,
	error)

Validate is used to validate an object

Types

This section is empty.

Jump to

Keyboard shortcuts

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