Skip to content

Commit

Permalink
amended validation rule so that there is an explanation, rather than …
Browse files Browse the repository at this point in the history
…using the error messages, and show the valid enum values in the docs
  • Loading branch information
eviltester committed Apr 13, 2024
1 parent 2f09c34 commit bd536f2
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ title: API Challenges Simple API
description: The Simple API is a multi-user REST API that you can use to practice testing without any authentication.
---

The API Challenges Simple API is an easy-to-use API where you can GET, DELETE, PUT and POST without any authentication.


# Simple API

The API Challenges Simple API is an easy-to-use API where you can GET, DELETE, PUT and POST without any authentication.

## About Simple API

To help you get started with API testing and practice using your tools, we have created the Simple API.

The Simple API has a single end point `/simpleapi/items` and you can `GET`, `DELETE`, `PUT` and `POST` without
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,11 @@ public boolean validates(final FieldValue value) {
public String getErrorMessage(final FieldValue value) {
return String.format("%s does not satisfy the regex %s",value.getName(), this.regexToMatch);
}

@Override
public String getExplanation() {
return String.format("Value must contain text that matches the regex %s", this.regexToMatch);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,9 @@ public boolean validates(final FieldValue value) {
public String getErrorMessage(final FieldValue value) {
return String.format("%s does not match the regex %s",value.getName(), this.regexToMatch);
}

@Override
public String getExplanation() {
return String.format("Value must match the regex %s", this.regexToMatch);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,9 @@ public String getErrorMessage(final FieldValue value) {
"Maximum allowable length exceeded for %s - maximum allowed is %d",
value.getName(), maxLength);
}

@Override
public String getExplanation() {
return "Maximum length allowed is %d".formatted(maxLength);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,9 @@ public boolean validates(final FieldValue value) {
public String getErrorMessage(final FieldValue value) {
return String.format("%s : can not be empty", value.getName());
}

@Override
public String getExplanation() {
return "Value can not be empty";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ public interface ValidationRule {
boolean validates(FieldValue value);

String getErrorMessage(FieldValue value);

String getExplanation();
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Collection;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

public class RestApiDocumentationGenerator {
private final Thingifier thingifier;
Expand Down Expand Up @@ -183,18 +184,23 @@ public String getApiDocumentation(final ApiRoutingDefinition routingDefinitions,
output.append("<ul>");
for (ValidationRule validation : theField.validationRules()) {
//use the validation error message in the documentation
output.append("<li>" + validation.getErrorMessage(theField.valueFor("")) + "</li>\n");
output.append("<li>" + validation.getExplanation() + "</li>\n");
}

output.append(String.format("<li>Mandatory?: %b</li>", theField.isMandatory()));

if(theField.getType()== FieldType.ENUM){
String allowedValues = theField.getExamples().stream().collect(Collectors.joining(", "));
output.append(String.format("<li>Allowed Values: %s </li>", allowedValues));
}

if(theField.getType()== FieldType.INTEGER){
output.append(String.format("<li>Values Between: \"%d\" to \"%d\" </li>",
output.append(String.format("<li>Allowed Values Between: \"%d\" to \"%d\" </li>",
theField.getMinimumIntegerValue(), theField.getMaximumIntegerValue()));
}

if(theField.getType()== FieldType.FLOAT){
output.append(String.format("<li>Values Between: \"%f\" to \"%f\" </li>",
output.append(String.format("<li>Allowed Values Between: \"%f\" to \"%f\" </li>",
theField.getMinimumFloatValue(), theField.getMaximumFloatValue()));
}

Expand Down

0 comments on commit bd536f2

Please sign in to comment.