jsonschemax

package
v0.0.631 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 30, 2024 License: Apache-2.0 Imports: 16 Imported by: 5

README

JSON Schema Helpers

This package contains utilities for working with JSON Schemas.

Listing all Possible JSON Schema Paths

Using jsonschemax.ListPaths() you can get a list of all possible JSON paths in a JSON Schema.

package main

import (
	"bytes"
	"fmt"
	"github.com/ory/jsonschema/v3"
	"github.com/ory/x/jsonschemax"
)

var schema = "..."

func main() {
	c := jsonschema.NewCompiler()
	_ = c.AddResource("test.json", bytes.NewBufferString(schema))
	paths, _ := jsonschemax.ListPaths("test.json", c)
	fmt.Printf("%+v", paths)
}

All keys are delimited using .. Please note that arrays are denoted with # when ListPathsWithArraysIncluded is used. For example, the JSON Schema

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "properties": {
    "providers": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          }
        }
      }
    }
  }
}

Results in paths:

[
  {
    "Title": "",
    "Description": "",
    "Examples": null,
    "Name": "providers",
    "Default": null,
    "Type": [],
    "TypeHint": 5,
    "Format": "",
    "Pattern": null,
    "Enum": null,
    "Constant": null,
    "ReadOnly": false,
    "MinLength": -1,
    "MaxLength": -1,
    "Required": false,
    "Minimum": null,
    "Maximum": null,
    "MultipleOf": null,
    "CustomProperties": null
  },
  {
    "Title": "",
    "Description": "",
    "Examples": null,
    "Name": "providers.#",
    "Default": null,
    "Type": {},
    "TypeHint": 5,
    "Format": "",
    "Pattern": null,
    "Enum": null,
    "Constant": null,
    "ReadOnly": false,
    "MinLength": -1,
    "MaxLength": -1,
    "Required": false,
    "Minimum": null,
    "Maximum": null,
    "MultipleOf": null,
    "CustomProperties": null
  },
  {
    "Title": "",
    "Description": "",
    "Examples": null,
    "Name": "providers.#.id",
    "Default": null,
    "Type": "",
    "TypeHint": 1,
    "Format": "",
    "Pattern": null,
    "Enum": null,
    "Constant": null,
    "ReadOnly": false,
    "MinLength": -1,
    "MaxLength": -1,
    "Required": false,
    "Minimum": null,
    "Maximum": null,
    "MultipleOf": null,
    "CustomProperties": null
  }
]

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FormatError added in v0.0.166

func FormatError(e *jsonschema.ValidationError) (string, string)

func FormatValidationErrorForCLI added in v0.0.166

func FormatValidationErrorForCLI(w io.Writer, conf []byte, err error)

func JSONPointerToDotNotation added in v0.0.82

func JSONPointerToDotNotation(pointer string) (string, error)

JSONPointerToDotNotation converts JSON Pointer "#/foo/bar" to dot-notation "foo.bar".

Types

type Error added in v0.0.82

type Error struct {
	// Type is the error type.
	Type ErrorType

	// DocumentPointer is the JSON Pointer in the document.
	DocumentPointer string

	// SchemaPointer is the JSON Pointer in the schema.
	SchemaPointer string

	// DocumentFieldName is a pointer to the document in dot-notation: fo.bar.baz
	DocumentFieldName string
}

Error represents a schema error.

func NewFromSanthoshError added in v0.0.82

func NewFromSanthoshError(validationError jsonschema.ValidationError) *Error

NewFromSanthoshError converts github.com/santhosh-tekuri/jsonschema.ValidationError to Error.

type ErrorType added in v0.0.82

type ErrorType int

ErrorType is the schema error type.

const (
	// ErrorTypeMissing represents a validation that failed because a value is missing.
	ErrorTypeMissing ErrorType = iota + 1
)

type Path

type Path struct {
	// Title of the path.
	Title string

	// Description of the path.
	Description string

	// Examples of the path.
	Examples []interface{}

	// Name is the JSON path name.
	Name string

	// Default is the default value of that path.
	Default interface{}

	// Type is a prototype (e.g. float64(0)) of the path type.
	Type interface{}

	TypeHint

	// Format is the format of the path if defined
	Format string

	// Pattern is the pattern of the path if defined
	Pattern *regexp.Regexp

	// Enum are the allowed enum values
	Enum []interface{}

	// first element in slice is constant value. note: slice is used to capture nil constant.
	Constant []interface{}

	// ReadOnly is whether the value is readonly
	ReadOnly bool

	// -1 if not specified
	MinLength int
	MaxLength int

	// Required if set indicates this field is required.
	Required bool

	Minimum *big.Float
	Maximum *big.Float

	MultipleOf *big.Float

	CustomProperties map[string]interface{}
}

Path represents a JSON Schema Path.

func ListPaths

func ListPaths(ctx context.Context, ref string, compiler *jsonschema.Compiler) ([]Path, error)

ListPaths lists all paths of a JSON Schema. Will return an error if circular references are found.

func ListPathsBytes

func ListPathsBytes(ctx context.Context, raw json.RawMessage, maxRecursion int16) ([]Path, error)

ListPathsBytes works like ListPathsWithRecursion but prepares the JSON Schema itself.

func ListPathsWithArraysIncluded added in v0.0.330

func ListPathsWithArraysIncluded(ctx context.Context, ref string, compiler *jsonschema.Compiler) ([]Path, error)

ListPathsWithArraysIncluded lists all paths of a JSON Schema. Will return an error if circular references are found. Includes arrays with `#`.

func ListPathsWithInitializedSchema added in v0.0.276

func ListPathsWithInitializedSchema(schema *jsonschema.Schema) ([]Path, error)

ListPathsWithInitializedSchema loads the paths from the schema without compiling it.

You MUST ensure that the compiler was using `ExtractAnnotations = true`.

func ListPathsWithInitializedSchemaAndArraysIncluded added in v0.0.330

func ListPathsWithInitializedSchemaAndArraysIncluded(schema *jsonschema.Schema) ([]Path, error)

ListPathsWithInitializedSchemaAndArraysIncluded loads the paths from the schema without compiling it.

You MUST ensure that the compiler was using `ExtractAnnotations = true`. Includes arrays with `#`.

func ListPathsWithRecursion added in v0.0.83

func ListPathsWithRecursion(ctx context.Context, ref string, compiler *jsonschema.Compiler, maxRecursion uint8) ([]Path, error)

ListPathsWithRecursion will follow circular references until maxRecursion is reached, without returning an error.

type PathEnhancer added in v0.0.95

type PathEnhancer interface {
	EnhancePath(Path) map[string]interface{}
}

type TypeHint added in v0.0.166

type TypeHint int
const (
	String TypeHint = iota + 1
	Float
	Int
	Bool
	JSON
	Nil

	BoolSlice
	StringSlice
	IntSlice
	FloatSlice
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL