Categorygithub.com/taskcluster/go-schematypes
modulepackage
0.0.0-20191014125726-d255d06c7b9c
Repository: https://github.com/taskcluster/go-schematypes.git
Documentation: pkg.go.dev

# README

Package schematypes

Build Status

Package schematypes provides types for constructing JSON schemas programmatically. This is useful when having a plugin architecture that accepts JSON matching a given schema as input. As this will allow nesting of plugins.

Example using an integer, works the same for objects and arrays.


import (
  "encoding/json"
  "fmt"

  "github.com/taskcluster/go-schematypes"
)

func main() {
  // Define a schema
  schema := schematypes.Integer{
    MetaData: schematypes.MetaData{
      Title:       "my-title",
      Description: "my-description",
    },
    Minimum: -240,
    Maximum: 240,
  }

  // Parse JSON
  var data interface{}
  err := json.Unmarshal(data, []byte(`234`))
  if err != nil {
    panic("JSON parsing error")
  }

  // Validate against schema
  err = schema.Validate(data)
  if err != nil {
    panic("JSON didn't validate against schema")
  }

  // Map parse data without casting, this is particularly useful for JSON
  // structures as they can be mapped to structs with the `json:"..."` tag.
  var result int
  err = schema.Map(&result, data)
  if err != nil {
    panic("JSON validation failed, or type given to map doesn't match the schema")
  }

  // Now we can use the value we mapped out
  fmt.Println(result)
}

License

This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.

# Functions

Merge multiple object schemas, this will create an object schema with all the properties from the schemas given, and all the required properties as the given object schemas have.
MustMap will map data into target using schema and panic, if it returns ErrTypeMismatch.
MustValidate panics if data doesn't validate against schema.
MustValidateAndMap panics if data doesn't validate or maps into target.
NewSchema creates a Schema that wraps a jsonschema.

# Variables

ErrTypeMismatch is returned when trying to map to a type that doesn't match or which isn't writable (for example passed by value and not pointer).

# Structs

An Array struct represents the JSON schema for an array.
Boolean schema type.
DateTime schema type for strings with format: date-time.
Duration schema type for duration as integer seconds or string on the form: /[-+]? (\d+ d(ays?)?)? (\d+ h((ours?)?|r))? (\d+ m(in(untes?)?)?)?/ This allows for a lot of human readable time durations, examples: '31 days 2 hours 5 min' '5 min' '2 hours 5 min' '+ 3 minutes' '- 2 hr' '- 3 days' '1d2h3m' ' 1 day 2 hour 1 minutes '.
The Integer struct represents a JSON schema for an integer.
IntegerEnum schema type for enums of integers.
Map specifies schema for a map from string to values.
Number schema type.
Object specifies schema for an object.
String schema type.
StringEnum schema type for enums of strings.
URI schema type for strings with format: uri.
ValidationError represents a validation failure as a list of validation issues.
A ValidationIssue is any error found validating a JSON object.

# Interfaces

A Schema is implemented by any object that can represent a JSON schema.

# Type aliases

An AllOf instance represents the allOf JSON schema construction.
An AnyOf instance represents the anyOf JSON schema construction.
A OneOf instance represents the oneOf JSON schema construction.
Properties defines the properties for a object schema.