-
Notifications
You must be signed in to change notification settings - Fork 2
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
Proposal: Add validation without data unmarshalling #31
Comments
Yes I agree. This is already planned. Probably for v0.10 I have a working prototype in a local branch. The approach of validating a struct is fine but it does have some limitations:
Also, v0.9 will come with an update to the zhttp package which will handle json also. I think there is merit to recommending you use the zog implementation because we can introduce future performance improvements during parsing. You will be able to do A part from that I do agree 100% that this needs to be implemented. The prototype I have locally has the api of For example, Parse right now will check for require based on the input data. That means that the results here are different based on if you use Parse or Validate: // imagine this is the json we get from the request & we have a
s := `
{
"name": "hello"
}
`
var o struct {
Name int `json:"name"`
}
oSchema = z.Struct(z.Schema{"name": z.Int().Required()}).Required()
err := c.Bind(&o) // This will return an error that it cannot parse hello into name
oSchema.Validate(&o) // this will return an error that o is required but it is 0
oSchema.Parse(zhttp.Request(c.Request()), &o) // This will return an error failed to coerce "hello" into an integer Most of this weird behaviours are actually quite intuitive once you stop to think about them for a bit but it is still the case that one single schema is behaving in two different ways. Some other concerns:
|
since PreTransforms could make sense, but I can't think of many cases where you would want to do this. as for default and catch, it makes sense to remove them for |
Currently. zog handles both data unmarshaling and validation with
Parse
, which is great when you are using the standard library. whoever, this leads to duplicated unmarshalling in other http frameworks, in my case, echo:a more memory and performance efficient code may look like this:
the utility of this is not only limited to this case, it can also be useful when you already have data from somewhere else and you just want to validate it:
my proposal is to either create a
Validate
method, or be able to passnil
toParse
. or do both, being able to passnil
toParse
and create aValidate
method that wraps it for a better API.The text was updated successfully, but these errors were encountered: