Controlling Randomness: Enforcing Rules Before API Submission
- Elisha Antunes
- Jun 7
- 3 min read
Updated: Jun 23
Let's take a look at how random event-level data can be programmatically constrained to follow business rules — before it's ever sent to the API. These techniques are useful when simulating submission workflows involving dates, daily thresholds, and multi-day event structures.
You may come across scenarios — whether in insurance, ticketing, or registration workflows — where users are expected to submit event-specific information. For each event type, the number of allowed days and maximum daily attendance might vary. If your API doesn’t enforce these rules, invalid submissions can easily sneak in.
Below we'll explore how to introduce controlled randomness that still respects rules.
The Problem
When working with APIs that expect structured event data, it's easy to generate unpredictable or non-compliant inputs — especially in automated environments.
Common issues include:
Too many event days for a short-term event
Daily attendance that exceeds the permitted limit
Past-dated event schedules
Mismatches between event types and their eligible characteristics
These issues can result in misleading test results or backend validation errors.
The Solution: Controlled Data Generation
Here's a solution pattern that addresses the above by:
Randomly selecting an eligible event type
Generating a random number of days within allowed limits
Capping maxDailyAttendance appropriately
Enforcing future-only dates
We’ll do this before each submission, using Postman pre-request scripts.
Event Type Rules (Mapping)
We define an object called rules that maps each event type to its own set of constraints:
const rules = {
"MusicFestival": { maxNumberOfDays: 3, maxDailyAttendance: 500 },
"Business_Meeting": { maxNumberOfDays: 1, maxDailyAttendance: 200 },
"TradeShow": { maxNumberOfDays: 5, maxDailyAttendance: 1000 },
"FoodExpo": { maxNumberOfDays: 2, maxDailyAttendance: 300 }
};This structure allows us to easily retrieve rules based on a selected event type.
For example:
If the selected type is "Business_Meeting", then:
maxNumberOfDays = 1
maxDailyAttendance = 200
These constraints directly shape how many days we generate and the max cap on daily attendees.
This mapping acts like a reference table — giving us tight control over what each event configuration is allowed to look like.
We’ll randomly pick one of these and use the corresponding rule set.
Generator Functions
We'll use a few helper functions to build valid data.
function generateFutureDate(offsetDays) {
const date = new Date();
date.setDate(date.getDate() + offsetDays);
return date.toISOString().split('T')[0];
}
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function generateEventDays(eventType, rules) {
const rule = rules[eventType];
const numberOfDays = getRandomInt(1, rule.maxNumberOfDays);
const days = [];
for (let i = 0; i < numberOfDays; i++) {
days.push({
eventDate: generateFutureDate(5 + i), // Staggered future dates
maxDailyAttendance: getRandomInt(50, rule.maxDailyAttendance)
});
}
return days;
}Putting It All Together
const eligibleEventTypes = Object.keys(rules);
const selectedEventType = eligibleEventTypes[Math.floor(Math.random() * eligibleEventTypes.length)];
const event = {
eventType: selectedEventType,
eventDays: generateEventDays(selectedEventType, rules)
};
console.log("Generated event object:", event);
pm.collectionVariables.set('event', JSON.stringify(event));Sample Output Request Payload
Here’s what a valid API request might look like after applying this logic:
{
"eventType": "TradeShow",
"eventDays": [
{
"eventDate": "2025-06-15",
"maxDailyAttendance": 450
},
{
"eventDate": "2025-06-16",
"maxDailyAttendance": 200
},
{
"eventDate": "2025-06-17",
"maxDailyAttendance": 275
}
]
}In this case:
The event type is "TradeShow"
The number of event days is 3, which is within the allowed maximum of 5
Each maxDailyAttendance value respects the upper limit of 1000
Takeaways
This technique helps test environments avoid invalid input structures, particularly when relying on randomized data. By mapping event types to rules and using dynamic generators with limits, you gain:
Higher fidelity test data
Cleaner submission flows
Fewer false positives in validation error logs
Confidence that your test cases reflect real business scenarios
