Skip to content

Commit

Permalink
docs: faq and issue template (#59)
Browse files Browse the repository at this point in the history
* docs: faq page

* docs: bug report template
  • Loading branch information
Oudwins authored Jan 2, 2025
1 parent 9698f31 commit 914b838
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
25 changes: 25 additions & 0 deletions .github/ISSUE_TEMPLATE/bug_report.md
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.
55 changes: 55 additions & 0 deletions docs/docs/faq.md
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.

0 comments on commit 914b838

Please sign in to comment.