Skip to content

Commit

Permalink
validation/fix: escape % in Schema.enumError
Browse files Browse the repository at this point in the history
  • Loading branch information
davidjb authored Feb 9, 2024
1 parent d265bc9 commit 460fe6f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ func (s *Schema) validate(scope []schemaRef, vscope int, spath string, v interfa
}
}
if !matched {
errors = append(errors, validationError("enum", s.enumError))
errors = append(errors, validationError("enum", "%s", s.enumError))
}
}

Expand Down
16 changes: 16 additions & 0 deletions schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,22 @@ func TestInvalidJsonTypeError(t *testing.T) {
}
}

func TestPercentInEnumError(t *testing.T) {
compiler := jsonschema.NewCompiler()
err := compiler.AddResource("test.json", strings.NewReader(`{"type": "string", "enum": ["%"]}`))
if err != nil {
t.Fatalf("addResource failed. reason: %v\n", err)
}
schema, err := compiler.Compile("test.json")
if err != nil {
t.Fatalf("schema compilation failed. reason: %v\n", err)
}
err = schema.Validate("hello world")
if strings.Contains(err.Error(), `"%!"(MISSING)`) {
t.Fatalf(`error contains "%%!"(MISSING)`)
}
}

func TestInfiniteLoopError(t *testing.T) {
t.Run("compile", func(t *testing.T) {
compiler := jsonschema.NewCompiler()
Expand Down

0 comments on commit 460fe6f

Please sign in to comment.