Documentation ¶
Index ¶
- func ValidateEntity(data []byte) (int, error)
- type BlockConstraint
- func (bc BlockConstraint) BlockConstraints(kind BlockKind) []*BlockConstraint
- func (bc BlockConstraint) Copy() *BlockConstraint
- func (bc BlockConstraint) DescribeCountConstraint(kind BlockKind) string
- func (bc BlockConstraint) IsNoop() bool
- func (bc BlockConstraint) Matches(b *newsdoc.Block) (Match, []string)
- func (bc *BlockConstraint) SetBlockConstraints(kind BlockKind, blocks []*BlockConstraint)
- type BlockConstraintSet
- type BlockDefinition
- type BlockKind
- type BlockSignature
- type BlockSource
- type ColourFormat
- type ConstraintMap
- type ConstraintSet
- type DefaultValueCollector
- type Deprecation
- type DeprecationContext
- type DeprecationDecision
- type DeprecationHandlerFunc
- type DocumentBlocks
- type DocumentConstraint
- type EntityRef
- type Enum
- type EnumConstraint
- type Geometry
- type Glob
- type GlobList
- type HTMLElement
- type HTMLPolicy
- type HTMLPolicySet
- type Match
- type NestedBlocks
- type RefType
- type Regexp
- type StringConstraint
- type StringFormat
- type ValidationContext
- type ValidationOptionFunc
- type ValidationResult
- type Validator
- type ValueAnnotation
- type ValueCollector
- type ValueDiscarder
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ValidateEntity ¶
ValidateEntity checks if the entity that starts at the beginning of the byte slice is valid and returns its length in bytes.
Types ¶
type BlockConstraint ¶
type BlockConstraint struct { Ref string `json:"ref,omitempty"` Declares *BlockSignature `json:"declares,omitempty"` Name string `json:"name,omitempty"` Description string `json:"description,omitempty"` Match ConstraintMap `json:"match,omitempty"` Count *int `json:"count,omitempty"` MaxCount *int `json:"maxCount,omitempty"` MinCount *int `json:"minCount,omitempty"` Links []*BlockConstraint `json:"links,omitempty"` Meta []*BlockConstraint `json:"meta,omitempty"` Content []*BlockConstraint `json:"content,omitempty"` Attributes ConstraintMap `json:"attributes,omitempty"` Data ConstraintMap `json:"data,omitempty"` Deprecated *Deprecation `json:"deprecated,omitempty"` }
BlockConstraint is a specification for a block.
func (BlockConstraint) BlockConstraints ¶
func (bc BlockConstraint) BlockConstraints(kind BlockKind) []*BlockConstraint
BlockConstraints implements the BlockConstraintsSet interface.
func (BlockConstraint) Copy ¶ added in v0.9.0
func (bc BlockConstraint) Copy() *BlockConstraint
func (BlockConstraint) DescribeCountConstraint ¶
func (bc BlockConstraint) DescribeCountConstraint(kind BlockKind) string
DescribeCountConstraint returns a human readable (english) description of the count contstraint for the block constraint.
func (BlockConstraint) IsNoop ¶ added in v0.9.0
func (bc BlockConstraint) IsNoop() bool
IsNoop returns true if the constraint doesn't affect anything.
func (BlockConstraint) Matches ¶
func (bc BlockConstraint) Matches(b *newsdoc.Block) (Match, []string)
Matches checks if the given block matches the constraint and returns the names of the attributes that matched.
func (*BlockConstraint) SetBlockConstraints ¶ added in v0.9.0
func (bc *BlockConstraint) SetBlockConstraints(kind BlockKind, blocks []*BlockConstraint)
SetBlockConstraints implements the BlockConstraintsSet interface.
type BlockConstraintSet ¶
type BlockConstraintSet interface { // BlockConstraints returns the constraints of the specified kind. BlockConstraints(kind BlockKind) []*BlockConstraint SetBlockConstraints(kind BlockKind, blocks []*BlockConstraint) }
type BlockDefinition ¶ added in v0.9.0
type BlockDefinition struct { ID string `json:"id"` Block BlockConstraint `json:"block"` }
type BlockKind ¶
type BlockKind string
BlockKind describes the different kinds of blocks that are available.
const ( BlockKindLink BlockKind = "link" BlockKindMeta BlockKind = "meta" BlockKindContent BlockKind = "content" )
The different kinds of blocks that a block source can have.
func (BlockKind) Description ¶
Description returns the pluralised name of the block kind.
type BlockSignature ¶
type BlockSignature struct { Type string `json:"type,omitempty"` Rel string `json:"rel,omitempty"` Role string `json:"role,omitempty"` }
BlockSignature is the signature of a block declaration.
func (BlockSignature) AsConstraint ¶ added in v0.9.0
func (bs BlockSignature) AsConstraint() ConstraintMap
type BlockSource ¶
type BlockSource interface { // GetBlocks returns the child blocks of the specified type. GetBlocks(kind BlockKind) []newsdoc.Block }
BlockSource acts as an intermediary to allow us to write code that can treat both documents and blocks as a source of blocks.
type ColourFormat ¶ added in v0.9.0
type ColourFormat string
const ( ColourUnknown ColourFormat = "" ColourHex ColourFormat = "hex" ColourRGB ColourFormat = "rgb" ColourRGBA ColourFormat = "rgba" )
type ConstraintMap ¶
type ConstraintMap struct { Keys []string Constraints map[string]StringConstraint }
func MakeConstraintMap ¶ added in v0.8.2
func MakeConstraintMap(constraints map[string]StringConstraint) ConstraintMap
func (ConstraintMap) Copy ¶ added in v0.9.0
func (cm ConstraintMap) Copy() ConstraintMap
func (ConstraintMap) JSONSchemaAlias ¶ added in v0.9.0
func (cm ConstraintMap) JSONSchemaAlias() any
func (ConstraintMap) MarshalJSON ¶ added in v0.8.1
func (cm ConstraintMap) MarshalJSON() ([]byte, error)
func (ConstraintMap) Requirements ¶
func (cm ConstraintMap) Requirements() string
func (*ConstraintMap) UnmarshalJSON ¶ added in v0.8.1
func (cm *ConstraintMap) UnmarshalJSON(data []byte) error
type ConstraintSet ¶
type ConstraintSet struct { Version int `json:"version,omitempty"` Schema string `json:"$schema,omitempty"` Name string `json:"name"` Documents []DocumentConstraint `json:"documents,omitempty"` Links []*BlockDefinition `json:"links,omitempty"` Meta []*BlockDefinition `json:"meta,omitempty"` Content []*BlockDefinition `json:"content,omitempty"` Enums []Enum `json:"enums,omitempty"` HTMLPolicies []HTMLPolicy `json:"htmlPolicies,omitempty"` }
func DecodeConstraintSetsFS ¶ added in v0.8.1
func DecodeConstraintSetsFS( sFS embed.FS, names ...string, ) ([]ConstraintSet, error)
DecodeConstraintSetsFS decodes a set of constraints from a embedded filesystem.
func (ConstraintSet) Validate ¶
func (cs ConstraintSet) Validate() error
type DefaultValueCollector ¶ added in v0.4.0
type DefaultValueCollector struct {
// contains filtered or unexported fields
}
func NewValueCollector ¶ added in v0.4.0
func NewValueCollector() *DefaultValueCollector
func (*DefaultValueCollector) CollectValue ¶ added in v0.4.0
func (c *DefaultValueCollector) CollectValue(a ValueAnnotation)
func (*DefaultValueCollector) Values ¶ added in v0.4.0
func (c *DefaultValueCollector) Values() []ValueAnnotation
func (*DefaultValueCollector) With ¶ added in v0.4.0
func (c *DefaultValueCollector) With(ref EntityRef) ValueCollector
type Deprecation ¶ added in v0.5.0
type DeprecationContext ¶ added in v0.5.0
type DeprecationContext struct { // Entity references the deprecated entity. Empty if this is a document // deprecation. Entity *EntityRef `json:"entity,omitempty"` // Block is provided unless this is a document or document attribute deprecation. Block *newsdoc.Block `json:"block,omitempty"` // Value is provided if this was a value deprecation. Value *string `json:"value,omitempty"` }
type DeprecationDecision ¶ added in v0.7.1
DeprecationDecision tells revisor how to handle the deprecation.
type DeprecationHandlerFunc ¶ added in v0.5.0
type DeprecationHandlerFunc func( ctx context.Context, doc *newsdoc.Document, deprecation Deprecation, c DeprecationContext, ) (DeprecationDecision, error)
DeprecationHandlerFunc can handle a deprecation, and should return an error if the deprecation should be enforced (treated as a validation error).
type DocumentBlocks ¶
type DocumentBlocks struct {
// contains filtered or unexported fields
}
func NewDocumentBlocks ¶
func NewDocumentBlocks(document *newsdoc.Document) DocumentBlocks
type DocumentConstraint ¶
type DocumentConstraint struct { Name string `json:"name,omitempty"` Description string `json:"description,omitempty"` // Declares is used to declare a document type. Declares string `json:"declares,omitempty"` // Match is used to extend other document declarations. Match ConstraintMap `json:"match,omitempty"` Links []*BlockConstraint `json:"links,omitempty"` Meta []*BlockConstraint `json:"meta,omitempty"` Content []*BlockConstraint `json:"content,omitempty"` Attributes ConstraintMap `json:"attributes,omitempty"` Deprecated *Deprecation `json:"deprecated,omitempty"` }
DocumentConstraint describes a set of constraints for a document. Either by declaring a document type, or matching against a document that has been declared somewhere else.
func (DocumentConstraint) BlockConstraints ¶
func (dc DocumentConstraint) BlockConstraints(kind BlockKind) []*BlockConstraint
BlockConstraints implements the BlockConstraintsSet interface.
func (DocumentConstraint) Matches ¶
func (dc DocumentConstraint) Matches( d *newsdoc.Document, vCtx *ValidationContext, ) Match
Matches checks if the given document matches the constraint.
func (*DocumentConstraint) SetBlockConstraints ¶ added in v0.9.0
func (dc *DocumentConstraint) SetBlockConstraints(kind BlockKind, blocks []*BlockConstraint)
SetBlockConstraints implements the BlockConstraintsSet interface.
type EntityRef ¶
type EnumConstraint ¶ added in v0.8.0
type EnumConstraint struct { Forbidden bool `json:"forbidden,omitempty"` Deprecated *Deprecation `json:"deprecated,omitempty"` Description string `json:"description,omitempty"` }
type Geometry ¶ added in v0.4.0
type Geometry string
const ( GeometryAny Geometry = "" GeometryPoint Geometry = "point" GeometryMultiPoint Geometry = "multipoint" GeometryLineString Geometry = "linestring" GeometryMultiLineString Geometry = "multilinestring" GeometryPolygon Geometry = "polygon" GeometryMultiPolygon Geometry = "multipolygon" GeometryCircularString Geometry = "circularstring" )
type Glob ¶
type Glob struct {
// contains filtered or unexported fields
}
Glob is used to represent a compiled glob pattern that can be used with JSON marshalling and unmarshalling.
func CompileGlob ¶
CompileGlob compiles a glob pattern.
func (*Glob) MarshalJSON ¶
func (*Glob) UnmarshalJSON ¶
type GlobList ¶
type GlobList []*Glob
GlobList is a Glob slice with some convenience functions.
func (GlobList) MatchOrEmpty ¶
MatchOrEmpty returns true if the value matches any of the glob patterns, or if the list is nil or empty.
type HTMLElement ¶
type HTMLElement struct {
Attributes ConstraintMap `json:"attributes,omitempty"`
}
HTMLElement describes the constraints for a HTML element.
type HTMLPolicy ¶
type HTMLPolicy struct { Name string `json:"name,omitempty"` Description string `json:"description,omitempty"` // Uses will base the policy on another policy. Uses string `json:"uses,omitempty"` // Extends will add the declared elements to another policy. Extends string `json:"extends,omitempty"` Elements map[string]HTMLElement `json:"elements"` // contains filtered or unexported fields }
HTMLPolicy is used to declare supported elements, and what attributes they can have.
func (*HTMLPolicy) Check ¶
func (hp *HTMLPolicy) Check(v string) error
Check that the given value follows the constraints of the policy.
type HTMLPolicySet ¶
type HTMLPolicySet struct {
// contains filtered or unexported fields
}
HTMLPolicySet is a set of declared HTML policies.
func NewHTMLPolicySet ¶
func NewHTMLPolicySet() *HTMLPolicySet
func (*HTMLPolicySet) Add ¶
func (s *HTMLPolicySet) Add(source string, policies ...HTMLPolicy) error
Add policies to the set.
func (*HTMLPolicySet) Resolve ¶
func (s *HTMLPolicySet) Resolve() (map[string]*HTMLPolicy, error)
Resolve all extensions and usages and return the finished policies.
type NestedBlocks ¶
type NestedBlocks struct {
// contains filtered or unexported fields
}
func NewNestedBlocks ¶
func NewNestedBlocks(block *newsdoc.Block) NestedBlocks
type Regexp ¶
type Regexp struct {
// contains filtered or unexported fields
}
func (*Regexp) MarshalJSON ¶
func (*Regexp) UnmarshalJSON ¶
type StringConstraint ¶
type StringConstraint struct { Name string `json:"name,omitempty"` Description string `json:"description,omitempty"` Optional bool `json:"optional,omitempty"` AllowEmpty bool `json:"allowEmpty,omitempty"` Const *string `json:"const,omitempty"` Enum []string `json:"enum,omitempty"` EnumRef string `json:"enumReference,omitempty"` Pattern *Regexp `json:"pattern,omitempty"` Glob GlobList `json:"glob,omitempty"` Format StringFormat `json:"format,omitempty"` Time string `json:"time,omitempty"` Geometry string `json:"geometry,omitempty"` ColourFormats []ColourFormat `json:"colourFormats,omitempty"` HTMLPolicy string `json:"htmlPolicy,omitempty"` Deprecated *Deprecation `json:"deprecated,omitempty"` // Labels (and hints) are not constraints per se, but should be seen as // labels on the value that can be used by systems that process data // with the help of revisor schemas. Labels []string `json:"labels,omitempty"` Hints map[string][]string `json:"hints,omitempty"` }
func (StringConstraint) Requirement ¶
func (sc StringConstraint) Requirement() string
func (*StringConstraint) Validate ¶
func (sc *StringConstraint) Validate( value string, exists bool, vCtx *ValidationContext, ) (*Deprecation, error)
type StringFormat ¶
type StringFormat string
const ( StringFormatNone StringFormat = "" StringFormatRFC3339 StringFormat = "RFC3339" StringFormatInt StringFormat = "int" StringFormatFloat StringFormat = "float" StringFormatBoolean StringFormat = "bool" StringFormatHTML StringFormat = "html" StringFormatUUID StringFormat = "uuid" StringFormatWKT StringFormat = "wkt" StringFormatColour StringFormat = "colour" )
func (StringFormat) Describe ¶
func (f StringFormat) Describe() string
type ValidationContext ¶
type ValidationOptionFunc ¶
type ValidationOptionFunc func(vc *ValidationContext)
func WithDeprecationHandler ¶ added in v0.5.0
func WithDeprecationHandler( fn DeprecationHandlerFunc, ) ValidationOptionFunc
func WithValueCollector ¶
func WithValueCollector( collector ValueCollector, ) ValidationOptionFunc
type ValidationResult ¶
type ValidationResult struct { Entity []EntityRef `json:"entity,omitempty"` Error string `json:"error,omitempty"` EnforcedDeprecation bool `json:"enforcedDeprecation,omitempty"` }
func (ValidationResult) String ¶
func (vr ValidationResult) String() string
type Validator ¶
type Validator struct {
// contains filtered or unexported fields
}
func NewValidator ¶
func NewValidator( constraints ...ConstraintSet, ) (*Validator, error)
func (*Validator) ValidateDocument ¶
func (v *Validator) ValidateDocument( ctx context.Context, document *newsdoc.Document, opts ...ValidationOptionFunc, ) ([]ValidationResult, error)
func (*Validator) WithConstraints ¶ added in v0.4.0
func (v *Validator) WithConstraints( constraints ...ConstraintSet, ) (*Validator, error)
WithConstraints returns a new Validator that uses an additional set of constraints.
type ValueAnnotation ¶
type ValueAnnotation struct { Ref []EntityRef `json:"ref"` Constraint StringConstraint `json:"constraint"` Value string `json:"value"` }
type ValueCollector ¶
type ValueCollector interface { CollectValue(a ValueAnnotation) With(ref EntityRef) ValueCollector }
type ValueDiscarder ¶
type ValueDiscarder struct{}
func (ValueDiscarder) CollectValue ¶
func (ValueDiscarder) CollectValue(_ ValueAnnotation)
CollectValue implements ValueCollector.
func (ValueDiscarder) With ¶
func (ValueDiscarder) With(_ EntityRef) ValueCollector
With implements ValueCollector.