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

Outdated documentation for ValidateBytes #69

Closed
matthiassommer opened this issue Jun 18, 2020 · 3 comments
Closed

Outdated documentation for ValidateBytes #69

matthiassommer opened this issue Jun 18, 2020 · 3 comments
Assignees
Labels

Comments

@matthiassommer
Copy link

matthiassommer commented Jun 18, 2020

The documentation presents examples such as
errs, err = rs.ValidateBytes(invalidPerson)

whereas the function definition is
func (s *Schema) ValidateBytes(ctx context.Context, data []byte) ([]KeyError, error) {

Can you provide me an example on how I should pass the context to this function? What is the benefit of this change?

For now, I go with

ctx := context.TODO()
errs, err := rs.ValidateBytes(ctx, metadataBytes)
@jc21
Copy link

jc21 commented Jun 25, 2020

Definitely interested to hear about "What is the benefit of this change?"

@b5
Copy link
Member

b5 commented Sep 2, 2020

Ah this is a great point, we'll get the documentation fixed up, but to speak to the benefit of the change:

we pass a context because the jsonschema spec states validation should be able to make web requests to fetch referenced schemas. Those requests can end up taking time (a slow server or a high-latency connection), and you might want to put a boundary on that request. Here's an example:

// create a context that times out after one second. whenever we create a context with values,
// we need to cancel it in all cases, so we defer a call to cancel
// we're passing the "background" context to "WithTimeout" because there's no deeper
// context to build on. context.Backround() has the same functionality as context.TODO()
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()

// call validation. If the root schema makes web requests, and those web requests
// take longer than one second, we'll get an error that is or wraps err.DeadlineExceeded
errs, err :=  rs.ValidateBytes(ctx, metadataBytes)

if err != nil {
  if errors.Is(err, context.DeadlineExceeded) {
    fmt.Println("request timed out!")
  }
  // handle other errors...
}

But if you don't want a deadline, the code you've written will work just fine! It's common practice to use context.Background() whenever you don't really need context cancellation. context.TODO() is generally reserved for code that's being upgraded to use contexts.

Hope that helps explain things! We'll mark this issue as closed when the docs are updated.

@Arqu Arqu self-assigned this Sep 7, 2020
@Arqu Arqu added the docs label Sep 7, 2020
@Arqu
Copy link
Contributor

Arqu commented Sep 7, 2020

The docs have been updated as part of #77. However I definitely think that the above higlights the rationale a lot more than just the code sample.

@Arqu Arqu closed this as completed Sep 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants