Skip to content

Commit

Permalink
Add MatchesJsonSchema
Browse files Browse the repository at this point in the history
  • Loading branch information
walkerus committed Oct 22, 2024
1 parent 04be624 commit 25bb1a1
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 10 deletions.
23 changes: 23 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,29 @@ func TestStubRule_ToJson(t *testing.T) {
WillReturnResponse(OK()),
ExpectedFileName: "not-logical-expression.json",
},
{
Name: "JsonSchemaMatcher",
StubRule: Post(URLPathEqualTo("/example")).
WithQueryParam("firstName", MatchesJsonSchema(
`{
"type": "object",
"required": [
"name"
],
"properties": {
"name": {
"type": "string"
},
"tag": {
"type": "string"
}
}
}`,
"V202012",
)).
WillReturnResponse(OK()),
ExpectedFileName: "matches-Json-schema.json",
},
}

for _, tc := range testCases {
Expand Down
21 changes: 11 additions & 10 deletions matching.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ package wiremock

// Types of params matching.
const (
ParamEqualTo ParamMatchingStrategy = "equalTo"
ParamMatches ParamMatchingStrategy = "matches"
ParamContains ParamMatchingStrategy = "contains"
ParamEqualToXml ParamMatchingStrategy = "equalToXml"
ParamEqualToJson ParamMatchingStrategy = "equalToJson"
ParamMatchesXPath ParamMatchingStrategy = "matchesXPath"
ParamMatchesJsonPath ParamMatchingStrategy = "matchesJsonPath"
ParamAbsent ParamMatchingStrategy = "absent"
ParamDoesNotMatch ParamMatchingStrategy = "doesNotMatch"
ParamDoesNotContains ParamMatchingStrategy = "doesNotContain"
ParamEqualTo ParamMatchingStrategy = "equalTo"
ParamMatches ParamMatchingStrategy = "matches"
ParamContains ParamMatchingStrategy = "contains"
ParamEqualToXml ParamMatchingStrategy = "equalToXml"
ParamEqualToJson ParamMatchingStrategy = "equalToJson"
ParamMatchesXPath ParamMatchingStrategy = "matchesXPath"
ParamMatchesJsonPath ParamMatchingStrategy = "matchesJsonPath"
ParamAbsent ParamMatchingStrategy = "absent"
ParamDoesNotMatch ParamMatchingStrategy = "doesNotMatch"
ParamDoesNotContains ParamMatchingStrategy = "doesNotContain"
ParamMatchesJsonSchema ParamMatchingStrategy = "matchesJsonSchema"
)

// Types of url matching.
Expand Down
22 changes: 22 additions & 0 deletions string_value_matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,28 @@ func StartsWith(prefix string) BasicParamMatcher {
return NewStringValueMatcher(ParamMatches, regex)
}

type JSONSchemaMatcher struct {
StringValueMatcher
schemaVersion string
}

// MatchesJsonSchema returns a matcher that matches when the parameter matches the specified JSON schema.
// Required wiremock version >= 3.0.0
func MatchesJsonSchema(schema string, schemaVersion string) BasicParamMatcher {
return JSONSchemaMatcher{
StringValueMatcher: NewStringValueMatcher(ParamMatchesJsonSchema, schema),
schemaVersion: schemaVersion,
}
}

// MarshalJSON returns the JSON encoding of the matcher.
func (m JSONSchemaMatcher) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]string{
string(m.strategy): m.value,
"schemaVersion": m.schemaVersion,
})
}

func regexContainsStartAnchor(regex string) bool {
return len(regex) > 0 && regex[0] == '^'
}
17 changes: 17 additions & 0 deletions testdata/matches-Json-schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"uuid": "%s",
"id": "%s",
"request": {
"method": "POST",
"urlPath": "/example",
"queryParameters": {
"firstName": {
"matchesJsonSchema" : "{\n \"type\": \"object\",\n \"required\": [\n \"name\"\n ],\n \"properties\": {\n \"name\": {\n \"type\": \"string\"\n },\n \"tag\": {\n \"type\": \"string\"\n }\n }\n}",
"schemaVersion" : "V202012"
}
}
},
"response": {
"status": 200
}
}

0 comments on commit 25bb1a1

Please sign in to comment.