Merge pull request #19 from fynmanoj/customval
add-custom-validation-basedon-conf
diff --git a/api/src/main/java/org/apache/fineract/cn/customer/api/v1/client/validation/NotBlankBasedOnConfig.java b/api/src/main/java/org/apache/fineract/cn/customer/api/v1/client/validation/NotBlankBasedOnConfig.java
new file mode 100644
index 0000000..c03d6c6
--- /dev/null
+++ b/api/src/main/java/org/apache/fineract/cn/customer/api/v1/client/validation/NotBlankBasedOnConfig.java
@@ -0,0 +1,46 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.fineract.cn.customer.api.v1.client.validation;
+
+import javax.validation.Constraint;
+import javax.validation.Payload;
+import java.lang.annotation.Documented;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+import static java.lang.annotation.ElementType.*;
+
+@Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER})
+@Retention(RetentionPolicy.RUNTIME)
+@Constraint(validatedBy = NotBlankBasedOnConfigValidator.class)
+@Documented
+public @interface NotBlankBasedOnConfig {
+ String message() default "{NotBlankBasedOnConfig.message}";
+
+ Class<?>[] groups() default {};
+ Class<? extends Payload>[] payload() default {};
+
+ @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER})
+ @Retention(RetentionPolicy.RUNTIME)
+ @Documented
+ @interface List {
+ NotBlankBasedOnConfig[] value();
+ }
+}
diff --git a/api/src/main/java/org/apache/fineract/cn/customer/api/v1/client/validation/NotBlankBasedOnConfigValidator.java b/api/src/main/java/org/apache/fineract/cn/customer/api/v1/client/validation/NotBlankBasedOnConfigValidator.java
new file mode 100644
index 0000000..c8fb404
--- /dev/null
+++ b/api/src/main/java/org/apache/fineract/cn/customer/api/v1/client/validation/NotBlankBasedOnConfigValidator.java
@@ -0,0 +1,53 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.fineract.cn.customer.api.v1.client.validation;
+
+import org.apache.commons.lang.StringUtils;
+import org.hibernate.validator.constraints.NotBlank;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Component;
+
+import javax.validation.ConstraintValidator;
+import javax.validation.ConstraintValidatorContext;
+
+@Component
+public class NotBlankBasedOnConfigValidator implements ConstraintValidator<NotBlankBasedOnConfig, Object> {
+ private String fieldName;
+ private String expectedFieldValue;
+
+ @Value("${config.bypassNotNull}")
+ private Boolean bypassMandatory;
+
+ @Override
+ public void initialize(NotBlankBasedOnConfig constraintAnnotation) {
+ }
+
+ @Override
+ public boolean isValid(Object value, ConstraintValidatorContext context) {
+ if(bypassMandatory)
+ return true;
+ return notBlank(value);
+ }
+ private boolean notBlank(@NotBlank Object vl){
+ if(vl == null) return false;
+ String value = (String) vl;
+ if(StringUtils.isBlank(value)) return false;
+ return true;
+ }
+}
diff --git a/api/src/main/java/org/apache/fineract/cn/customer/api/v1/client/validation/NotNullBasedOnConfig.java b/api/src/main/java/org/apache/fineract/cn/customer/api/v1/client/validation/NotNullBasedOnConfig.java
new file mode 100644
index 0000000..19552c8
--- /dev/null
+++ b/api/src/main/java/org/apache/fineract/cn/customer/api/v1/client/validation/NotNullBasedOnConfig.java
@@ -0,0 +1,44 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.fineract.cn.customer.api.v1.client.validation;
+
+import javax.validation.Constraint;
+import javax.validation.Payload;
+import java.lang.annotation.*;
+
+import static java.lang.annotation.ElementType.*;
+import static java.lang.annotation.ElementType.CONSTRUCTOR;
+
+@Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER})
+@Retention(RetentionPolicy.RUNTIME)
+@Constraint(validatedBy = NotNullBasedOnConfigValidator.class)
+@Documented
+public @interface NotNullBasedOnConfig {
+ String message() default "{NotNullBasedOnConfig.message}";
+
+ Class<?>[] groups() default {};
+ Class<? extends Payload>[] payload() default {};
+
+ @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER})
+ @Retention(RetentionPolicy.RUNTIME)
+ @Documented
+ @interface List {
+ NotNullBasedOnConfig[] value();
+ }
+}
diff --git a/api/src/main/java/org/apache/fineract/cn/customer/api/v1/client/validation/NotNullBasedOnConfigValidator.java b/api/src/main/java/org/apache/fineract/cn/customer/api/v1/client/validation/NotNullBasedOnConfigValidator.java
new file mode 100644
index 0000000..c7367a1
--- /dev/null
+++ b/api/src/main/java/org/apache/fineract/cn/customer/api/v1/client/validation/NotNullBasedOnConfigValidator.java
@@ -0,0 +1,47 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.fineract.cn.customer.api.v1.client.validation;
+
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Component;
+
+import javax.validation.ConstraintValidator;
+import javax.validation.ConstraintValidatorContext;
+
+@Component
+public class NotNullBasedOnConfigValidator implements ConstraintValidator<NotNullBasedOnConfig, Object> {
+ private String fieldName;
+ private String expectedFieldValue;
+
+ @Value("${config.bypassNotNull}")
+ private Boolean bypassMandatory;
+
+ @Override
+ public void initialize(NotNullBasedOnConfig constraintAnnotation) {
+ }
+
+ @Override
+ public boolean isValid(Object value, ConstraintValidatorContext context) {
+ if(bypassMandatory)
+ return true;
+ if(value == null)
+ return false;
+ return true;
+ }
+}
diff --git a/api/src/main/java/org/apache/fineract/cn/customer/api/v1/domain/Customer.java b/api/src/main/java/org/apache/fineract/cn/customer/api/v1/domain/Customer.java
index e8cfae3..184f097 100644
--- a/api/src/main/java/org/apache/fineract/cn/customer/api/v1/domain/Customer.java
+++ b/api/src/main/java/org/apache/fineract/cn/customer/api/v1/domain/Customer.java
@@ -18,6 +18,8 @@
*/
package org.apache.fineract.cn.customer.api.v1.domain;
+import org.apache.fineract.cn.customer.api.v1.client.validation.NotBlankBasedOnConfig;
+import org.apache.fineract.cn.customer.api.v1.client.validation.NotNullBasedOnConfig;
import org.apache.fineract.cn.customer.catalog.api.v1.domain.Value;
import java.util.List;
import javax.validation.Valid;
@@ -41,22 +43,22 @@
@NotBlank
private String identifier;
- @NotNull
+ @NotNullBasedOnConfig
private Type type;
- @NotBlank
+ @NotBlankBasedOnConfig
private String givenName;
private String middleName;
- @NotBlank
+ @NotBlankBasedOnConfig
private String surname;
- @NotNull
+ @NotNullBasedOnConfig
private DateOfBirth dateOfBirth;
- @NotNull
+ @NotNullBasedOnConfig
private Boolean member;
private String accountBeneficiary;
private String referenceCustomer;
private String assignedOffice;
private String assignedEmployee;
- @NotNull
+ @NotNullBasedOnConfig
@Valid
private Address address;
@Valid
diff --git a/service/src/main/resources/application.yml b/service/src/main/resources/application.yml
index 1008fc9..42308b8 100644
--- a/service/src/main/resources/application.yml
+++ b/service/src/main/resources/application.yml
@@ -73,3 +73,6 @@
upload:
image:
max-size: 524288
+
+config:
+ bypassNotNull: true