schema

package
v0.0.0-...-55a0270 Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2022 License: Apache-2.0 Imports: 14 Imported by: 1

Documentation

Index

Constants

View Source
const (
	Draft7 = "http://json-schema.org/draft-07/schema#"
	Type   = "object"
)
View Source
const (
	VersionRxStr           = `^[0-9]+\.[0-9]+$`
	VersionPathResource    = "version"
	ResourceIDPathResource = "id"
	FragSep                = ";"
	FragAssignment         = "="
)

Variables

View Source
var Version0 = VersionInfo{
	Version:         "0.0",
	ValidRangeLower: "0.0",
	ValidRangeUpper: "0.0",
	Description:     "Supports JSON Objects for Metadata and Schema Definitions, both validated against JSON schemas",
	Validator:       V0,
}
View Source
var Version1 = VersionInfo{
	Version:         "1.0",
	ValidRangeLower: "1.0",
	ValidRangeUpper: "1.0",
	Description:     "Supports JSON Objects for Metadata and Schema Definitions, both validated against JSON schemas",
	Validator:       V1,
}

Functions

func Compare

func Compare(v1 string, comparator Comparator, v2 string) (bool, error)

Compare allows the comparison of two semantic versions with a given comparator defined by the Comparator enumeration

func ExtractSchemaAuthorDID

func ExtractSchemaAuthorDID(schemaID string) (did.DID, error)

ExtractSchemaAuthorDID parses the schema URI (did:work:<authorDID>;id=<uuid>;version=<version>) and returns the author's DID.

func ExtractSchemaResourceID

func ExtractSchemaResourceID(schemaID string) (string, error)

ExtractSchemaResourceID parses the schema URI (did:work:<authorDID>;id=<uuid>;version=<version>) and returns the resource ID.

func IDIsInVersionRange

func IDIsInVersionRange(schemaID string, versionRange string) (bool, error)

IDIsInVersionRange checks if the schema version falls within the given range. Returns an error if either the schema ID or range cannot be parsed.

func InRangeInclusive

func InRangeInclusive(version string, lower string, upper string) (bool, error)

InRangeInclusive determines whether the provided version is in the inclusive range of the provided version bounds

func IsJSON

func IsJSON(str string) bool

True if string is valid JSON, false otherwise

func Validate

func Validate(schema, document string) error

Validate exists to hide gojsonschema logic within this file it is the entry-point to validation logic, requiring the caller pass in valid json strings for each argument

func ValidateCredential

func ValidateCredential(credentialSubjectSchema, documentJSON string) error

Validate a credential's data is valid against its schema

func ValidateJSONSchema

func ValidateJSONSchema(maybeSchema ledger.JSONSchemaMap) error

ValidateJSONSchema takes in a map that is purported to be a JSON schema (schema definition) An error is returned if it is not a valid JSON schema, and nil is returned on success

func ValidateJSONSchemaString

func ValidateJSONSchemaString(maybeSchema string) error

ValidateJSONSchema takes in a string that is purported to be a JSON schema (schema definition) An error is returned if it is not a valid JSON schema, and nil is returned on success

func ValidateLedgerSchemaV1

func ValidateLedgerSchemaV1(document interface{}) error

func ValidateSchemaRequest

func ValidateSchemaRequest(document interface{}, version string) error

Validates a schema by finding the right validation method according to the version It is possible that multiple validation methods would be supported for a given version, so we always choose the validation method created last (heavy assumption here that the versions we track are properly ordered).

func ValidateWithJSONLoader

func ValidateWithJSONLoader(schemaLoader, documentLoader gojsonschema.JSONLoader) error

Validate takes schema and document loaders; the document from the loader is validated against the schema from the loader. Nil if good, error if bad

Types

type ArrayType

type ArrayType struct {
	AttributeType `json:"type" validate:"required"`
	*StringType
	*NumberType
}

TODO add support for arrays of objects and arrays

type Attribute

type Attribute struct {
	Name     string        `json:"-" validate:"required"`
	Type     AttributeType `json:"type,omitempty" validate:"required"`
	Required bool          `json:"-" validate:"required"`
	*StringType
	*NumberType
	*ObjectType
	*ArrayType
}

type AttributeType

type AttributeType string
const (
	String  AttributeType = "string"
	Number  AttributeType = "number"
	Object  AttributeType = "object"
	Array   AttributeType = "array"
	Boolean AttributeType = "boolean"
)

type Builder

type Builder struct {
	Name                 string `validate:"required"`
	Description          string `validate:"required"`
	AdditionalProperties bool
	Attributes           []Attribute `validate:"required"`
}

func (Builder) Build

func (b Builder) Build() (ledger.JSONSchemaMap, error)

Build takes in a schema descriptor and an ordered set of attributes and builds a valid JSON schema. The output is run through a schema validator before being returned

type Comparator

