BusinessValidationCommon

Business validation validator stream

License

License

GroupId

GroupId

com.mservicetech
ArtifactId

ArtifactId

business-validation-common
Last Version

Last Version

1.0.1
Release Date

Release Date

Type

Type

jar
Description

Description

BusinessValidationCommon
Business validation validator stream
Project URL

Project URL

https://github.com/mservicetech/business-validation-common
Source Code Management

Source Code Management

https://github.com/mservicetech/business-validation-common.git

Download business-validation-common

How to add to project

<!-- https://jarcasting.com/artifacts/com.mservicetech/business-validation-common/ -->
<dependency>
    <groupId>com.mservicetech</groupId>
    <artifactId>business-validation-common</artifactId>
    <version>1.0.1</version>
</dependency>
// https://jarcasting.com/artifacts/com.mservicetech/business-validation-common/
implementation 'com.mservicetech:business-validation-common:1.0.1'
// https://jarcasting.com/artifacts/com.mservicetech/business-validation-common/
implementation ("com.mservicetech:business-validation-common:1.0.1")
'com.mservicetech:business-validation-common:jar:1.0.1'
<dependency org="com.mservicetech" name="business-validation-common" rev="1.0.1">
  <artifact name="business-validation-common" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.mservicetech', module='business-validation-common', version='1.0.1')
)
libraryDependencies += "com.mservicetech" % "business-validation-common" % "1.0.1"
[com.mservicetech/business-validation-common "1.0.1"]

Dependencies

compile (3)

Group / Artifact Type Version
org.yaml : snakeyaml jar 1.26
org.slf4j : slf4j-api jar 1.7.30
org.apache.commons : commons-lang3 jar 3.5

test (3)

Group / Artifact Type Version
ch.qos.logback : logback-classic jar 1.2.3
junit : junit jar 4.13.1
org.mockito : mockito-core jar 2.7.21

Project Modules

There are no modules declared in this project.

business-validation-common

Define base interface and common logic for API business validation

Build Status

Summary

In microservice API, normally it has validation in two levels:

  • Openapi schema validation

    Field level validation for request/response in the openapi specification.

    For example: required fields, data type, data format...

  • Business validation

    Certain business rule validation for which cannot be defined the detail in the openapi specification

    For example, validate account number for different account type; validate driving license number issued from different country/place

For the openapi schema validation, schema validation can be applied directly. Some microservice framwork embedded it with the framework; for example, sprint-boot integrate with swagger codegen and bean validation, light-4j integration with request/response handler chain cross-cutting concerns.

And in the application, schema validation can be applied by applying library directly:

https://github.com/mservicetech/openapi-schema-validation

For business validation, some monolithic application use rule engineer for it. But rule engineer is too heavy for microservice API. And developers more like to build the business validation in pure java code.

Ideally, we would like to build the complex business validation to small and light validation validators as validation chain. The validator chain can be plugin to the service class to controller/handler which will not impact the business logic implmentation in the service/controller/handler classes.

Implementation detail:

For validator implementation, please refer the example in the /sample package

To invoke the validator class list stream

  • Initial validators (get the list validators which implements BaseValidator interface )

For example, on Sprint-boot:

  @Autowired
  List<BaseValidator> validators;

  • Invoke the validators
        List<ValidationCode> codes = new ArrayList<>();
        Stream<ValidationResult> validationResultStream = validators.stream().filter(validator->validator.support(FILTER_NAME)).flatMap(v->v.validate(null,drivingLicense));
        validationResultStream.filter(v->v.isError()).forEach(v->codes.addAll(v.getValidationCodes()));
        if (codes.size()>0) {
            //TODO throw exception or return error codes
        }

  • Invoke the validators with priority order (from small number to large number) defined in the interface method
        Stream<ValidationResult> validationResultStream = validators.stream().
                filter(validator->validator.support(drivingLicense.getPlaceOfIssue())).sorted(Comparator.comparingInt(BaseValidator::priority)).
                flatMap(v->v.validate(null,drivingLicense));

        validationResultStream.filter(v->v.isError()).forEach(v->codes.addAll(v.getValidationCodes()));

rule annotation support

Provide rule annotation which user can define basic rule information there, for example:

@Rule(id="0002", description = "account balance validation", errorMessage = "Balance for account cannot less than 0.",
        effectiveDate= "2020-02-20", endDate="2021-12-32")
public class AccountBalanceValidator implements AnnotationBaseValidator<String, Account> {

The validation class need implement AnnotationBaseValidator interface instead of BaseValidator; Library provide set of methods to use the annotation fields

For implementation detail, please refer to the sample in the test package

The implementation validation class need implement two interface methods:

  • annotationSupport (base support and isValidTimeRange or others)
  • validate (base validation rule logic)
com.mservicetech

mservicetech

microservice technical support

Versions

Version
1.0.1
1.0.0