package jsonschema

import "cuelang.org/go/encoding/jsonschema"

Package jsonschema converts JSON Schema to CUE

JSON Schema data is presented in CUE, so any of the supported encodings that can represent JSON Schema data can be used as a source.

Package jsonschema implements the JSON schema standard.

Mapping and Linking

JSON Schema are often defined in a single file. CUE, on the other hand idiomatically defines schema as a definition.

CUE:

$schema: which schema is used for validation.
$id: which validation does this schema provide.

Foo: _ @jsonschema(sc)
@source(https://...) // What schema is used to validate.

NOTE: JSON Schema is a draft standard and may undergo backwards incompatible changes.

Index

Constants

const DefaultVersion = VersionDraft2020_12

DefaultVersion defines the default schema version used when there is no $schema field and no explicit [Config.DefaultVersion].

Functions

func DefaultMapURL

func DefaultMapURL(u *url.URL) (string, cue.Path, error)

DefaultMapURL implements the default schema ID to import path mapping. It trims off any ".json" suffix and uses the package name "schema" if the final component of the path isn't a valid CUE identifier.

func Extract

func Extract(data cue.InstanceOrValue, cfg *Config) (f *ast.File, err error)

Extract converts JSON Schema data into an equivalent CUE representation.

The generated CUE schema is guaranteed to deem valid any value that is a valid instance of the source JSON schema.

Types

type Config

type Config struct {
	PkgName string

	// ID sets the URL of the original source, corresponding to the $id field.
	ID string

	// JSON reference of location containing schema. The empty string indicates
	// that there is a single schema at the root.
	//
	// Examples:
	//  "#/"                     top-level fields are schemas.
	//  "#/components/schemas"   the canonical OpenAPI location.
	Root string

	// Map maps the locations of schemas and definitions to a new location.
	// References are updated accordingly. A returned label must be
	// an identifier or string literal.
	//
	// The default mapping is
	//    {}                     {}
	//    {"definitions", foo}   {#foo} or {#, foo}
	//    {"$defs", foo}         {#foo} or {#, foo}
	Map func(pos token.Pos, path []string) ([]ast.Label, error)

	// MapURL maps a URL reference as found in $ref to
	// an import path for a package and a path within that package.
	// If this is nil, [DefaultMapURL] will be used.
	MapURL func(u *url.URL) (importPath string, path cue.Path, err error)

	// Strict reports an error for unsupported features and keywords,
	// rather than ignoring them. When true, this is equivalent to
	// setting both StrictFeatures and StrictKeywords to true.
	Strict bool

	// StrictFeatures reports an error for features that are known
	// to be unsupported.
	StrictFeatures bool

	// StrictKeywords reports an error when unknown keywords
	// are encountered.
	StrictKeywords bool

	// DefaultVersion holds the default schema version to use
	// when no $schema field is present. If it is zero, [DefaultVersion]
	// will be used.
	DefaultVersion Version
	// contains filtered or unexported fields
}

A Config configures a JSON Schema encoding or decoding.

type Version

type Version int
const (
	VersionUnknown Version = iota // unknown
	VersionDraft4                 // http://json-schema.org/draft-04/schema#
	// Note: draft 5 never existed and should not be used.
	VersionDraft6       // http://json-schema.org/draft-06/schema#
	VersionDraft7       // http://json-schema.org/draft-07/schema#
	VersionDraft2019_09 // https://json-schema.org/draft/2019-09/schema
	VersionDraft2020_12 // https://json-schema.org/draft/2020-12/schema

	// Note: OpenAPI stands alone: it's not in the regular JSON Schema lineage.
	VersionOpenAPI // OpenAPI 3.0
)

func ParseVersion

func ParseVersion(sv string) (Version, error)

ParseVersion parses a version URI that defines a JSON Schema version.

func (Version) String

func (i Version) String() string

Source Files

constraints.go constraints_array.go constraints_combinator.go constraints_format.go constraints_generic.go constraints_meta.go constraints_number.go constraints_object.go constraints_string.go decode.go doc.go jsonschema.go ref.go version.go version_string.go

Directories

PathSynopsis
encoding/jsonschema/internal
Version
v0.12.0-0.dev
Published
Nov 20, 2024
Platform
darwin/amd64
Imports
18 packages
Last checked
1 hour ago

Tools for package owners.