Declarative Rules

Introduction:

Declarative rules feature enables a user to attach certain business related rules to any oeCloud model. This feature mainly integrates business rules engine with oeCloud.

What you’ll build

  • You’ll build a model ‘TestModel’ and attac business rules to it.

What You’ll need:

How to complete this guide:

ModelRule

ModelRule is a framework model which attaches certain rules to any oeCloud model. It has the following structure :

{
"name": "ModelRule",
"base": "BaseEntity",
"plural": "ModelRules",
"properties": {
    "modelName": {
      "type": "string",
      "required": true,
      "unique": true
    },
    "disabled": {
      "type": "boolean",
      "default": false
    },
    "defaultRules": {
      "type": ["string"],
      "default": []
    },
    "validationRules": {
      "type": ["string"],
      "default": []
    }
  }
}

modelName

It represents the model to which the rules will be attached.

disabled

This is a flag which decides at runtime if the attached rules should be evaluated or not(if it is true then rules will not get evaluated).

defaultRules

It contains an array of strings representing the decision table names, which will be evaluated with the payload that is POSTed.Evaluation of those rules will enhance the payload by adding the evaluated results to it.Those rules will be evaluated in the same sequence as they are mentioned in the array.

validationRules

It is also an array of strings representing decision table names, but evaluation of those rules will validate the enhanced payload and if any rule is violated will return an array of error.

Create the following model :

{
	"name": "TestModel",
	"base": "BaseEntity",
	"plural": "TestModels",
	"properties": {
		"organisation": {
			"type": "string",
			"max": 8
		},
		"employeeId": "string",
		"department": "string",
		"salarySlip": "boolean",
		"form16": "boolean",
		"passport": "string",
		"city": "string",
		"age": {
			"type": "number",
			"max": 50
		},
		"basePay": "number",
		"experience": "number",
		"address proof": "boolean"
	}
}

Now declare certain rules to be attached to the above created model.

Post the below data to model “ModelRule” :

{
	"modelName": "TestModel",
	"defaultRules": ["PropertyPopulatorOne", "PropertyPopulatorTwo"],
	"validationRules": ["validation"]
}

Post the below data to model “DecisionTable” :

{
	"name": "PropertyPopulatorOne",
	"document": {
		"documentName": "PropertyPopulatorOne.xlsx",
		"documentData": "the base64 encoded data goes here"
	}
}

Note : In the above JSON data, field “documentData” of property “document” should contain the base64 encoded data for the corresponding excel to be uploaded. Field “documentName” contains the excel file’s name to be uploaded and property “name” can contain any name to identify the uploaded file.

PropertyPopulatorOne, PropertyPopulatorTwo and validation are decision table names already present in database(“DecisionTable” model contain those entries).

Those rules can be downloaded from below :

PropertyPopulatorOne.xlsx

PropertyPopulatorTwo.xlsx

validation.xlsx

After those rules are declared for the “TestModel” you can post to the below data to it :

{
	"organisation": "A Corp",
	"designation": "SSE",
	"state": "KA",
	"pincode": "560100",
	"loanAmount": 400000,
	"experience": 3,
	"age": 19,
	"address proof": false,
	"passport": "A1234567",
	"employeeId": "123456"
}

This payload will go through the defaultRules(PropertyPopulatorOne and PropertyPopulatorTwo) first which will enhance it and add certain fields depending upon the rules.Next it will go through validationRules(validation) which will validate it and return error if any in addition to model definition validation errors.

Output for the above post is :

{
  "organisation": "A Corp",
  "employeeId": "123456",
  "department": "A-Dept",
  "passport": "A1234567",
  "city": "BLR",
  "age": 19,
  "basePay": 300000,
  "experience": 3,
  "address proof": false,
  "id": "592277539e43b5f830cf371b",
  "designation": "SSE",
  "state": "KA",
  "pincode": "560100",
  "loanAmount": 400000
}

Let’s post the following erroneous data and see the result.

{
	"organisation": "A Corp",
	"designation": "SSE",
	"state": "KA",
	"pincode": "560100",
	"loanAmount": 400000,
	"experience": 3,
	"age": 55,
	"address proof": false
}

Output for the above POST is :

{
  "error": {
    "status": 422,
    "message": "The `TestModel` instance is not valid.",
    "txnId": "19e4c7e0-2f37-11e7-96a1-d523aef303a7",
    "errors": [
      {
        "code": "validation-err-002",
        "message": " maximum value criteria for this field is not met",
        "retriable": false,
        "path": "TestModel->age"
      },
      {
        "code": "err-employer-check",
        "message": "employer details(employeeId, department and organisation) were not given"
      },
      {
        "code": "err-address-check",
        "message": "passport details alongwith city necessary"
      }
    ]
  }
}

Three errors are coming as we have violated two of the validation decision table rules(1. employeeId should be present when “experience” >2 and 2. passport should be present if “address proof” is not present i.e. false) and the model definition validation(max age is 50).

Summary

Congratulations! You’ve just completed a declarative rules example using an application powered by oeCloud. To experience more features offered by oeCloud explore examples. Happy coding.

For more information on decision table and business rules, please refer : Business Rules Engine