wso2 / docs-open-banking

Apache License 2.0
39 stars 25 forks source link

Document Validation APIs #231

Closed thiloshon closed 3 years ago

thiloshon commented 3 years ago

Description: Document the new Validation Layer for developers.

Validation Layer of WSO2

WSO2 OpenBanking uses annotation-based constraints to perform validations. This application layer agnostic validation makes it easier to extend and reuse. Hibernate Validator which is the reference implementation of the JSR 380 specification of Java API for bean validation, is used in OB.

To make it easier to perform validations, WSO2 OB Accelerator offers a pre configured validator and a set of common validation annotations.

First add the open-banking common dependency.

<dependency>
    <groupId>com.wso2.finance</groupId>
    <artifactId>openbanking-common</artifactId>
</dependency>

Create your models:

class OBRequestObject { 
     private String name;
     private Claims claims;
}

class Claims {
     private String aud;
     private String scope;
}

Add standard validation-api annotations;

class OBRequestObject { 

     @NotNull
     private String name;
     private Claims claims;
}

class Claims {
     private String aud;
     private String scope;
     @Max(3600)
     private int sharing_duration;
}

WSO2 OB offers some common annotations used in OB use cases as well. Eg: @RequiredParameter, @ValidScope, @ValidAudience

Usage example:

RequiredParameter(param=“claims.scope”, message=“Scope is a mandatory Field”)
RequiredParameter(param=“name”, message=“Name is a mandatory Field”)
class OBRequestObject { 

     private String name;
     private Claims claims;
}

class Claims {
     private String aud;
     private String scope;
     private int sharing_duration
}

Note:

  1. You have to pass the field path to resolve the variables annotations require. (“claim.scope”)
  2. You can pass a custom error message when the constrain fails in the message parameter.
  3. All annotations are targeted in class level.

Finally to get the OB Validator instance and execute validations:

OpenBankingValidator openbankingValidator = OpenBankingValidator.getInstance();
String violation = openbankingValidator.getFirstViolation(requestObject);

if (violation != null) {
    //throw error using violation message
}

Affected Product Version: OB 3.0

SSParamee commented 3 years ago

Documentation for this is on-hold for now...

SSParamee commented 3 years ago

Please find the WIP doc for this task: https://docs.google.com/document/d/1RgZXu_LV-fXuYMB6snJuqmHHJcWC6bVuJupZ7mbOubk/edit?ts=601928ce#

isharailanga commented 3 years ago

This doc live now - https://ob.docs.wso2.com/en/latest/develop/validation-layer/