Skip to content

Commit 19a6145

Browse files
committed
cleaning up code, fixing test coverage.
1 parent aefcffe commit 19a6145

5 files changed

Lines changed: 318 additions & 77 deletions

File tree

strict/matcher.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,6 @@ func (v *Validator) dataMatchesSchema(schema *base.Schema, data any) (bool, erro
3535
if err != nil {
3636
return false, err
3737
}
38-
if compiled == nil {
39-
return false, nil
40-
}
41-
4238
return compiled.Validate(data) == nil, nil
4339
}
4440

strict/property_collector.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,7 @@ func (v *Validator) collectDeclaredProperties(
5959
continue
6060
}
6161
// trigger property exists, include dependent schema's properties
62-
depProxy := pair.Value()
63-
if depProxy == nil {
64-
continue
65-
}
66-
mergePropertiesIntoDeclared(declared, depProxy.Schema())
62+
mergePropertiesIntoDeclared(declared, pair.Value().Schema())
6763
}
6864
}
6965

strict/utils.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,7 @@ func compilePattern(pattern string) *regexp.Regexp {
126126

127127
b.WriteString("$")
128128

129-
re, err := regexp.Compile(b.String())
130-
if err != nil {
131-
return nil
132-
}
129+
re, _ := regexp.Compile(b.String())
133130
return re
134131
}
135132

strict/utils_test.go

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
// Copyright 2023-2025 Princess Beef Heavy Industries, LLC / Dave Shanley
2+
// SPDX-License-Identifier: MIT
3+
4+
package strict
5+
6+
import (
7+
"testing"
8+
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
func TestCompilePattern_EscapedDoubleAsterisk(t *testing.T) {
13+
// Pattern \*\* should match literal "**" in path (lines 78-80)
14+
// This escapes the glob ** so it matches the literal string **
15+
re := compilePattern(`$.body.\*\*`)
16+
assert.NotNil(t, re)
17+
18+
// Should match literal ** in path
19+
assert.True(t, re.MatchString("$.body.**"))
20+
21+
// Should NOT match arbitrary depth (that's what unescaped ** does)
22+
assert.False(t, re.MatchString("$.body.foo.bar"))
23+
}
24+
25+
func TestCompilePattern_EscapedNonAsterisk(t *testing.T) {
26+
// Pattern with escaped character that's not * (lines 88-90)
27+
// \n should match literal 'n', \. should match literal '.'
28+
re := compilePattern(`$.body\nvalue`)
29+
assert.NotNil(t, re)
30+
31+
// Should match with literal 'n' (the escape just includes the next char)
32+
assert.True(t, re.MatchString("$.bodynvalue"))
33+
}
34+
35+
func TestCompilePattern_EscapedDot(t *testing.T) {
36+
// Escaped dot should be literal dot
37+
re := compilePattern(`$.body\.name`)
38+
assert.NotNil(t, re)
39+
40+
// Should match path with literal dot
41+
assert.True(t, re.MatchString("$.body.name"))
42+
}
43+
44+
func TestCompilePattern_Empty(t *testing.T) {
45+
// Empty pattern returns nil
46+
re := compilePattern("")
47+
assert.Nil(t, re)
48+
}
49+
50+
func TestBuildPath_WithDot(t *testing.T) {
51+
// Property with dot uses bracket notation
52+
result := buildPath("$.body", "a.b")
53+
assert.Equal(t, "$.body['a.b']", result)
54+
}
55+
56+
func TestBuildPath_WithBrackets(t *testing.T) {
57+
// Property with brackets uses bracket notation
58+
result := buildPath("$.body", "x[0]")
59+
assert.Equal(t, "$.body['x[0]']", result)
60+
}
61+
62+
func TestBuildPath_Simple(t *testing.T) {
63+
// Simple property uses dot notation
64+
result := buildPath("$.body", "name")
65+
assert.Equal(t, "$.body.name", result)
66+
}
67+
68+
func TestBuildArrayPath(t *testing.T) {
69+
result := buildArrayPath("$.body.items", 5)
70+
assert.Equal(t, "$.body.items[5]", result)
71+
}
72+
73+
func TestCompileIgnorePaths_Empty(t *testing.T) {
74+
result := compileIgnorePaths(nil)
75+
assert.Nil(t, result)
76+
77+
result = compileIgnorePaths([]string{})
78+
assert.Nil(t, result)
79+
}
80+
81+
func TestCompileIgnorePaths_WithPatterns(t *testing.T) {
82+
patterns := []string{
83+
"$.body.metadata",
84+
"$.body.items[*].internal",
85+
}
86+
result := compileIgnorePaths(patterns)
87+
assert.Len(t, result, 2)
88+
}
89+
90+
func TestTruncateValue_LongString(t *testing.T) {
91+
// String > 50 chars gets truncated
92+
longStr := "this is a very long string that exceeds fifty characters in length"
93+
result := TruncateValue(longStr)
94+
assert.Equal(t, "this is a very long string that exceeds fifty c...", result)
95+
}
96+
97+
func TestTruncateValue_ShortString(t *testing.T) {
98+
shortStr := "short"
99+
result := TruncateValue(shortStr)
100+
assert.Equal(t, "short", result)
101+
}
102+
103+
func TestTruncateValue_LargeMap(t *testing.T) {
104+
// Map with > 3 keys shows {...}
105+
m := map[string]any{"a": 1, "b": 2, "c": 3, "d": 4}
106+
result := TruncateValue(m)
107+
assert.Equal(t, "{...}", result)
108+
}
109+
110+
func TestTruncateValue_SmallMap(t *testing.T) {
111+
m := map[string]any{"a": 1, "b": 2}
112+
result := TruncateValue(m)
113+
assert.Equal(t, m, result)
114+
}
115+
116+
func TestTruncateValue_LargeSlice(t *testing.T) {
117+
// Slice with > 3 elements shows [...]
118+
s := []any{1, 2, 3, 4}
119+
result := TruncateValue(s)
120+
assert.Equal(t, "[...]", result)
121+
}
122+
123+
func TestTruncateValue_SmallSlice(t *testing.T) {
124+
s := []any{1, 2}
125+
result := TruncateValue(s)
126+
assert.Equal(t, s, result)
127+
}
128+
129+
func TestTruncateValue_OtherTypes(t *testing.T) {
130+
// Other types returned as-is
131+
assert.Equal(t, 42, TruncateValue(42))
132+
assert.Equal(t, true, TruncateValue(true))
133+
assert.Equal(t, 3.14, TruncateValue(3.14))
134+
}

0 commit comments

Comments
 (0)