Skip to content

Commit

Permalink
godoc
Browse files Browse the repository at this point in the history
  • Loading branch information
santhosh-tekuri committed May 17, 2024
1 parent 6972e50 commit 9b27966
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 2 deletions.
14 changes: 13 additions & 1 deletion compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Compiler struct {
mediaTypes map[string]*MediaType
}

// NewCompiler create CompilerObject.
func NewCompiler() *Compiler {
return &Compiler{
schemas: map[urlPtr]*Schema{},
Expand Down Expand Up @@ -94,6 +95,10 @@ func (c *Compiler) RegisterContentMediaType(mt *MediaType) {
}

// RegisterVocabulary registers custom vocabulary.
//
// NOTE:
// - vocabularies are disabled for draft >= 2019-09
// see [Compiler.AssertVocabs]
func (c *Compiler) RegisterVocabulary(vocab *Vocabulary) {
c.roots.vocabularies[vocab.URL] = vocab
}
Expand Down Expand Up @@ -163,7 +168,7 @@ func (c *Compiler) MustCompile(loc string) *Schema {
return sch
}

// Compile compiles json-schema at gven loc.
// Compile compiles json-schema at given loc.
func (c *Compiler) Compile(loc string) (*Schema, error) {
uf, err := absolute(loc)
if err != nil {
Expand Down Expand Up @@ -296,11 +301,18 @@ func (q *queue) get(up urlPtr) *Schema {

// regexp --

// Regexp is the representation of compiled regular expression.
type Regexp interface {
fmt.Stringer

// MatchString reports whether the string s contains
// any match of the regular expression.
MatchString(string) bool
}

// RegexpEngine parses a regular expression and returns,
// if successful, a Regexp object that can be used to
// match against text.
type RegexpEngine func(string) (Regexp, error)

func (re RegexpEngine) validate(v any) error {
Expand Down
5 changes: 4 additions & 1 deletion content.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import (

// Decoder specifies how to decode specific contentEncoding.
type Decoder struct {
Name string
// Name of contentEncoding.
Name string
// Decode given string to byte array.
Decode func(string) ([]byte, error)
}

Expand All @@ -23,6 +25,7 @@ var decoders = map[string]*Decoder{

// MediaType specified how to validate bytes against specific contentMediaType.
type MediaType struct {
// Name of contentMediaType.
Name string

// Validate checks whether bytes conform to this mediatype.
Expand Down
7 changes: 7 additions & 0 deletions draft.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@ import (
"strings"
)

// Position tells possible tokens in json.
type Position uint

const (
// PosProp represents all properties of json object.
PosProp Position = 0
// PosItem represents all items of json array.
PosItem Position = 1
)

// SchemaPosition tells where to look for subschema inside keyword.
type SchemaPosition []Position

func (sp SchemaPosition) collect(v any, ptr jsonPointer, target map[jsonPointer]any) {
Expand All @@ -39,6 +43,7 @@ func (sp SchemaPosition) collect(v any, ptr jsonPointer, target map[jsonPointer]
}
}

// Subschemas tells possible Subschemas for given keyword.
type Subschemas map[string][]SchemaPosition

func (ss Subschemas) collect(obj map[string]any, ptr jsonPointer, target map[jsonPointer]any) {
Expand All @@ -54,6 +59,7 @@ func (ss Subschemas) collect(obj map[string]any, ptr jsonPointer, target map[jso
}
}

// A Draft represents json-schema specification.
type Draft struct {
version int
url string
Expand All @@ -65,6 +71,7 @@ type Draft struct {
defaultVocabs []string // names of default vocabs
}

// String returns the specification url.
func (d *Draft) String() string {
return d.url
}
Expand Down
1 change: 1 addition & 0 deletions format.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

// Format defined specific format.
type Format struct {
// Name of format.
Name string

// Validate checks if given value is of this format.
Expand Down
2 changes: 2 additions & 0 deletions loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type URLLoader interface {

// --

// FileLoader loads json file url.
type FileLoader struct{}

func (l FileLoader) Load(url string) (any, error) {
Expand All @@ -37,6 +38,7 @@ func (l FileLoader) Load(url string) (any, error) {
return UnmarshalJSON(f)
}

// ToFile is helper method to convert file url to file path.
func (l FileLoader) ToFile(url string) (string, error) {
u, err := gourl.Parse(url)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"math/big"
)

// Schema is the regpresentation of a compiled
// jsonschema.
type Schema struct {
up urlPtr
resource *Schema
Expand Down Expand Up @@ -167,6 +169,7 @@ func (jt jsonType) String() string {

// --

// Types encapsulates list of json value types.
type Types int

func newTypes(v any) *Types {
Expand Down

0 comments on commit 9b27966

Please sign in to comment.