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

IO loaders #136

Merged
merged 3 commits into from
Feb 10, 2017
Merged

IO loaders #136

merged 3 commits into from
Feb 10, 2017

Conversation

technoweenie
Copy link
Contributor

This adds new loaders for io.Reader and io.Writer objects.

Validating an http request body:

reqLoader, body := gojsonschema.NewReaderLoader(r.Body)
obj := &someObj{}
err := json.NewDecoder(body).Decode(obj)
r.Body.Close()
assert.Nil(t, err)

res, err := gojsonschema.Validate(schemaLoader, reqLoader)
if assert.Nil(t, err) {
  assert.True(t, res.Valid())
}

Validating an http response body:

resLoader, resWriter := gojsonschema.NewWriterLoader(w)
err = json.NewEncoder(resWriter).Encode(someObj)
assert.Nil(t, err)

res, err := gojsonschema.Validate(schemaLoader, reqLoader)
if assert.Nil(t, err) {
  assert.True(t, res.Valid())
}

You can see this in use at git-lfs/git-lfs#1931.

@xeipuuv xeipuuv merged commit 6b67b3f into xeipuuv:master Feb 10, 2017
@technoweenie technoweenie deleted the io-loaders branch February 13, 2017 16:31
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

Successfully merging this pull request may close these issues.

2 participants