Validator for Pet forms.
* - * We're not using Bean Validation annotations here because it is easier to define such + * We're not using Bean Validation annotations here because it is easier to + * define such * validation rule in Java. *
* @@ -38,9 +39,23 @@ public class PetValidator implements Validator { Pet pet = (Pet) obj; String name = pet.getName(); // name validation + if (!StringUtils.hasText(name)) { errors.rejectValue("name", REQUIRED, REQUIRED); } + // Edge Case: Check for leading/trailing spaces if trimmed but not null + else if (!name.equals(name.trim())) { + errors.rejectValue("name", "leadingTrailingSpace", "cannot start or end with spaces"); + } + // Edge Case: Max length + else if (name.length() > 30) { + errors.rejectValue("name", "maxLength", "must be 30 characters or less"); + } + // Edge Case: Symbols/Special Chars + else if (!name.matches("^[a-zA-Z0-9\\s'-]+$")) { + errors.rejectValue("name", "invalidCharacters", + "contains invalid characters (only letters, numbers, spaces, hyphens allowed)"); + } // type validation if (pet.isNew() && pet.getType() == null) { diff --git a/src/main/java/org/springframework/samples/petclinic/system/OpenApiConfiguration.java b/src/main/java/org/springframework/samples/petclinic/system/OpenApiConfiguration.java new file mode 100644 index 000000000..3b27e0eaa --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/system/OpenApiConfiguration.java @@ -0,0 +1,20 @@ +package org.springframework.samples.petclinic.system; + +import io.swagger.v3.oas.models.OpenAPI; +import io.swagger.v3.oas.models.info.Info; +import io.swagger.v3.oas.models.info.License; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class OpenApiConfiguration { + + @Bean + public OpenAPI petClinicOpenAPI() { + return new OpenAPI().info(new Info().title("Spring PetClinic API") + .description("Spring PetClinic sample application") + .version("v0.0.1") + .license(new License().name("Apache 2.0").url("http://springdoc.org"))); + } + +} diff --git a/src/main/resources/templates/owners/ownersList.html b/src/main/resources/templates/owners/ownersList.html index 01223c1c5..adf430c8b 100644 --- a/src/main/resources/templates/owners/ownersList.html +++ b/src/main/resources/templates/owners/ownersList.html @@ -9,15 +9,18 @@| ID | Name | Address | City | Telephone | Pets | +Pet Count |
|---|---|---|---|---|---|---|
| @@ -25,6 +28,7 @@ | + |