Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding sample models. Refactored tests.
- Loading branch information
1 parent
057c493
commit 8bc94cf
Showing
8 changed files
with
209 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/main/java/com/github/vickumar1981/svalidate/util/Validation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.github.vickumar1981.svalidate.util; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.function.Supplier; | ||
import java.util.stream.Collectors; | ||
|
||
public class Validation<T> { | ||
|
||
private List<T> exceptions; | ||
|
||
private Validation(List<T> errors) { | ||
this.exceptions = errors; | ||
} | ||
|
||
public List<T> errors() { | ||
return exceptions; | ||
} | ||
|
||
public Validation<T> append(Validation<T> other) { | ||
this.exceptions.addAll(other.errors()); | ||
return this; | ||
} | ||
|
||
public Validation<T> andThen(Supplier<Validation<T>> other) { | ||
if (isSuccess()) { | ||
return other.get(); | ||
} | ||
return this; | ||
} | ||
|
||
public boolean isSuccess() { | ||
return exceptions.isEmpty(); | ||
} | ||
|
||
public boolean isFailure() { | ||
return !isSuccess(); | ||
} | ||
|
||
public static <A> Validation fail(A ...value) { | ||
return new Validation<>( | ||
Arrays.stream(value).collect(Collectors.toList()) | ||
); | ||
} | ||
|
||
public static <A> Validation success() { | ||
return new Validation<>(Collections.emptyList()); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/com/github/vickumar1981/svalidate/util/ValidationSyntax.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.github.vickumar1981.svalidate.util; | ||
|
||
import java.util.Optional; | ||
import java.util.function.Function; | ||
|
||
public class ValidationSyntax { | ||
public static <T> Function<Boolean, Validation<T>> orElse(T ...errors) { | ||
return (cond) -> { | ||
if (!cond) { | ||
return Validation.fail(errors); | ||
} else { | ||
return Validation.success(); | ||
} | ||
}; | ||
} | ||
|
||
public static <A> Function<Optional<A>, Validation<A>> errorIfEmpty(A ...errors) { | ||
return (validatable) -> validatable.map(v -> Validation.success()).orElse(Validation.fail(errors)); | ||
} | ||
|
||
public static <A> Function<Optional<A>, Validation<A>> errorIfDefined(A ...errors) { | ||
return (validatable) -> validatable.map(v -> Validation.fail(errors)).orElse(Validation.success()); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/main/java/com/github/vickumar1981/svalidate/util/example/model/Address.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.github.vickumar1981.svalidate.util.example.model; | ||
|
||
import com.github.vickumar1981.svalidate.util.Validation; | ||
|
||
import static com.github.vickumar1981.svalidate.util.ValidationSyntax.orElse; | ||
|
||
public class Address implements Validatable<String> { | ||
private String street; | ||
private String city; | ||
private String state; | ||
private String zipCode; | ||
|
||
public Address(String street, String city, String state, String zipCode) { | ||
this.street = street; | ||
this.city = city; | ||
this.state = state; | ||
this.zipCode = zipCode; | ||
} | ||
|
||
public Validation<String> validate() { | ||
return orElse("Zip code must be 5 digits").apply(zipCode.matches("\\d{5}")).append( | ||
orElse("State abbr must be 2 letters").apply(state.matches("[A-Z]{2}")) | ||
); | ||
} | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/com/github/vickumar1981/svalidate/util/example/model/Validatable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.github.vickumar1981.svalidate.util.example.model; | ||
|
||
import com.github.vickumar1981.svalidate.util.Validation; | ||
|
||
public interface Validatable<T> { | ||
Validation<T> validate(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import com.github.javafaker.Faker | ||
import com.github.vickumar1981.svalidate.{Validatable, ValidatableWith} | ||
import com.github.vickumar1981.svalidate.example.model.{Address, ContactSettings, Contacts, Person} | ||
|
||
import scala.util.Random | ||
|
||
object TestFixtures { | ||
private final val zipCodeLength = 5 | ||
private final val phoneNumberLength = 10 | ||
private final val maxContacts = 10 | ||
private val faker = new Faker() | ||
|
||
case class BlankObject() | ||
|
||
implicit object BlankObjectValidator extends Validatable[BlankObject] | ||
implicit object BlankObjectValidatorWith extends ValidatableWith[BlankObject, String] | ||
|
||
def validAddress: Address = Address( | ||
faker.address.streetAddress, | ||
faker.address.city, | ||
faker.address.stateAbbr, | ||
faker.address.zipCode.take(zipCodeLength)) | ||
|
||
def validPerson: Person = Person( | ||
faker.name.firstName, | ||
faker.name.lastName, | ||
true, | ||
Some(validAddress), | ||
Some(Random.alphanumeric.filter(_.isDigit).take(phoneNumberLength).mkString)) | ||
|
||
def mkContactList: List[String] = (1 to Random.nextInt(maxContacts)) | ||
.toList.map { _ => faker.internet().emailAddress() } | ||
|
||
def validContactInfo: Contacts = Contacts( | ||
Some(mkContactList), | ||
Some(mkContactList)) | ||
|
||
def invalidContactAndSettings: (Contacts, ContactSettings) = { | ||
val generateTrueFalse = () => Random.nextInt(maxContacts) % 2 | ||
val hasContacts = (x: Int) => if (x == 1) Some(true) else Some(false) | ||
val hasFacebook = hasContacts(generateTrueFalse()) | ||
val hasTwitter = hasContacts(generateTrueFalse()) | ||
val contacts = Contacts( | ||
if (hasFacebook.getOrElse(true)) None else Some(mkContactList), | ||
if (hasTwitter.getOrElse(true)) None else Some(mkContactList) | ||
) | ||
(contacts, ContactSettings(hasFacebook, hasTwitter)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters