Skip to content

solrac97gr/validator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Validator

I create this packages for use it as part of my future template with multi packages separeted of the principal template.

Its a simple validator for use in your projects who contains two principal interfaces and structs who implements it.

Interfaces

Validator

type Validator interface {
    Struct(s EvaluableStruct) error
}

ValidatorFunc

type ValidatorFunc func(args ...interface{}) error

EvaluableStruct

type EvaluableStruct interface {
    Validate(args ...interface{}) error
}

Example

package main

import (
    "fmt"
    "github.com/solrac97gr/validator"
    "github.com/solrac97gr/validations"
)

type User struct {
    Name string 
    Age  int    
}

func (u *User) Validate(args ...interface{}) error {
    err := validations.StringRequired(u)
    if err != nil {
        return err
    }

   err = validations.IntGratherThan(u.Age,18)
    if err != nil {
        return err
    }

    return nil
}


func main() {
    user := &User{
        Name: "Solrac",
        Age:  17,
    }

    val := validator.New()

    err := val.Struct(user)
    if err != nil {
        fmt.Println(err)
    }
}

Validations

  • Format
  • String
  • Slice
  • Float
  • Network
  • Int

Idea

You must create a struct who implements the EvaluableStruct interface and add the Validate method who contains the logic for validate the struct.

The validator must be part of your dependencies in your depencency injection container.

Considerations

This package is in development and is not ready for production.

License

MIT

About

Work in progress 🚧

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages