-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
48b2045
commit ad72f26
Showing
5 changed files
with
101 additions
and
4 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
...ns/src/main/java/com/github/jeffnelson/jackson/patch/validator/PatchNotNullValidator.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,25 @@ | ||
|
||
package com.github.jeffnelson.jackson.patch.validator; | ||
|
||
import javax.validation.ConstraintValidator; | ||
import javax.validation.ConstraintValidatorContext; | ||
|
||
import com.github.jeffnelson.jackson.patch.PatchField; | ||
import com.github.jeffnelson.jackson.patch.validator.constraints.PatchNotNull; | ||
|
||
public class PatchNotNullValidator implements ConstraintValidator<PatchNotNull,PatchField<?>> { | ||
|
||
@Override | ||
public void initialize(PatchNotNull constraintAnnotation) { | ||
// noop | ||
} | ||
|
||
@Override | ||
public boolean isValid(PatchField<?> value, ConstraintValidatorContext context) { | ||
if (value.shouldPatch()) { | ||
return value.getValue() != null; | ||
} | ||
return true; | ||
} | ||
|
||
} |
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
34 changes: 34 additions & 0 deletions
34
...src/main/java/com/github/jeffnelson/jackson/patch/validator/constraints/PatchNotNull.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,34 @@ | ||
|
||
package com.github.jeffnelson.jackson.patch.validator.constraints; | ||
|
||
import static java.lang.annotation.ElementType.FIELD; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
|
||
import javax.validation.Constraint; | ||
import javax.validation.Payload; | ||
|
||
import com.github.jeffnelson.jackson.patch.PatchField; | ||
import com.github.jeffnelson.jackson.patch.validator.PatchNotNullValidator; | ||
|
||
/** | ||
* Annotation to apply validation to {@literal PatchField<String>} | ||
* <p> | ||
* Default behavior. If {@link PatchField#shouldPatch()} returns true, value cannot be null | ||
* | ||
* @author jeff.nelson | ||
* | ||
*/ | ||
@Constraint(validatedBy = PatchNotNullValidator.class) | ||
@Retention(RUNTIME) | ||
@Target(FIELD) | ||
public @interface PatchNotNull { | ||
|
||
String message() default "Required"; | ||
|
||
Class<?>[] groups() default {}; | ||
|
||
Class<? extends Payload>[] payload() default {}; | ||
} |
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
38 changes: 38 additions & 0 deletions
38
...rc/test/java/com/github/jeffnelson/jackson/patch/validator/PatchNotNullValidatorTest.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,38 @@ | ||
|
||
package com.github.jeffnelson.jackson.patch.validator; | ||
|
||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import com.github.jeffnelson.jackson.patch.PatchField; | ||
|
||
public class PatchNotNullValidatorTest { | ||
|
||
private PatchNotNullValidator validator; | ||
|
||
@Before | ||
public void setup() { | ||
validator = new PatchNotNullValidator(); | ||
} | ||
|
||
@Test | ||
public void testNoPatch() { | ||
PatchField<String> value = PatchField.<String> builder().shouldPatch(false).build(); | ||
assertTrue(validator.isValid(value, null)); | ||
} | ||
|
||
@Test | ||
public void testPatch_helloWorld() { | ||
PatchField<String> value = PatchField.<String> builder().shouldPatch(true).value("hello, world").build(); | ||
assertTrue(validator.isValid(value, null)); | ||
} | ||
|
||
@Test | ||
public void testPatch_null() { | ||
PatchField<String> value = PatchField.<String> builder().shouldPatch(true).value(null).build(); | ||
assertFalse(validator.isValid(value, null)); | ||
} | ||
} |