Categorygithub.com/aaharu/go-jsschema
modulepackage
0.0.0-20180604175310-e38a2f24e826
Repository: https://github.com/aaharu/go-jsschema.git
Documentation: pkg.go.dev

# README

go-jsschema

Build Status

GoDoc

JSON Schema for Go

SYNOPSIS

package schema_test

import (
  "log"

  "github.com/lestrrat/go-jsschema"
  "github.com/lestrrat/go-jsschema/validator"
)

func Example() {
  s, err := schema.ReadFile("schema.json")
  if err != nil {
    log.Printf("failed to read schema: %s", err)
    return
  }

  for name, pdef := range s.Properties {
    // Do what you will with `pdef`, which contain
    // Schema information for `name` property
    _ = name
    _ = pdef
  }

  // You can also validate an arbitrary piece of data
  var p interface{} // initialize using json.Unmarshal...
  v := validator.New(s)
  if err := v.Validate(p); err != nil {
    log.Printf("failed to validate data: %s", err)
  }
}

DESCRIPTION

This packages parses a JSON Schema file, and allows you to inspect, modify the schema, but does nothing more.

If you want to validate using the JSON Schema that you read using this package, look at go-jsval, which allows you to generate validators, so that you don't have to dynamically read in the JSON schema for each instance of your program.

In the same lines, this package does not really care about loading external schemas from various locations (it's just easier to just gather all the schemas in your local system). It is possible to do this via go-jsref if you really want to do it.

BENCHMARKS

Latest version of libraries as of Sep 3 2016.

Benchmarks with gojsonschema are prefixed with Gojsonschema.

Benchmarks without prefixes are about ths package.

$ go test -tags benchmark -benchmem -bench=.
BenchmarkGojsonschemaParse-4            5000        357330 ns/op      167451 B/op       1866 allocs/op
BenchmarkParse-4                     1000000          1577 ns/op        1952 B/op          9 allocs/op
BenchmarkParseAndMakeValidator-4      500000          2806 ns/op        2304 B/op         13 allocs/op
PASS

TODO

  • Properly resolve ids and $refs (it works in simple cases, but elaborate scopes probably don't work)

Contributors

  • Daisuke Maki
  • Nate Finch

References

NameNotes
go-jsvalValidator generator
go-jshschemaJSON Hyper Schema implementation
go-jsrefJSON Reference implementation
go-jspointerJSON Pointer implementations

# Packages

No description provided by the author
No description provided by the author

# Functions

New creates a new schema object.
Read reads from `in` and parses its content to create a new Schema object.
ReadFile reads the file `f` and parses its content to create a new Schema object.

# Constants

The list of primitive types.
The list of primitive types.
The list of pre-defined JSON Schema formats.
The list of pre-defined JSON Schema formats.
The list of pre-defined JSON Schema formats.
The list of pre-defined JSON Schema formats.
The list of pre-defined JSON Schema formats.
The list of pre-defined JSON Schema formats.
HyperSchemaURL contains the JSON Hyper Schema URL.
The list of primitive types.
MIMEType contains the MIME used for a JSON Schema.
The list of primitive types.
The list of primitive types.
The list of primitive types.
SchemaURL contains the JSON Schema URL.
The list of primitive types.
The list of primitive types.

# Variables

ErrExpectedArrayOfString is returned when we encounter something other than array of strings.
ErrInvalidStringArray is the same as ErrExpectedArrayOfString.

# Structs

AdditionalItems represents schema for additonalItems.
AdditionalProperties represents schema for additonalProperties.
Bool represents a "boolean" value in a JSON Schema, such as "exclusiveMinimum", "exclusiveMaximum", etc.
DependencyMap contains the dependencies defined within this schema.
Integer represents a "integer" value in a JSON Schema, such as "minLength", "maxLength", etc.
ItemSpec represents a specification for `item` field.
Number represents a "number" value in a JSON Schema, such as "minimum", "maximum", etc.
Schema represents a JSON Schema object.

# Type aliases

Format represents one of the pre-defined JSON Schema formats.
PrimitiveType represents a JSON Schema primitive type such as "string", "integer", etc.
PrimitiveTypes is a list of PrimitiveType.
SchemaList is a list of Schemas.