Skip to content

Commit

Permalink
Add lock around jsonschema usage
Browse files Browse the repository at this point in the history
jsonschema isn't thread-safe,
qri-io/jsonschema#80,
so this adds a lock around the part that accesses a package level
variable so that we can use it in goroutines.

Signed-off-by: Carolyn Van Slyck <[email protected]>
  • Loading branch information
carolynvs committed Nov 24, 2020
1 parent a7234ee commit 68a6c84
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions bundle/definition/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package definition
import (
"encoding/base64"
"fmt"
"sync"

"github.com/qri-io/jsonschema"
)
Expand Down Expand Up @@ -33,9 +34,16 @@ func (c ContentEncoding) Validate(propPath string, data interface{}, errs *[]jso
}
}

var jsonSchemaLock sync.Mutex

// NewRootSchema returns a jsonschema.RootSchema with any needed custom
// jsonschema.Validators pre-registered
func NewRootSchema() *jsonschema.RootSchema {
// This is not thread-safe because jsonschema.RegisterValidator modifies package variables
// https://github.com/qri-io/jsonschema/issues/80
jsonSchemaLock.Lock()
defer jsonSchemaLock.Unlock()

// Register custom validators here
// Note: as of writing, jsonschema doesn't have a stock validator for instances of type `contentEncoding`
// There may be others missing in the library that exist in http://json-schema.org/draft-07/schema#
Expand Down

0 comments on commit 68a6c84

Please sign in to comment.