Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 29 additions & 11 deletions validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,18 +285,15 @@ func (vd *validator) objValidate(obj map[string]any) {
// propertyNames --
if s.PropertyNames != nil {
for pname := range obj {
sch, meta, resources := s.PropertyNames, vd.meta, vd.resources
res := vd.metaResource(sch)
if res != nil {
meta = res.dialect.getSchema(vd.assertVocabs, vd.vocabularies)
sch = meta
}
if err := sch.validate(pname, vd.regexpEngine, meta, resources, vd.assertVocabs, vd.vocabularies); err != nil {
if err := vd.validatePropertyName(s.PropertyNames, pname); err != nil {
verr := err.(*ValidationError)
verr.InstanceLocation = vd.vloc
verr.SchemaURL = s.PropertyNames.Location
verr.ErrorKind = &kind.PropertyNames{Property: pname}
vd.addErr(verr)
var causes []*ValidationError
if _, ok := verr.ErrorKind.(*kind.Group); ok {
causes = verr.Causes
} else {
causes = []*ValidationError{verr}
}
vd.addErrors(causes, &kind.PropertyNames{Property: pname})
}
}
}
Expand Down Expand Up @@ -704,6 +701,27 @@ func (vd *validator) validateVal(sch *Schema, v any, vtok string) error {
return err
}

func (vd *validator) validatePropertyName(sch *Schema, pname string) error {
scp := vd.scp.child(sch, "", vd.scp.vid+1)
subvd := validator{
v: pname,
vloc: vd.vloc,
sch: sch,
scp: scp,
uneval: unevalFrom(pname, sch, false),
errors: nil,
boolResult: vd.boolResult,
regexpEngine: vd.regexpEngine,
meta: vd.meta,
resources: vd.resources,
assertVocabs: vd.assertVocabs,
vocabularies: vd.vocabularies,
}
subvd.handleMeta()
_, err := subvd.validate()
return err
}

func (vd *validator) validateValue(sch *Schema, v any, vpath []string) error {
vloc := append(vd.vloc, vpath...)
scp := vd.scp.child(sch, "", vd.scp.vid+1)
Expand Down
54 changes: 54 additions & 0 deletions validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,57 @@ func TestInvalidFloats(t *testing.T) {
}
}
}

func TestPropertyNamesLocation(t *testing.T) {
c := jsonschema.NewCompiler()
doc, err := jsonschema.UnmarshalJSON(strings.NewReader(`{
"type": "object",
"properties": {
"foo": {
"type": "object",
"propertyNames": {"pattern": "^[a-z]+$"}
}
}
}`))
if err != nil {
t.Fatal(err)
}
if err := c.AddResource("schema.json", doc); err != nil {
t.Fatal(err)
}
sch, err := c.Compile("schema.json")
if err != nil {
t.Fatal(err)
}

err = sch.Validate(map[string]any{
"foo": map[string]any{"BAR": "baz"},
})
if err == nil {
t.Fatal("Validate() = nil, want error")
}

out := err.(*jsonschema.ValidationError).BasicOutput()

want := map[string]string{
"/properties/foo/propertyNames": "/foo",
"/properties/foo/propertyNames/pattern": "/foo",
}
got := map[string]string{}
for _, u := range out.Errors {
got[u.KeywordLocation] = u.InstanceLocation
}
for kwLoc, instLoc := range want {
actual, ok := got[kwLoc]
if !ok {
t.Errorf("missing unit with keywordLocation %q; got units %v", kwLoc, got)
continue
}
if actual != instLoc {
t.Errorf("keywordLocation %q: instanceLocation = %q, want %q", kwLoc, actual, instLoc)
}
}
if _, doubled := got["/properties/foo/propertyNames/propertyNames"]; doubled {
t.Errorf("keywordLocation has duplicated /propertyNames; got units %v", got)
}
}