Skip to content

Commit

Permalink
Update documentation and fix a type in a test
Browse files Browse the repository at this point in the history
  • Loading branch information
johandorland committed Aug 16, 2018
1 parent cf97511 commit da425eb
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
47 changes: 47 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,51 @@ To check the result :
}
```

## Loading local schemas

By default `file` and `http(s)` references to external schemas are loaded automatically via the file system or via http(s). An external schema can also be loaded using a `SchemaLoader`.

```go
sl := gojsonschema.NewSchemaLoader()
loader1 := gojsonschema.NewStringLoader(`{ "type" : "string" }`)
err := sl.AddSchema("http://some_host.com/string.json", loader1)
```

Alternatively if your schema already has an `$id` you can use the `AddSchemas` function
```go
loader2 := gojsonschema.NewStringLoader(`{
"$id" : "http://some_host.com/maxlength.json",
"maxLength" : 5
}`)
err = sl.AddSchemas(loader2)
```

The main schema should be passed to the `Compile` function. This main schema can then directly reference the added schemas without needing to download them.
```go
loader3 := gojsonschema.NewStringLoader(`{
"$id" : "http://some_host.com/main.json",
"allOf" : [
{ "$ref" : "http://some_host.com/string.json" },
{ "$ref" : "http://some_host.com/maxlength.json" }
]
}`)

schema, err := sl.Compile(loader3)

documentLoader := gojsonschema.NewStringLoader(`"hello world"`)

result, err := schema.Validate(documentLoader)
```

It's also possible to pass a `ReferenceLoader` to the `Compile` function that references a loaded schema.

```go
err = sl.AddSchemas(loader3)
schema,err := sl.Compile(NewReferenceLoader("http://some_host.com/main.json"))
```

Schemas added by `AddSchema` and `AddSchemas` are only validated when the entire schema is compiled. Returned errors only contain errors about invalid URIs or if a URI is associated with multiple schemas. This may change in the future.

## Working with Errors

The library handles string error codes which you can customize by creating your own gojsonschema.locale and setting it
Expand Down Expand Up @@ -192,6 +237,8 @@ Note: An error of RequiredType has an err.Type() return value of "required"
"number_gt": NumberGTError
"number_lte": NumberLTEError
"number_lt": NumberLTError
"condition_then" : ConditionThenError
"condition_else" : ConditionElseError

**err.Value()**: *interface{}* Returns the value given

Expand Down
2 changes: 1 addition & 1 deletion schemaLoader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestCrossReference(t *testing.T) {
}

// Multiple schemas identifying under the same $id should throw an error
func TestDoubleIDRefernce(t *testing.T) {
func TestDoubleIDReference(t *testing.T) {
ps := NewSchemaLoader()
err := ps.AddSchema("http://localhost:1234/test4.json", NewStringLoader("{}"))
assert.Nil(t, err)
Expand Down

0 comments on commit da425eb

Please # to comment.