Skip to content

Commit f8a8c9f

Browse files
committedDec 23, 2022
increasing test coverage
1 parent a27ed68 commit f8a8c9f

File tree

5 files changed

+170
-120
lines changed

5 files changed

+170
-120
lines changed
 

‎README.md

+8
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,11 @@ example of how a "strictly" typed document can be enforced.
4848
- object
4949
- array
5050
- list
51+
52+
53+
### TODO:
54+
55+
- [] Add Formats from V10 and Gookit Validator
56+
- [] Add Custom Error Types
57+
- [] Support Extending Schemas
58+
- [] Add Benchmarks

‎formats.go

-115
This file was deleted.

‎rules.go

+15
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,21 @@ func Evaluate(property, ruleType string, ruleArg, value interface{}) error {
137137
return fmt.Errorf("%s must contain %v but got %v", property, ruleArg, value)
138138
}
139139

140+
case "startswith":
141+
substr, ok := ruleArg.(string)
142+
if !ok {
143+
return fmt.Errorf("startswith rule must be a string but got %v", ruleArg)
144+
}
145+
146+
str, ok := value.(string)
147+
if !ok {
148+
return fmt.Errorf("%s must be a string but got %v", property, value)
149+
}
150+
151+
if !validate.StartsWith(str, substr) {
152+
return fmt.Errorf("%s must start with %v but got %v", property, substr, value)
153+
}
154+
140155
// TODO: should we support multiple formats for a single property?
141156
case "format":
142157
format, ok := ruleArg.(string)

‎rules_test.go

+79-4
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ func TestEvalRegex(t *testing.T) {
181181
}
182182

183183
// test bad value type
184-
err = jsontype.Evaluate("fake", "noneof", "abc", 123)
184+
err = jsontype.Evaluate("fake", "regex", "abc", 123)
185185
if err == nil {
186186
t.Fatal("expected error")
187187
}
@@ -225,6 +225,33 @@ func TestEvalContaains(t *testing.T) {
225225
}
226226
}
227227

228+
func TestEvalStartsWith(t *testing.T) {
229+
230+
// test bad arg type
231+
err := jsontype.Evaluate("fake", "startswith", 123, "abc")
232+
if err == nil {
233+
t.Fatal("expected error")
234+
}
235+
236+
// test bad value type
237+
err = jsontype.Evaluate("fake", "startswith", "abc", 123)
238+
if err == nil {
239+
t.Fatal("expected error")
240+
}
241+
242+
// test that "abc" starts with "a"
243+
err = jsontype.Evaluate("fake", "startswith", "a", "abc")
244+
if err != nil {
245+
t.Fatal(err)
246+
}
247+
248+
// test that "abc" does not start with "d"
249+
err = jsontype.Evaluate("fake", "startswith", "d", "abc")
250+
if err == nil {
251+
t.Fatal("expected error")
252+
}
253+
}
254+
228255
func TestEvalFormatAlpha(t *testing.T) {
229256
// test that "abc" is alpha
230257
err := jsontype.Evaluate("fake", "format", "alpha", "abc")
@@ -239,9 +266,30 @@ func TestEvalFormatAlpha(t *testing.T) {
239266
}
240267
}
241268

