Skip to content

Commit

Permalink
validator: support custom dialect in subschema
Browse files Browse the repository at this point in the history
  • Loading branch information
santhosh-tekuri committed Apr 29, 2024
1 parent 2c61c55 commit c599da8
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 19 deletions.
35 changes: 35 additions & 0 deletions draft.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,41 @@ func (d *dialect) hasVocab(name string) bool {
return slices.Contains(d.draft.defaultVocabs, name)
}

func (d *dialect) getSchema() *Schema {
if d.vocabs == nil {
return d.draft.sch
}
// TODO: support custom vocabulary
var allOf []*Schema
for _, vocab := range d.vocabs {
sch := d.draft.allVocabs[vocab]
if sch != nil {
allOf = append(allOf, sch)
}
}
if !slices.Contains(d.vocabs, "core") {
sch := d.draft.allVocabs["core"]
if sch == nil {
sch = d.draft.sch
}
allOf = append(allOf, sch)
}
sch := &Schema{
Location: "urn:mem:metaschema",
up: urlPtr{url("urn:mem:metaschema"), ""},
DraftVersion: d.draft.version,
AllOf: allOf,
}
sch.resource = sch
if sch.DraftVersion >= 2020 {
sch.DynamicAnchor = "meta"
sch.dynamicAnchors = map[string]*Schema{
"meta": sch,
}
}
return sch
}

// --

type ParseIDError struct {
Expand Down
18 changes: 2 additions & 16 deletions root.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,24 +268,10 @@ func (r *root) collectAnchors(sch any, schPtr jsonPointer, res *resource) error
func (r *root) validate(ptr jsonPointer, v any, regexpEngine RegexpEngine) error {
dialect := r.resource(ptr).dialect
up := urlPtr{r.url, ptr}
if dialect.vocabs == nil {
meta := dialect.draft.sch
if err := meta.validate(v, regexpEngine, meta, r.resources); err != nil {
return &SchemaValidationError{URL: up.String(), Err: err}
}
return nil
}

// TODO: validate with vocabs with metaswitch
// validate only with the vocabs listed in metaschema
if err := dialect.draft.allVocabs["core"].validate(v, regexpEngine, nil, nil); err != nil {
meta := dialect.getSchema()
if err := meta.validate(v, regexpEngine, meta, r.resources); err != nil {
return &SchemaValidationError{URL: up.String(), Err: err}
}
for _, vocab := range dialect.vocabs {
if err := dialect.draft.allVocabs[vocab].validate(v, regexpEngine, nil, nil); err != nil {
return &SchemaValidationError{URL: up.String(), Err: err}
}
}
return nil
}

Expand Down
30 changes: 30 additions & 0 deletions testdata/Extra-Test-Suite/tests/draft2020-12/mixed-dialects.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,35 @@
"valid": false
}
]
},
{
"description": "subschema without applicator",
"schema": {
"$defs": {
"one": {
"$id": "http://temp.com/one.json",
"$schema": "http://localhost:1234/draft2020-12/no-applicator.json",
"$comment": "should ignore unevaluatedProperties",
"type": "object",
"allOf": [
1,
{ "type": "number" }
]
}
},
"$ref": "#/$defs/one"
},
"tests": [
{
"description": "valid",
"data": {},
"valid": true
},
{
"description": "invalid",
"data": 1,
"valid": false
}
]
}
]
6 changes: 3 additions & 3 deletions validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ func (vd *validator) objValidate(obj map[string]any) {
sch, meta, resources := s.PropertyNames, vd.meta, vd.resources
res := vd.metaResource(sch)
if res != nil {
meta = res.dialect.draft.sch
meta = res.dialect.getSchema()
sch = meta
}
if err := sch.validate(pname, vd.regexpEngine, meta, resources); err != nil {
Expand Down Expand Up @@ -493,7 +493,7 @@ func (vd *validator) strValidate(str string) {
sch, meta, resources := s.ContentSchema, vd.meta, vd.resources
res := vd.metaResource(sch)
if res != nil {
meta = res.dialect.draft.sch
meta = res.dialect.getSchema()
sch = meta
}
if err = sch.validate(*deserialized, vd.regexpEngine, meta, resources); err != nil {
Expand Down Expand Up @@ -710,7 +710,7 @@ func (vd *validator) handleMeta() {
if res == nil {
return
}
sch := res.dialect.draft.sch
sch := res.dialect.getSchema()
vd.meta = sch
vd.sch = sch
}
Expand Down

0 comments on commit c599da8

Please sign in to comment.