Skip to content

Commit

Permalink
GH-1 add a not null validator
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffnelson committed Nov 21, 2019
1 parent 48b2045 commit ad72f26
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 4 deletions.
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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
import org.apache.commons.lang3.StringUtils;

import com.github.jeffnelson.jackson.patch.PatchField;
import com.github.jeffnelson.jackson.patch.validator.constraints.PatchStringValid;
import com.github.jeffnelson.jackson.patch.validator.constraints.PatchStringRequired;

public class PatchStringValidator implements ConstraintValidator<PatchStringValid,PatchField<String>> {
public class PatchStringValidator implements ConstraintValidator<PatchStringRequired,PatchField<String>> {

boolean allowNull, allowEmpty, allowBlank;

@Override
public void initialize(PatchStringValid anno) {
public void initialize(PatchStringRequired anno) {
this.allowNull = anno.allowNull();
this.allowEmpty = anno.allowEmpty();
this.allowBlank = anno.allowBlank();
Expand Down
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 {};
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
@Constraint(validatedBy = PatchStringValidator.class)
@Retention(RUNTIME)
@Target(FIELD)
public @interface PatchStringValid {
public @interface PatchStringRequired {

String message() default "Required";

Expand Down
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));
}
}

0 comments on commit ad72f26

Please sign in to comment.