-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* docs: faq page * docs: bug report template
- Loading branch information
Showing
2 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
--- | ||
name: Bug report | ||
about: Create a report to help us improve | ||
title: "" | ||
labels: "" | ||
assignees: "" | ||
--- | ||
|
||
**Before you begin** | ||
|
||
- [ ] I have read the [FAQ](https://zog.dev/faq) and my issue was not answered there | ||
|
||
**What version of Zog are you using** | ||
|
||
**Describe the bug** | ||
A clear and concise description of what the bug is. | ||
|
||
**To Reproduce** | ||
A small, self-container, complete reproduction, uploaded to a Github repo, containing the minimum amount of files required to reproduce the behaviour, along with a list of commands that need to be run. Keep it simple. | ||
|
||
**Expected behavior** | ||
A clear and concise description of what you expected to happen. | ||
|
||
**Additional context** | ||
Add any other context about the problem here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
--- | ||
sidebar_position: 12 | ||
--- | ||
|
||
# FAQ | ||
|
||
## My code panics / zog doesn't work | ||
|
||
Please make sure that you are using Zog correctly. The expectation is that if Zog causes a panic you are doing something wrong. So before reporting an issue please check this common mistakes: | ||
|
||
**1 Not passing a pointer to the struct you are parsing into** | ||
|
||
```go | ||
payload := models.UserPayload{} | ||
errMap := models.UserSchema.Parse(zhttp.Request(c.Request), payload) // note that the "payload" is not a pointer correct code here is &payload | ||
|
||
``` | ||
|
||
**2 No defining your schema correctly** | ||
|
||
```go | ||
type Name struct { | ||
FirstName string `json:"first_name" zog:"first_name"` // zog struct tag is used to define the name of the field in the input data (i.e json key) not the name of the schema key (common mistake) | ||
LastName string `json:"last_name" zog:"last_name"` | ||
} | ||
|
||
data := new(Name) | ||
|
||
var schema = z.Struct(z.Schema{ | ||
"first_name": z.String().Required(z.Message("First name is required")), // here you are telling zog that your struct should have a First_name field, but this is incorrect because the struct has a FirstName field. The key here should be "firstName" or "FirstName" (both are valid) | ||
"last_name": z.String().Required(z.Message("Last name is required")), // same issue here | ||
}) | ||
``` | ||
|
||
This comes from a misunderstanding of how the zog struct tag works. The zog struct tag is used to define the name of the field in the input data (i.e json key) not the name of the schema key. The schema is only aware of what the struct looks like it is not aware of the source of the input data. | ||
|
||
## Error: "Struct is missing expected schema key: \{some_key\}" | ||
|
||
This error is telling you that you are defining your schema keys incorrectly. For example: | ||
|
||
```go | ||
type Name struct { | ||
FirstName string `json:"first_name" zog:"first_name"` // zog struct tag is used to define the name of the field in the input data (i.e json key) not the name of the schema key (common mistake) | ||
LastName string `json:"last_name" zog:"last_name"` | ||
} | ||
|
||
data := new(Name) | ||
|
||
var schema = z.Struct(z.Schema{ | ||
"first_name": z.String().Required(z.Message("First name is required")), // here you are telling zog that your struct should have a First_name field, but this is incorrect because the struct has a FirstName field. The key here should be "firstName" or "FirstName" (both are valid) | ||
"last_name": z.String().Required(z.Message("Last name is required")), // same issue here | ||
}) | ||
``` | ||
|
||
This comes from a misunderstanding of how the zog struct tag works. The zog struct tag is used to define the name of the field in the input data (i.e json key) not the name of the schema key. The schema is only aware of what the struct looks like it is not aware of the source of the input data. |