269+
func TestEvalFormatBadInput(t *testing.T) {
270+
// test bad arg type
271+
err := jsontype.Evaluate("fake", "format", 123, "abc")
272+
if err == nil {
273+
t.Fatal("expected error")
274+
}
275+
276+
// test bad value type
277+
err = jsontype.Evaluate("fake", "format", "alpha", 123)
278+
if err == nil {
279+
t.Fatal("expected error")
280+
}
281+
}
282+
242283
func TestEvalFormatAlphaNum(t *testing.T) {
284+
285+
// test bad arg type
286+
err := jsontype.Evaluate("fake", "format", "alphanum", 123)
287+
if err == nil {
288+
t.Fatal("expected error")
289+
}
290+
243291
// test that "abc123" is alphanum
244-
err := jsontype.Evaluate("fake", "format", "alphanum", "abc123")
292+
err = jsontype.Evaluate("fake", "format", "alphanum", "abc123")
245293
if err != nil {
246294
t.Fatal(err)
247295
}
@@ -251,11 +299,24 @@ func TestEvalFormatAlphaNum(t *testing.T) {
251299
if err != nil {
252300
t.Fatal(err)
253301
}
302+
303+
// test bad value type
304+
err = jsontype.Evaluate("fake", "format", "alphanum", "abc-123")
305+
if err == nil {
306+
t.Fatal("expected error")
307+
}
254308
}
255309

256310
func TestEvalFormatAlphaDash(t *testing.T) {
311+
312+
// test bad arg type
313+
err := jsontype.Evaluate("fake", "format", "alphadash", 123)
314+
if err == nil {
315+
t.Fatal("expected error")
316+
}
317+
257318
// test that "abc123" is alphanum
258-
err := jsontype.Evaluate("fake", "format", "alphadash", "abc123")
319+
err = jsontype.Evaluate("fake", "format", "alphadash", "abc123")
259320
if err != nil {
260321
t.Fatal(err)
261322
}
@@ -284,8 +345,15 @@ func TestEvalFormatAlphaDash(t *testing.T) {
284345
}
285346

286347
func TestEvalFormatEmail(t *testing.T) {
348+
349+
// test bad arg type
350+
err := jsontype.Evaluate("fake", "format", "email", 123)
351+
if err == nil {
352+
t.Fatal("expected error")
353+
}
354+
287355
// test that "abc@gmail.com" is email
288-
err := jsontype.Evaluate("fake", "format", "email", "abc@gmail.com")
356+
err = jsontype.Evaluate("fake", "format", "email", "abc@gmail.com")
289357
if err != nil {
290358
t.Fatal(err)
291359
}
@@ -296,3 +364,10 @@ func TestEvalFormatEmail(t *testing.T) {
296364
t.Fatal("expected error")
297365
}
298366
}
367+
368+
func TestBadRule(t *testing.T) {
369+
err := jsontype.Evaluate("fake", "badrule", "abc", "abc")
370+
if err == nil {
371+
t.Fatal("expected error")
372+
}
373+
}

‎schema_test.go

+68-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ func TestSchemaValidate(t *testing.T) {
145145
}
146146

147147
// load our schema into the schema manager
148-
err := sm.LoadSchema([]byte(`{"type": "Person", "properties": {"name": {"type": "string"}}}`))
148+
err := sm.LoadSchema([]byte(`{"type":"Person","properties":{"name":{"type":"string","rules":{"min_length":1,"max_length":100,"format":"alpha"}}}}`))
149149
if err != nil {
150150
t.Fatal(err)
151151
}
@@ -161,6 +161,19 @@ func TestSchemaValidate(t *testing.T) {
161161
if err != nil {
162162
t.Fatal(err)
163163
}
164+
165+
// test bad json
166+
err = schema.Validate([]byte(`{`))
167+
if err == nil {
168+
t.Fatal("expected error")
169+
}
170+
171+
// test rule violation
172+
err = schema.Validate([]byte(`{"name": "1a-"}`))
173+
if err == nil {
174+
t.Fatal("expected error")
175+
}
176+
164177
}
165178

166179
func TestSchemaValidateRequiredProperties(t *testing.T) {
@@ -190,6 +203,60 @@ func TestSchemaValidateRequiredProperties(t *testing.T) {
190203
}
191204
}
192205

206+
func TestSchemaUndefinedProperties(t *testing.T) {
207+
208+
// create our schema manager
209+
sm := jsontype.NewSchemaManager()
210+
if sm == nil {
211+
t.Fatal("failed to create schema manager")
212+
}
213+
214+
// load our schema into the schema manager
215+
err := sm.LoadSchema([]byte(`{"type":"Person","properties":{"name":{"type":"string"},"age":{"type":"number"}}}`))
216+
if err != nil {
217+
t.Fatal(err)
218+
}
219+
220+
// get our schema from the schema manager
221+
schema, err := sm.GetSchema("person")
222+
if err != nil {
223+
t.Fatal(err)
224+
}
225+
226+
// validate our json data against our schema
227+
err = schema.Validate([]byte(`{"name": "John", "age": 30, "address": "123 Main St"}`))
228+
if err == nil {
229+
t.Fatal("expected error")
230+
}
231+
}
232+
233+
func TestSchemaValidatePropertyType(t *testing.T) {
234+
235+
// create our schema manager
236+
sm := jsontype.NewSchemaManager()
237+
if sm == nil {
238+
t.Fatal("failed to create schema manager")
239+
}
240+
241+
// load our schema into the schema manager
242+
err := sm.LoadSchema([]byte(`{"type":"Person","properties":{"name":{"type":"string"},"age":{"type":"number"}}}`))
243+
if err != nil {
244+
t.Fatal(err)
245+
}
246+
247+
// get our schema from the schema manager
248+
schema, err := sm.GetSchema("person")
249+
if err != nil {
250+
t.Fatal(err)
251+
}
252+
253+
// validate our json data against our schema
254+
err = schema.Validate([]byte(`{"name": "John", "age": "30"}`))
255+
if err == nil {
256+
t.Fatal("expected error")
257+
}
258+
}
259+
193260
func TestSchemaToString(t *testing.T) {
194261

195262
// create our schema manager

0 commit comments

Comments
 (0)