Documentation ¶
Index ¶
- func ValidateEntity(data []byte) (int, error)
- type BlockConstraint
- type BlockConstraintSet
- type BlockKind
- type BlockSignature
- type BlockSource
- type BlocksFrom
- type BorrowedBlocks
- type ConstraintMap
- type ConstraintSet
- type DocumentBlocks
- type DocumentConstraint
- type EntityRef
- 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 { 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"` BlocksFrom []BlocksFrom `json:"blocksFrom,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) 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) 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) UnmarshalJSON ¶
func (bc *BlockConstraint) UnmarshalJSON(data []byte) error
type BlockConstraintSet ¶
type BlockConstraintSet interface { // BlockConstraints returns the constraints of the specified kind. BlockConstraints(kind BlockKind) []*BlockConstraint }
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"` }
BlockSignature is the signature of a block declaration.
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 BlocksFrom ¶
type BlocksFrom struct { DocType string `json:"docType,omitempty"` Global bool `json:"global,omitempty"` Kind BlockKind `json:"kind"` }
BlocksFrom allows a block to borrow definitions for its child blocks from a document type.
type BorrowedBlocks ¶
type BorrowedBlocks struct { Kind BlockKind Source BlockConstraintSet }
BorrowedBlocks wraps a block constraint set that has been borrowed.
func (BorrowedBlocks) BlockConstraints ¶
func (bb BorrowedBlocks) BlockConstraints(kind BlockKind) []*BlockConstraint
BlockConstraints implements the BlockConstraintsSet interface.
type ConstraintMap ¶
type ConstraintMap map[string]StringConstraint
func (ConstraintMap) Requirements ¶
func (cm ConstraintMap) Requirements() string
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 []*BlockConstraint `json:"links,omitempty"` Meta []*BlockConstraint `json:"meta,omitempty"` Content []*BlockConstraint `json:"content,omitempty"` Attributes ConstraintMap `json:"attributes,omitempty"` HTMLPolicies []HTMLPolicy `json:"htmlPolicies,omitempty"` }
func (ConstraintSet) BlockConstraints ¶
func (cs ConstraintSet) BlockConstraints(kind BlockKind) []*BlockConstraint
func (ConstraintSet) Validate ¶
func (cs ConstraintSet) Validate() 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"` }
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) UnmarshalJSON ¶
func (dc *DocumentConstraint) UnmarshalJSON(data []byte) error
type EntityRef ¶
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"` Pattern *Regexp `json:"pattern,omitempty"` Glob GlobList `json:"glob,omitempty"` Format StringFormat `json:"format,omitempty"` Time string `json:"time,omitempty"` HTMLPolicy string `json:"htmlPolicy,omitempty"` }
func (*StringConstraint) Requirement ¶
func (sc *StringConstraint) Requirement() string
func (*StringConstraint) Validate ¶
func (sc *StringConstraint) Validate( value string, exists bool, vCtx *ValidationContext, ) 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" )
func (StringFormat) Describe ¶
func (f StringFormat) Describe() string
type ValidationContext ¶
type ValidationOptionFunc ¶
type ValidationOptionFunc func(vc *ValidationContext)
func WithValueCollector ¶
func WithValueCollector( collector ValueCollector, ) ValidationOptionFunc
type ValidationResult ¶
type ValidationResult struct { Entity []EntityRef `json:"entity,omitempty"` Error string `json:"error,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( document *newsdoc.Document, opts ...ValidationOptionFunc, ) []ValidationResult
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.