Skip to content

Commit

Permalink
fix: only schema resources can have $schema prop
Browse files Browse the repository at this point in the history
  • Loading branch information
santhosh-tekuri committed Apr 22, 2024
1 parent 9bdf19c commit f7367d6
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
4 changes: 4 additions & 0 deletions draft.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ type Draft struct {
defaultVocabs []string // names of default vocabs
}

func (d *Draft) String() string {
return d.url
}

var (
Draft4 = &Draft{
version: 4,
Expand Down
19 changes: 10 additions & 9 deletions root.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,6 @@ func (r *root) _collectResources(sch any, base url, schPtr jsonPointer) error {
return nil
}

if sch, ok := obj["$schema"]; ok {
if sch, ok := sch.(string); ok && sch != "" {
if got := draftFromURL(sch); got != nil && got != r.draft {
loc := urlPtr{r.url, schPtr}
return &MetaSchemaMismatchError{loc.String()}
}
}
}

var res *resource
if id := r.draft.getID(obj); id != "" {
uf, err := base.join(id)
Expand All @@ -177,6 +168,16 @@ func (r *root) _collectResources(sch any, base url, schPtr jsonPointer) error {
}

if res != nil {
// note: only schema resources can have "$schema"
if sch, ok := obj["$schema"]; ok {
if sch, ok := sch.(string); ok && sch != "" {
if got := draftFromURL(sch); got != nil && got != r.draft {
loc := urlPtr{r.url, schPtr}
return &MetaSchemaMismatchError{loc.String()}
}
}
}

found := false
for _, res := range r.resources {
if res.id == base {
Expand Down
2 changes: 2 additions & 0 deletions testdata/invalid_schemas.json
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@
"$schema": "https://json-schema.org/draft/2020-12/schema#",
"$defs": {
"one": {
"$id": "temp",
"$schema": "https://json-schema.org/draft/2019-09/schema#"
}
}
Expand All @@ -266,6 +267,7 @@
"$schema": "https://json-schema.org/draft/2020-12/schema#",
"$defs": {
"one": {
"$id": "temp",
"$schema": "https://json-schema.org/draft/2020-12/schema"
}
}
Expand Down

0 comments on commit f7367d6

Please sign in to comment.