Help us shape the future of Go! Take the 2026 Go Developer Survey

rule

package module
v0.8.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 31, 2026 License: GPL-3.0 Imports: 0 Imported by: 0

README

Business Rule

Tests Status Go Report Card PkgGoDev

Powerful business rules

Introduction

A business rule defines or constrains some aspect of business. Given a specified context (data) a business rule always resolves to either true or false.

Business rules are intended to assert business structure or to control or influence the behavior of the business. Business rules describe the operations, definitions and constraints that apply to an organization. Business rules can apply to people, processes, corporate behavior and computing systems in an organization, and are put in place to help the organization achieve its goals.

Formal specification:

Given {context} When {condition(s)} Then {pass} Or {fail}

Specification

The syntax Given {context} When {condition(s)} Then {pass} Or {fail} is a common way to express business rules. It is used to define a set of conditions that must be met in order for a business process to be successful.

The Given clause specifies the context of the rule. This could be a specific event, such as a customer making a purchase, or a general condition, such as the availability of a product.

The When clause specifies the conditions that must be met. These conditions can be anything from the customer having a valid credit card to the product being in stock.

The Then clause specifies the outcome of the rule. If all of the conditions are met, the business process will pass. If any of the conditions are not met, the business process will fail.

Here is an example of a business rule expressed in this syntax:

Given a customer makes a purchase
When the customer has a valid credit card
Then the purchase will pass

This rule states that a purchase will only be successful if the customer has a valid credit card. If the customer does not have a valid credit card, the purchase will fail.

Business rules expressed in this syntax can be used to automate business processes and ensure that they are consistent and efficient. They can also be used to prevent errors and fraud.

Install

go get github.com/dracory/rule

Usage

1) Direct usage
str := "Hello World"

// specify a simple rule that tests if a string is "Hello World"
rule := rule.New().
    // Your data. The given context for this rule
    SetContext(str).
    // Your condition. When will the condition be met
    SetCondition(func(ctx any) bool {
        value := ctx.(string)
        return value == "Hello World"
    })

if rule.Fails() {
    // Then Execute fail logic
    print("String is not what we expected")
}

if rule.Passes() {
    // Then Execute pass logic
    print("String is what we expected")
}
2) Your Custom Rule Type

Direct usage may not be very practical if we want to reuse on multiple places and avoid duplication of business logic.

Which is why it is recommended to create your own custom rule type


// 1. Specify the business rule class
type AllowAccessRule {
    rule.Rule
}

func NewAllowAccessRule(user models.User) *AllowAccessRule {
	rule := &AllowAccessRule{}
    rule.SetContext(user)
	rule.SetCondition(rule.allowAccessCondition)
	return rule
}

var _ rule.RuleInterface = (*AllowAccessRule)(nil) // (optional) verify it extends the RuleInterface interface


func (rule *AllowAccessRule) allowAccessCondition(context any) bool {
	user := context.(models.User)
	return user.IsEmailConfirmed() && user.IsActive()
}

Now lets use it where we need it

if NewAllowAccessRule(user).Fails() {
    panic('You are not allowed access to this part of the website');
}

More About Business Rules

Business rules can be classified into three main types:

  • Structural rules define the relationships between different entities in a business, such as customers, products, and orders.
  • Decision rules determine how the business should respond to certain events, such as a customer making a purchase or a product becoming out of stock.
  • Process flow rules specify the steps that must be taken to complete a business process, such as the steps involved in processing a sales order.

Business rules are important for a number of reasons. They help to ensure that the business operates in a consistent and efficient manner. They also help to prevent errors and fraud. Additionally, business rules can be used to automate business processes, which can save time and money.

The benefits of business rules include:

  • Consistency: Business rules ensure that the business operates in a consistent manner, regardless of who is performing the task. This is important for maintaining high-quality customer service and reducing errors.
  • Efficiency: Business rules can be used to automate business processes, which can save time and money. For example, a business rule can be used to automatically approve sales orders that meet certain criteria.
  • Compliance: Business rules can be used to ensure that the business complies with regulations. For example, a business rule can be used to verify that customers are of legal age to purchase alcohol.
  • Flexibility: Business rules can be easily changed as the business needs change. This is important for businesses that are constantly evolving.

The challenges of business rules include:

  • Complexity: Business rules can be complex, especially for large businesses with complex operations. This can make it difficult to manage and maintain business rules.
  • Documentation: It is important to document business rules in a clear and concise manner so that they can be easily understood by everyone who needs to use them.
  • Testing: Business rules should be tested thoroughly to ensure that they work as expected. This can be a time-consuming and expensive process.
  • Change management: Business rules need to be managed effectively as the business changes. This includes updating the rules when necessary and communicating the changes to everyone who needs to know about them.

Overall, business rules are an important tool for businesses of all sizes. They can help to improve efficiency, compliance, and flexibility. However, it is important to manage business rules effectively to avoid the challenges associated with them.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Rule

type Rule struct {
	// contains filtered or unexported fields
}

func New

func New() *Rule

New creates a new rule

func NewRule

func NewRule() *Rule

NewRule creates a new rule Deprecated use New as it is shorter

func (*Rule) AddFailMessage

func (rule *Rule) AddFailMessage(message string)

AddFailMessage adds a fail message

func (*Rule) AddPassMessage

func (rule *Rule) AddPassMessage(message string)

AddPassMessage adds a pass message

func (*Rule) Evaluate

func (rule *Rule) Evaluate() bool

Evaluates the condition and returns the result

func (*Rule) FailMessageFirst

func (rule *Rule) FailMessageFirst() string

FailMessageFirst retrieves the first fail message

func (*Rule) FailMessageLast

func (rule *Rule) FailMessageLast() string

FailMessageLast retrieves the last fail message

func (*Rule) FailMessages

func (rule *Rule) FailMessages() []string

FailMessages retrieves the fail messages

func (*Rule) Fails

func (rule *Rule) Fails() bool

Returns true if the rule evaluates to false

func (*Rule) GetContext

func (rule *Rule) GetContext() any

func (*Rule) PassMessageFirst

func (rule *Rule) PassMessageFirst() string

PassMessageFirst retrieves the first pass message

func (*Rule) PassMessageLast

func (rule *Rule) PassMessageLast() string

PassMessageLast retrieves the last pass message

func (*Rule) PassMessages

func (rule *Rule) PassMessages() []string

PassMessages retrieves the pass messages

func (*Rule) Passes

func (rule *Rule) Passes() bool

Returns true if the rule evaluates to true

func (*Rule) SetCondition

func (rule *Rule) SetCondition(condition func(context any) bool) RuleInterface

Sets the condition for the rule

func (*Rule) SetContext

func (rule *Rule) SetContext(context any) RuleInterface

Sets the context/context for the rule

func (*Rule) Validate added in v0.8.0

func (rule *Rule) Validate() (passed bool, messages []string)

Validate evaluates the rule and returns pass/fail with appropriate messages

type RuleInterface

type RuleInterface interface {
	SetContext(ctx any) RuleInterface
	GetContext() any
	SetCondition(conditionCallback func(ctx any) bool) RuleInterface
	Passes() bool
	Fails() bool
	Evaluate() bool
	Validate() (passed bool, messages []string)
	AddFailMessage(message string)
	AddPassMessage(message string)
	FailMessages() []string
	PassMessages() []string
	FailMessageFirst() string
	FailMessageLast() string
	PassMessageFirst() string
	PassMessageLast() string
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL