GithubHelp home page GithubHelp logo

kat-co / vala Goto Github PK

View Code? Open in Web Editor NEW
116.0 7.0 11.0 12 KB

A simple, extensible, library to make argument validation in Go palatable.

License: MIT License

Go 100.00%
go parameters arguments validation

vala's Introduction

vala GoDoc

A simple, extensible, library to make argument validation in Go palatable.

Instead of this:

func BoringValidation(a, b, c, d, e, f, g MyType) {
  if (a == nil)
    panic("a is nil")
  if (b == nil)
    panic("b is nil")
  if (c == nil)
    panic("c is nil")
  if (d == nil)
    panic("d is nil")
  if (e == nil)
    panic("e is nil")
  if (f == nil)
    panic("f is nil")
  if (g == nil)
    panic("g is nil")
}

Do this:

func ClearValidation(a, b, c, d, e, f, g MyType) {
  BeginValidation().Validate(
    IsNotNil(a, "a"),
    IsNotNil(b, "b"),
    IsNotNil(c, "c"),
    IsNotNil(d, "d"),
    IsNotNil(e, "e"),
    IsNotNil(f, "f"),
    IsNotNil(g, "g"),
  ).CheckAndPanic() // All values will get checked before an error is thrown!
}

Instead of this:

func BoringValidation(a, b, c, d, e, f, g MyType) error {
  if (a == nil)
    return fmt.Errorf("a is nil")
  if (b == nil)
    return fmt.Errorf("b is nil")
  if (c == nil)
    return fmt.Errorf("c is nil")
  if (d == nil)
    return fmt.Errorf("d is nil")
  if (e == nil)
    return fmt.Errorf("e is nil")
  if (f == nil)
    return fmt.Errorf("f is nil")
  if (g == nil)
    return fmt.Errorf("g is nil")
}

Do this:

func ClearValidation(a, b, c, d, e, f, g MyType) (err error) {
  defer func() { recover() }
  BeginValidation().Validate(
    IsNotNil(a, "a"),
    IsNotNil(b, "b"),
    IsNotNil(c, "c"),
    IsNotNil(d, "d"),
    IsNotNil(e, "e"),
    IsNotNil(f, "f"),
    IsNotNil(g, "g"),
  ).CheckSetErrorAndPanic(&err) // Return error will get set, and the function will return.

  // ...

  VeryExpensiveFunction(c, d)
}

Tier your validation:

func ClearValidation(a, b, c MyType) (err error) {
  err = BeginValidation().Validate(
    IsNotNil(a, "a"),
    IsNotNil(b, "b"),
    IsNotNil(c, "c"),
  ).CheckAndPanic().Validate( // Panic will occur here if a, b, or c are nil.
    HasLen(a.Items, 50, "a.Items"),
    GreaterThan(b.UserCount, 0, "b.UserCount"),
    Equals(c.Name, "Vala", "c.name"),
    Not(Equals(c.FriendlyName, "Foo", "c.FriendlyName")),
  ).Check()

  if err != nil {
  	return err
  }

  // ...

  VeryExpensiveFunction(c, d)
}

Extend with your own validators for readability. Note that an error should always be returned so that the Not function can return a message if it passes. Unlike idiomatic Go, use the boolean to check for success.

func ReportFitsRepository(report *Report, repository *Repository) Checker {
	return func() (passes bool, err error) {

		err = fmt.Errof("A %s report does not belong in a %s repository.", report.Type, repository.Type)
		passes = (repository.Type == report.Type)
		return passes, err
	}
}

func AuthorCanUpload(authorName string, repository *Repository) Checker {
	return func() (passes bool, err error) {
        err = fmt.Errof("%s does not have access to this repository.", authorName)
		passes = !repository.AuthorCanUpload(authorName)
		return passes, err
	}
}

func AuthorIsCollaborator(authorName string, report *Report) Checker {
	return func() (passes bool, err error) {

        err = fmt.Errorf("The given author was not one of the collaborators for this report.")
		for _, collaboratorName := range report.Collaborators() {
			if collaboratorName == authorName {
				passes = true
				break
			}
		}

        return passes, err
	}
}

func HandleReport(authorName string, report *Report, repository *Repository) {

	BeginValidation().Validate(
    	AuthorIsCollaborator(authorName, report),
		AuthorCanUpload(authorName, repository),
		ReportFitsRepository(report, repository),
	).CheckAndPanic()
}

vala's People

Contributors

aarondl avatar elithrar avatar kat-co avatar nullbio avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

vala's Issues

Possible "blank error" being generated

I think that this line is generating a blank error as the first error is appended. It seems unintentional?
https://github.com/kat-co/vala/blob/master/validation.go#L74

It could probably be:
return &Validation{make([]string, 0, numErrors)}

That way the memory is allocated for a single element, but no element is actually added. In fact, instead of validationFactory() you could simply use: &Validation{} in it's place, as even if the slice is nil, append will work just fine and create a new slice.

Thanks for the library

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.