jsonschema

package
v0.3.16 Latest Latest
Warning

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

Go to latest
Published: May 6, 2024 License: MPL-2.0 Imports: 10 Imported by: 34

Documentation

Overview

Example (BasicSchema)
package main

import (
	"fmt"
	"log"

	"github.com/cloudquery/codegen/jsonschema"
)

func main() {
	type basic struct {
		A string `json:"a" jsonschema:"minLength=2,required"`
	}

	data, err := jsonschema.Generate(new(basic))
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(string(data))
}
Output:

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://github.com/cloudquery/codegen/jsonschema_test/basic",
  "$ref": "#/$defs/basic",
  "$defs": {
    "basic": {
      "properties": {
        "a": {
          "type": "string",
          "minLength": 2
        }
      },
      "additionalProperties": false,
      "type": "object",
      "required": [
        "a"
      ]
    }
  }
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Generate

func Generate(a any, options ...Option) ([]byte, error)

Generate returns a formatted JSON schema for the input struct, according to the tags defined by https://github.com/invopop/jsonschema

func GenerateIntoFile added in v0.3.3

func GenerateIntoFile(a any, filePath string, options ...Option)

func Sanitize added in v0.3.5

func Sanitize(sc *jsonschema.Schema) error

func TestJSONSchema added in v0.3.5

func TestJSONSchema(t *testing.T, schema string, cases []TestCase)

func WithRemovedKeys added in v0.3.5

func WithRemovedKeys(t *testing.T, val any, keys ...string) string

Types

type Option added in v0.3.9

type Option func(*jsonschema.Reflector)

func WithAddGoComments added in v0.3.9

func WithAddGoComments(base, path string) Option

type TestCase added in v0.3.5

type TestCase struct {
	Name         string
	Spec         string
	Err          bool
	ErrorMessage string
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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