type Comparator int
const (
	LessThan Comparator = iota
	LessThanOrEqualTo
	GreaterThan
	GreaterThanOrEqualTo
	Equals
)

type FormatType

type FormatType string
const (
	Date     FormatType = "date"
	Time     FormatType = "time"
	DateTime FormatType = "date-time"
	Email    FormatType = "email"
)

type IDFormatErr

type IDFormatErr struct {
	// contains filtered or unexported fields
}

IDFormatErr is a formatting error in the Schema ID, which should be in the form <author_did>;id=<uuid>;version=<major.minor>.

func (IDFormatErr) Error

func (e IDFormatErr) Error() string

type InvalidSchemaError

type InvalidSchemaError struct {
	Errors []string
}

func (InvalidSchemaError) Error

func (err InvalidSchemaError) Error() string

type NumberType

type NumberType struct {
	Minimum          float32 `json:"minimum,omitempty"`
	Maximum          float32 `json:"maximum,omitempty"`
	ExclusiveMinimum float32 `json:"exclusiveMinimum,omitempty"`
	ExclusiveMaximum float32 `json:"exclusiveMaximum,omitempty"`
}

type ObjectType

type ObjectType struct {
	Properties           map[string]Attribute `json:"properties" validate:"required"`
	RequiredProperties   []string             `json:"requiredProperties" validate:"required"`
	AdditionalProperties bool                 `json:"additionalProperties" validate:"required"`
}

type Range

type Range struct {
	MajorRange RangeBounded
	MinorRange RangeBounded
}

Note uses Prefix range https://docs.gradle.org/current/userguide/single_versions.html

func RangeFromStr

func RangeFromStr(bound string) (Range, error)

RangeFromStr parses a string into a Range object. Returns an error if the string does not match any of the following:

1) Exact match. This looks like a version string. For example, "1.0" is a range that only matches version "1.0".

2) Major range. This looks like a carrot "^" prefixed to a version. For example, "^2.1" matches everything greater than or equal to a particular version ("2.1") in the same major range ("2.x").

3) Wild minor. This looks like a version string where the minor version has been replaced with either an "x" or an "*". This is equivalent to Major range with minor version "0". For example, "3.x" is includes everything with a major version of "3".

4) Wild. This is either "x" or "*". This matches any version.

func (Range) FallsInRange

func (rng Range) FallsInRange(schemaVersion Version) bool

FallsInRange returns true if the given version falls within this Range.

type RangeBounded

type RangeBounded func(subject int) bool

type StringType

type StringType struct {
	Format FormatType `json:"format,omitempty"`
}

type UnRecognisedRangeError

type UnRecognisedRangeError struct {
	// contains filtered or unexported fields
}

UnRecognisedRangeError is returned when encountering an invalid range string.

func (UnRecognisedRangeError) Error

func (e UnRecognisedRangeError) Error() string

type UnRecognisedVersionError

type UnRecognisedVersionError struct {
	// contains filtered or unexported fields
}

UnRecognisedVersionError is a formatting error in the Schema version, which should be in the form "major.minor".

func (UnRecognisedVersionError) Error

func (e UnRecognisedVersionError) Error() string

type UpdateInput

type UpdateInput struct {
	UpdatedSchema            *ledger.Schema
	UpdatedSchemaCategoryID  string
	PreviousSchema           *ledger.Schema
	PreviousSchemaCategoryID string
}

type UpdateResult

type UpdateResult struct {
	Valid          bool
	MajorChange    bool
	MinorChange    bool
	DerivedVersion string
	Message        string
}

func ValidateSchemaUpdate

func ValidateSchemaUpdate(schemaUpdateInput *UpdateInput) (*UpdateResult, error)

ValidateSchemaUpdate compares two schemas using schemaver rules and returns a summary of the update, which includes whether it's a major or minor change and a proposed version for the schema update.

type Validator

type Validator int
const (
	InValidator Validator = 0
	V0          Validator = 1
	V1          Validator = 2
)

func FindValidatorForVersion

func FindValidatorForVersion(version string) (Validator, error)

Find the most recently created validator the supports the provided version

type Version

type Version struct {
	Major int
	Minor int
}

wrapper around modified semantic version where only major and minor numbers are valid

func ExtractSchemaVersionFromID

func ExtractSchemaVersionFromID(schemaID string) (Version, error)

ExtractSchemaVersionFromID parses the schema URI (did:work:<authorDID>;id=<uuid>;version=<version>) and returns the schema version.

func VersionFromStr

func VersionFromStr(versionStr string) (Version, error)

VersionFromStr parses a version string into a Version object. Returns an error if the version string does not match the VersionRxStr regular expression, i.e. "<major>.<minor>", where major and minor are integers.

type VersionInfo

type VersionInfo struct {
	Version         string
	ValidRangeLower string
	ValidRangeUpper string
	Description     string
	Validator       Validator
}

Jump to

Keyboard shortcuts

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