I'm making a JSON validation function and I'm trying to implement it. However I'm having issues with my structs when I try to import them as a parameter to my validation function, which is in another package.
The reason to why it's in another package is because a universal validation function that I will call in different route files, so I can't actually have any structs inside that package, they must be imported and defined in the route file.
type UsersJSON struct {
Users struct {
Put []UserJSON `json:"PUT"`
} `json:"users"`
}
type UserJSON struct {
display_name string `json:"display_name"`
username string `json:"username"`
email string `json:"email"`
password string `json:"password"`
}
func MyFunc(w http.ResponseWriter, r *http.Request) {
errors, _ := schema.Validate(data, r, UsersJSON)
}
func Validate(schemaHelper interface{}) (interface{}, error) {
file, err := os.Open("config/schema/schema.json")
if err != nil {
return nil, err
}
defer file.Close()
byteValue, _ := ioutil.ReadAll(file)
var helpers schemaHelper // this is the error
json.Unmarshal(byteValue, &helpers)
fmt.Printf("%v", helpers)
}
My JSON schema looks like this:
{
"users": {
"PUT": {
}
}
}
I want to make this method work because it makes automating validation much easier and faster.
This doesn't compile and gives the error schemaHelper is not a type
Any idea how I can solve this?