How to validate Go object data like Javascript's Joi? [closed]
I use Joi to do object schema and data validation in JavaScript. In Golang, I know how to do schema validation, but how do I do data validation?
Is there a Joi equivalient for Golang? I am aware of Godog, but it hasn't reached 1.0 yet.
go
closed as off-topic by Flimzy, icza, ThunderCat, Peter, Volker Dec 29 '18 at 15:03
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it." – Flimzy, icza, ThunderCat, Peter, Volker
If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
I use Joi to do object schema and data validation in JavaScript. In Golang, I know how to do schema validation, but how do I do data validation?
Is there a Joi equivalient for Golang? I am aware of Godog, but it hasn't reached 1.0 yet.
go
closed as off-topic by Flimzy, icza, ThunderCat, Peter, Volker Dec 29 '18 at 15:03
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it." – Flimzy, icza, ThunderCat, Peter, Volker
If this question can be reworded to fit the rules in the help center, please edit the question.
It depends on, what you want... What is the context of your validation? Maybe a framework like Gobuffalo could help you. Do you valdiate, before you write the data into a db? Or do you want to check a dump?
– apxp
Dec 29 '18 at 13:33
Look forgovalidatorpackage.
– zoonman
Dec 29 '18 at 18:04
@apxp I want to validate the data that I received before writing in to a db or send it to some other component. What do you suggest?
– iam thadiyan
Dec 30 '18 at 9:17
add a comment |
I use Joi to do object schema and data validation in JavaScript. In Golang, I know how to do schema validation, but how do I do data validation?
Is there a Joi equivalient for Golang? I am aware of Godog, but it hasn't reached 1.0 yet.
go
I use Joi to do object schema and data validation in JavaScript. In Golang, I know how to do schema validation, but how do I do data validation?
Is there a Joi equivalient for Golang? I am aware of Godog, but it hasn't reached 1.0 yet.
go
go
asked Dec 29 '18 at 13:03
iam thadiyaniam thadiyan
558
558
closed as off-topic by Flimzy, icza, ThunderCat, Peter, Volker Dec 29 '18 at 15:03
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it." – Flimzy, icza, ThunderCat, Peter, Volker
If this question can be reworded to fit the rules in the help center, please edit the question.
closed as off-topic by Flimzy, icza, ThunderCat, Peter, Volker Dec 29 '18 at 15:03
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it." – Flimzy, icza, ThunderCat, Peter, Volker
If this question can be reworded to fit the rules in the help center, please edit the question.
It depends on, what you want... What is the context of your validation? Maybe a framework like Gobuffalo could help you. Do you valdiate, before you write the data into a db? Or do you want to check a dump?
– apxp
Dec 29 '18 at 13:33
Look forgovalidatorpackage.
– zoonman
Dec 29 '18 at 18:04
@apxp I want to validate the data that I received before writing in to a db or send it to some other component. What do you suggest?
– iam thadiyan
Dec 30 '18 at 9:17
add a comment |
It depends on, what you want... What is the context of your validation? Maybe a framework like Gobuffalo could help you. Do you valdiate, before you write the data into a db? Or do you want to check a dump?
– apxp
Dec 29 '18 at 13:33
Look forgovalidatorpackage.
– zoonman
Dec 29 '18 at 18:04
@apxp I want to validate the data that I received before writing in to a db or send it to some other component. What do you suggest?
– iam thadiyan
Dec 30 '18 at 9:17
It depends on, what you want... What is the context of your validation? Maybe a framework like Gobuffalo could help you. Do you valdiate, before you write the data into a db? Or do you want to check a dump?
– apxp
Dec 29 '18 at 13:33
It depends on, what you want... What is the context of your validation? Maybe a framework like Gobuffalo could help you. Do you valdiate, before you write the data into a db? Or do you want to check a dump?
– apxp
Dec 29 '18 at 13:33
Look for
govalidator package.– zoonman
Dec 29 '18 at 18:04
Look for
govalidator package.– zoonman
Dec 29 '18 at 18:04
@apxp I want to validate the data that I received before writing in to a db or send it to some other component. What do you suggest?
– iam thadiyan
Dec 30 '18 at 9:17
@apxp I want to validate the data that I received before writing in to a db or send it to some other component. What do you suggest?
– iam thadiyan
Dec 30 '18 at 9:17
add a comment |
1 Answer
1
active
oldest
votes
Try ozzo-validation https://github.com/go-ozzo/ozzo-validation
package main
import (
"fmt"
"regexp"
"github.com/go-ozzo/ozzo-validation"
"github.com/go-ozzo/ozzo-validation/is"
)
type Address struct {
Street string
City string
State string
Zip string
}
func (a Address) Validate() error {
return validation.ValidateStruct(&a,
// Street cannot be empty, and the length must between 5 and 50
validation.Field(&a.Street, validation.Required, validation.Length(5, 50)),
// City cannot be empty, and the length must between 5 and 50
validation.Field(&a.City, validation.Required, validation.Length(5, 50)),
// State cannot be empty, and must be a string consisting of two letters in upper case
validation.Field(&a.State, validation.Required, validation.Match(regexp.MustCompile("^[A-Z]{2}$"))),
// State cannot be empty, and must be a string consisting of five digits
validation.Field(&a.Zip, validation.Required, validation.Match(regexp.MustCompile("^[0-9]{5}$"))),
)
}
func main() {
a := Address{
Street: "123",
City: "Unknown",
State: "Virginia",
Zip: "12345",
}
err := a.Validate()
fmt.Println(err)
// Output:
// Street: the length must be between 5 and 50; State: must be in a valid format.
}
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try ozzo-validation https://github.com/go-ozzo/ozzo-validation
package main
import (
"fmt"
"regexp"
"github.com/go-ozzo/ozzo-validation"
"github.com/go-ozzo/ozzo-validation/is"
)
type Address struct {
Street string
City string
State string
Zip string
}
func (a Address) Validate() error {
return validation.ValidateStruct(&a,
// Street cannot be empty, and the length must between 5 and 50
validation.Field(&a.Street, validation.Required, validation.Length(5, 50)),
// City cannot be empty, and the length must between 5 and 50
validation.Field(&a.City, validation.Required, validation.Length(5, 50)),
// State cannot be empty, and must be a string consisting of two letters in upper case
validation.Field(&a.State, validation.Required, validation.Match(regexp.MustCompile("^[A-Z]{2}$"))),
// State cannot be empty, and must be a string consisting of five digits
validation.Field(&a.Zip, validation.Required, validation.Match(regexp.MustCompile("^[0-9]{5}$"))),
)
}
func main() {
a := Address{
Street: "123",
City: "Unknown",
State: "Virginia",
Zip: "12345",
}
err := a.Validate()
fmt.Println(err)
// Output:
// Street: the length must be between 5 and 50; State: must be in a valid format.
}
add a comment |
Try ozzo-validation https://github.com/go-ozzo/ozzo-validation
package main
import (
"fmt"
"regexp"
"github.com/go-ozzo/ozzo-validation"
"github.com/go-ozzo/ozzo-validation/is"
)
type Address struct {
Street string
City string
State string
Zip string
}
func (a Address) Validate() error {
return validation.ValidateStruct(&a,
// Street cannot be empty, and the length must between 5 and 50
validation.Field(&a.Street, validation.Required, validation.Length(5, 50)),
// City cannot be empty, and the length must between 5 and 50
validation.Field(&a.City, validation.Required, validation.Length(5, 50)),
// State cannot be empty, and must be a string consisting of two letters in upper case
validation.Field(&a.State, validation.Required, validation.Match(regexp.MustCompile("^[A-Z]{2}$"))),
// State cannot be empty, and must be a string consisting of five digits
validation.Field(&a.Zip, validation.Required, validation.Match(regexp.MustCompile("^[0-9]{5}$"))),
)
}
func main() {
a := Address{
Street: "123",
City: "Unknown",
State: "Virginia",
Zip: "12345",
}
err := a.Validate()
fmt.Println(err)
// Output:
// Street: the length must be between 5 and 50; State: must be in a valid format.
}
add a comment |
Try ozzo-validation https://github.com/go-ozzo/ozzo-validation
package main
import (
"fmt"
"regexp"
"github.com/go-ozzo/ozzo-validation"
"github.com/go-ozzo/ozzo-validation/is"
)
type Address struct {
Street string
City string
State string
Zip string
}
func (a Address) Validate() error {
return validation.ValidateStruct(&a,
// Street cannot be empty, and the length must between 5 and 50
validation.Field(&a.Street, validation.Required, validation.Length(5, 50)),
// City cannot be empty, and the length must between 5 and 50
validation.Field(&a.City, validation.Required, validation.Length(5, 50)),
// State cannot be empty, and must be a string consisting of two letters in upper case
validation.Field(&a.State, validation.Required, validation.Match(regexp.MustCompile("^[A-Z]{2}$"))),
// State cannot be empty, and must be a string consisting of five digits
validation.Field(&a.Zip, validation.Required, validation.Match(regexp.MustCompile("^[0-9]{5}$"))),
)
}
func main() {
a := Address{
Street: "123",
City: "Unknown",
State: "Virginia",
Zip: "12345",
}
err := a.Validate()
fmt.Println(err)
// Output:
// Street: the length must be between 5 and 50; State: must be in a valid format.
}
Try ozzo-validation https://github.com/go-ozzo/ozzo-validation
package main
import (
"fmt"
"regexp"
"github.com/go-ozzo/ozzo-validation"
"github.com/go-ozzo/ozzo-validation/is"
)
type Address struct {
Street string
City string
State string
Zip string
}
func (a Address) Validate() error {
return validation.ValidateStruct(&a,
// Street cannot be empty, and the length must between 5 and 50
validation.Field(&a.Street, validation.Required, validation.Length(5, 50)),
// City cannot be empty, and the length must between 5 and 50
validation.Field(&a.City, validation.Required, validation.Length(5, 50)),
// State cannot be empty, and must be a string consisting of two letters in upper case
validation.Field(&a.State, validation.Required, validation.Match(regexp.MustCompile("^[A-Z]{2}$"))),
// State cannot be empty, and must be a string consisting of five digits
validation.Field(&a.Zip, validation.Required, validation.Match(regexp.MustCompile("^[0-9]{5}$"))),
)
}
func main() {
a := Address{
Street: "123",
City: "Unknown",
State: "Virginia",
Zip: "12345",
}
err := a.Validate()
fmt.Println(err)
// Output:
// Street: the length must be between 5 and 50; State: must be in a valid format.
}
answered Dec 29 '18 at 14:30
Maxim ShubinMaxim Shubin
1,20410
1,20410
add a comment |
add a comment |
It depends on, what you want... What is the context of your validation? Maybe a framework like Gobuffalo could help you. Do you valdiate, before you write the data into a db? Or do you want to check a dump?
– apxp
Dec 29 '18 at 13:33
Look for
govalidatorpackage.– zoonman
Dec 29 '18 at 18:04
@apxp I want to validate the data that I received before writing in to a db or send it to some other component. What do you suggest?
– iam thadiyan
Dec 30 '18 at 9:17