Skip to content

Commit

Permalink
pkg/generator: support validation regex literals (#295)
Browse files Browse the repository at this point in the history
Often, regular expressions used for validating strings will contain
complex escape sequences. Once we read those in during schema ingest,
we will have the escaped values, but we cannot paste those into a
double-quote string in Go as we would need to re-escape the sequences.
If we instead use a back-tick string, we can use the regular expressions
verbatim.

Signed-off-by: Steve Kuznetsov <[email protected]>
  • Loading branch information
stevekuznetsov authored Oct 30, 2024
1 parent 6f65c77 commit 2faccef
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ golang-install-tools:
.PHONY: tools-go tools-brew

tools-go:
@scripts/tools-golang.sh
@scripts/golang-install-tools.sh

tools-brew:
@scripts/tools-brew.sh
Expand Down
10 changes: 8 additions & 2 deletions pkg/generator/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,15 @@ func (v *stringValidator) generate(out *codegen.Emitter) {
out.Indent(1)
}

out.Printlnf(`if matched, _ := regexp.MatchString("%s", string(%s%s)); !matched {`, v.pattern, pointerPrefix, value)
out.Printlnf(
`if matched, _ := regexp.MatchString(`+"`%s`"+`, string(%s%s)); !matched {`,
v.pattern, pointerPrefix, value,
)
out.Indent(1)
out.Printlnf(`return fmt.Errorf("field %%s pattern match: must match %%s", "%s", "%s")`, v.pattern, v.fieldName)
out.Printlnf(
`return fmt.Errorf("field %%s pattern match: must match %%s", `+"`%s`"+`, "%s")`,
v.pattern, v.fieldName,
)
out.Indent(-1)
out.Printlnf("}")

Expand Down
8 changes: 4 additions & 4 deletions tests/data/validation/pattern/pattern.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/data/validation/pattern/pattern.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"properties": {
"myString": {
"type": "string",
"pattern": "^0x[0-9a-f]{10}$"
"pattern": "^0x[0-9a-f]{10}\\.$"
},
"myNullableString": {
"type": "string",
Expand Down
6 changes: 6 additions & 0 deletions tests/generation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"strings"
"testing"

"github.com/google/go-cmp/cmp"

"github.com/atombender/go-jsonschema/pkg/generator"
)

Expand Down Expand Up @@ -250,6 +252,10 @@ func testExampleFile(t *testing.T, cfg generator.Config, fileName string) {
}
}

if diff := cmp.Diff(string(goldenData), string(source)); diff != "" {
t.Errorf("Contents different (left is expected, right is actual):\n%s", diff)
}

if diff, ok := diffStrings(t, string(goldenData), string(source)); !ok {
t.Fatalf("Contents different (left is expected, right is actual):\n%s", *diff)
}
Expand Down
4 changes: 2 additions & 2 deletions tests/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,12 @@ func TestPattern(t *testing.T) {
}{
{
desc: "no violations",
data: `{"myString": "0x12345abcde"}`,
data: `{"myString": "0x12345abcde."}`,
},
{
desc: "myString does not match pattern",
data: `{"myString": "0x123456"}`,
wantErr: errors.New("field ^0x[0-9a-f]{10}$ pattern match: must match MyString"),
wantErr: errors.New("field ^0x[0-9a-f]{10}\\.$ pattern match: must match MyString"),
},
}

Expand Down

0 comments on commit 2faccef

Please sign in to comment.