Home Projects Portfolio Dashboard Export PDF Log in

Evolving Business Rules: Redesigning Donation Criteria for Flexibility

The ibime-connect project recently integrated a significant update to its core donation mechanics. This change, merged from the redesign/donation-criteria branch, focuses on enhancing the system's ability to define, validate, and apply donation eligibility rules more flexibly and clearly.

The Challenge of Shifting Requirements

Business rules, especially those tied to external factors like legal compliance or marketing campaigns, rarely stay static. In ibime-connect, our initial donation criteria needed to evolve to support more nuanced conditions. This meant moving beyond simple checks to a more structured approach that could easily incorporate new parameters without constant code refactors.

Consider a scenario where donation eligibility shifts from a simple minimum amount to also include donor location, previous donation history, or even specific campaign targets. Hardcoding these rules quickly leads to unmanageable conditional logic and brittle systems.

A Structured Approach with Schema Validation

To tackle this, the redesign emphasized a schema-driven approach for defining criteria. By externalizing the structure and validation rules, we gain immense flexibility. For instance, using a library like Zod in TypeScript allows us to define the expected shape and constraints of our donation criteria objects. This ensures that any criteria loaded into the system conform to a predictable structure before application, much like a blueprint guides construction.

Here's an illustrative example of how donation criteria might be defined using Zod:

import { z } from 'zod';

// Define the schema for a single donation criterion
const DonationCriterionSchema = z.object({
  id: z.string().uuid(),
  type: z.enum(['minimumAmount', 'donorLocation', 'campaignTarget']),
  value: z.union([z.number().positive(), z.string().min(1)]),
  isActive: z.boolean().default(true),
  description: z.string().optional(),
});

// Define the schema for a collection of donation criteria
export const DonationCriteriaListSchema = z.array(DonationCriterionSchema);

// Example of parsing/validating incoming criteria data
type DonationCriterion = z.infer<typeof DonationCriterionSchema>;

const rawCriteriaData = [
  { id: '123e4567-e89b-12d3-a456-426614174000', type: 'minimumAmount', value: 10, isActive: true },
  { id: 'another-uuid', type: 'donorLocation', value: 'USA' } // isActive defaults to true
];

try {
  const validatedCriteria = DonationCriteriaListSchema.parse(rawCriteriaData);
  console.log('Validated Criteria:', validatedCriteria);
} catch (error) {
  console.error('Validation Error:', error);
}

This TypeScript snippet demonstrates how Zod can be used to define a robust schema for various types of donation criteria. Each criterion has an id, a type (e.g., minimumAmount, donorLocation), a value, and an isActive flag. The DonationCriteriaListSchema then validates an array of these criteria, ensuring consistency across the board. If new types of criteria are introduced, the type enum can be extended, and the value union adjusted, minimizing changes elsewhere in the system.

Applying the Criteria

Once validated, these structured criteria can be loaded and applied by a dedicated DonationEligibilityService. This service would ingest the current set of active criteria and evaluate them against a prospective donation request. This separation of concerns—defining criteria, validating their structure, and then applying their logic—makes the system far more maintainable. New rules can be added or existing ones modified in a data store, and as long as they adhere to the schema, the application logic remains stable.

The Takeaway

When faced with evolving business rules, invest in a clear, schema-driven approach for defining and validating those rules. This separates rule definition from rule application, significantly reducing complexity and making your system adaptable to future changes. Prioritize explicit data structures over implicit conditional logic to ensure your application can gracefully handle shifting requirements.


Generated with Gitvlg.com

Evolving Business Rules: Redesigning Donation Criteria for Flexibility
Luis Romero

Luis Romero

Author

Share: