package jsonschema
import (
"fmt"
"regexp"
"slices"
)
type Compiler struct {
schemas map [urlPtr ]*Schema
roots *roots
formats map [string ]*Format
decoders map [string ]*Decoder
mediaTypes map [string ]*MediaType
assertFormat bool
assertContent bool
}
func NewCompiler () *Compiler {
return &Compiler {
schemas : map [urlPtr ]*Schema {},
roots : newRoots (),
formats : map [string ]*Format {},
decoders : map [string ]*Decoder {},
mediaTypes : map [string ]*MediaType {},
assertFormat : false ,
assertContent : false ,
}
}
func (c *Compiler ) DefaultDraft (d *Draft ) {
c .roots .defaultDraft = d
}
func (c *Compiler ) AssertFormat () {
c .assertFormat = true
}
func (c *Compiler ) AssertContent () {
c .assertContent = true
}
func (c *Compiler ) RegisterFormat (f *Format ) {
if f .Name != "regex" {
c .formats [f .Name ] = f
}
}
func (c *Compiler ) RegisterContentEncoding (d *Decoder ) {
c .decoders [d .Name ] = d
}
func (c *Compiler ) RegisterContentMediaType (mt *MediaType ) {
c .mediaTypes [mt .Name ] = mt
}
func (c *Compiler ) RegisterVocabulary (vocab *Vocabulary ) {
c .roots .vocabularies [vocab .URL ] = vocab
}
func (c *Compiler ) AssertVocabs () {
c .roots .assertVocabs = true
}
func (c *Compiler ) AddResource (url string , doc any ) error {
uf , err := absolute (url )
if err != nil {
return err
}
if isMeta (string (uf .url )) {
return &ResourceExistsError {string (uf .url )}
}
if !c .roots .loader .add (uf .url , doc ) {
return &ResourceExistsError {string (uf .url )}
}
return nil
}
func (c *Compiler ) UseLoader (loader URLLoader ) {
c .roots .loader .loader = loader
}
func (c *Compiler ) UseRegexpEngine (engine RegexpEngine ) {
if engine == nil {
engine = goRegexpCompile
}
c .roots .regexpEngine = engine
}
func (c *Compiler ) enqueue (q *queue , up urlPtr ) *Schema {
if sch , ok := c .schemas [up ]; ok {
return sch
}
if sch := q .get (up ); sch != nil {
return sch
}
sch := newSchema (up )
q .append (sch )
return sch
}
func (c *Compiler ) MustCompile (loc string ) *Schema {
sch , err := c .Compile (loc )
if err != nil {
panic (fmt .Sprintf ("jsonschema: Compile(%q): %v" , loc , err ))
}
return sch
}
func (c *Compiler ) Compile (loc string ) (*Schema , error ) {
uf , err := absolute (loc )
if err != nil {
return nil , err
}
up , err := c .roots .resolveFragment (*uf )
if err != nil {
return nil , err
}
return c .doCompile (up )
}
func (c *Compiler ) doCompile (up urlPtr ) (*Schema , error ) {
q := &queue {}
compiled := 0
c .enqueue (q , up )
for q .len () > compiled {
sch := q .at (compiled )
if err := c .roots .ensureSubschema (sch .up ); err != nil {
return nil , err
}
r := c .roots .roots [sch .up .url ]
v , err := sch .up .lookup (r .doc )
if err != nil {
return nil , err
}
if err := c .compileValue (v , sch , r , q ); err != nil {
return nil , err
}
compiled ++
}
for _ , sch := range *q {
c .schemas [sch .up ] = sch
}
return c .schemas [up ], nil
}
func (c *Compiler ) compileValue (v any , sch *Schema , r *root , q *queue ) error {
res := r .resource (sch .up .ptr )
sch .DraftVersion = res .dialect .draft .version
base := urlPtr {sch .up .url , res .ptr }
sch .resource = c .enqueue (q , base )
if sch .DraftVersion >= 2020 && sch .up == sch .resource .up {
res := r .resource (sch .up .ptr )
for anchor , anchorPtr := range res .anchors {
if slices .Contains (res .dynamicAnchors , anchor ) {
up := urlPtr {sch .up .url , anchorPtr }
danchorSch := c .enqueue (q , up )
if sch .dynamicAnchors == nil {
sch .dynamicAnchors = map [string ]*Schema {}
}
sch .dynamicAnchors [string (anchor )] = danchorSch
}
}
}
switch v := v .(type ) {
case bool :
sch .Bool = &v
case map [string ]any :
if err := c .compileObject (v , sch , r , q ); err != nil {
return err
}
}
sch .allPropsEvaluated = sch .AdditionalProperties != nil
if sch .DraftVersion < 2020 {
sch .allItemsEvaluated = sch .AdditionalItems != nil
switch items := sch .Items .(type ) {
case *Schema :
sch .allItemsEvaluated = true
case []*Schema :
sch .numItemsEvaluated = len (items )
}
} else {
sch .allItemsEvaluated = sch .Items2020 != nil
sch .numItemsEvaluated = len (sch .PrefixItems )
}
return nil
}
func (c *Compiler ) compileObject (obj map [string ]any , sch *Schema , r *root , q *queue ) error {
if len (obj ) == 0 {
b := true
sch .Bool = &b
return nil
}
oc := objCompiler {
c : c ,
obj : obj ,
up : sch .up ,
r : r ,
res : r .resource (sch .up .ptr ),
q : q ,
}
return oc .compile (sch )
}
type queue []*Schema
func (q *queue ) append (sch *Schema ) {
*q = append (*q , sch )
}
func (q *queue ) at (i int ) *Schema {
return (*q )[i ]
}
func (q *queue ) len () int {
return len (*q )
}
func (q *queue ) get (up urlPtr ) *Schema {
i := slices .IndexFunc (*q , func (sch *Schema ) bool { return sch .up == up })
if i != -1 {
return (*q )[i ]
}
return nil
}
type Regexp interface {
fmt .Stringer
MatchString (string ) bool
}
type RegexpEngine func (string ) (Regexp , error )
func (re RegexpEngine ) validate (v any ) error {
s , ok := v .(string )
if !ok {
return nil
}
_ , err := re (s )
return err
}
func goRegexpCompile(s string ) (Regexp , error ) {
return regexp .Compile (s )
}
The pages are generated with Golds v0.8.7 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .