top
(README.md)
[![Build Status](https://travis-ci.org/juju/gojsonschema.svg)](https://travis-ci.org/juju/gojsonschema)

# gojsonschema

## Description

An implementation of JSON Schema, based on IETF's draft v4 - Go language

References :

* http://json-schema.org
* http://json-schema.org/latest/json-schema-core.html
* http://json-schema.org/latest/json-schema-validation.html

## Installation

```
go get github.com/juju/gojsonschema
```

Dependencies :
* https://github.com/juju/gojsonpointer
* https://github.com/juju/gojsonreference
* https://github.com/stretchr/testify/assert

## Usage 

### Example

```go

package main

import (
    "fmt"
    "github.com/juju/gojsonschema"
)

func main() {

    schemaLoader := gojsonschema.NewReferenceLoader("file:///home/me/schema.json")
    documentLoader := gojsonschema.NewReferenceLoader("file:///home/me/document.json")

    result, err := gojsonschema.Validate(schemaLoader, documentLoader)
    if err != nil {
        panic(err.Error())
    }

    if result.Valid() {
        fmt.Printf("The document is valid\n")
    } else {
        fmt.Printf("The document is not valid. see errors :\n")
        for _, desc := range result.Errors() {
            fmt.Printf("- %s\n", desc)
        }
    }

}


```

#### Loaders

There are various ways to load your JSON data.
In order to load your schemas and documents, 
first declare an appropriate loader :

* Web / HTTP, using a reference :

```go
loader := gojsonschema.NewReferenceLoader("http://www.some_host.com/schema.json")
```

* Local file, using a reference :

```go
loader := gojsonschema.NewReferenceLoader("file:///home/me/schema.json")
```

References use the URI scheme, the prefix (file://) and a full path to the file are required.

* JSON strings :

```go
loader := gojsonschema.NewStringLoader(`{"type": "string"}`)
```

* Custom Go types :

```go
m := map[string]interface{}{"type": "string"}
loader := gojsonschema.NewGoLoader(m)
```

And

```go
type Root struct {
	Users []User `json:"users"`
}

type User struct {
	Name string `json:"name"`
}

...

data := Root{}
data.Users = append(data.Users, User{"John"})
data.Users = append(data.Users, User{"Sophia"})
data.Users = append(data.Users, User{"Bill"})

loader := gojsonschema.NewGoLoader(data)
```

#### Validation

Once the loaders are set, validation is easy :

```go
result, err := gojsonschema.Validate(schemaLoader, documentLoader)
```

Alternatively, you might want to load a schema only once and process to multiple validations :

```go
schema, err := gojsonschema.NewSchema(schemaLoader)
...
result1, err := schema.Validate(documentLoader1)
...
result2, err := schema.Validate(documentLoader2)
...
// etc ...
```

To check the result :

```go
    if result.Valid() {
    	fmt.Printf("The document is valid\n")
    } else {
        fmt.Printf("The document is not valid. see errors :\n")
        for _, desc := range result.Errors() {
            fmt.Printf("- %s\n", desc)
        }
    }
```

## Uses

gojsonschema uses the following test suite :

https://github.com/json-schema/JSON-Schema-Test-Suite

Imported by 26 package(s)

  1. github.com/alesstimec/charm
  2. github.com/axw/charm
  3. github.com/binary132/charm
  4. github.com/bogdanteleaga/charm
  5. github.com/cmars/charm
  6. github.com/dimitern/charm
  7. github.com/dooferlad/charm
  8. github.com/ericsnowcurrently/juju-charm
  9. github.com/frankban/juju-charm
  10. github.com/gabriel-samfira/charm
  11. github.com/howbazaar/charm
  12. github.com/juju/charm
  13. github.com/kat-co/charm
  14. github.com/katco-/charm
  15. github.com/makyo/charm
  16. github.com/mattyw/charm
  17. github.com/natefinch/charm
  18. github.com/perrito666/charm
  19. github.com/tasdomas/charm
  20. github.com/urosj/charm
  21. gopkg.in/juju/charm.v2
  22. gopkg.in/juju/charm.v3
  23. gopkg.in/juju/charm.v4
  24. gopkg.in/juju/charm.v5
  25. gopkg.in/juju/charm.v5-unstable
  26. gopkg.in/juju/charm.v6-unstable

Imported only in test by 1 package(s)

  1. github.com/binary132/gojsonschema

Imports 1 package(s)

  1. github.com/juju/gojsonreference

Test imports 1 package(s)

  1. github.com/stretchr/testify/assert