Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Json fileds type validation #511

Open
egor-ponomarev opened this issue May 23, 2024 · 0 comments
Open

Json fileds type validation #511

egor-ponomarev opened this issue May 23, 2024 · 0 comments

Comments

@egor-ponomarev
Copy link

Hello all !

I would like to know if there is any way or any plan to have the ability of validation values/types of json fields when parsing?
Let's say we have a struct with time.Time field:

type Country struct {
	Id        int
	CreatedAt time.Time
}

and try to parse a json where CreatedAt is an integer:

func TestWrongFieldType(t *testing.T) {
	// Arrange
	actualCity := &Country{}
	bytes := []byte("{\"id\":5, \"createdAt\": 2}")

	// Act
	// json = "github.com/goccy/go-json"
	err := json.Unmarshal(bytes, actualCity)

	// Assert
	assert.NotNil(t, err)
	assert.Equal(t, "Time.UnmarshalJSON: input is not a JSON string", err.Error())
}

Would it be possible to get in the error more detailed info, something like:

errors: [
   { 
       "createdAt": "wrong value"
   }
]

In case of several nested fields (including array fields) and several errors it could be:

errors: [
   { 
       "continent.name": "wrong value (or more detailed error description)"
   },
   { 
       "population.city[4].count": "wrong value (or more detailed error description)"
   },   
]

Thank you in advance!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant