Framework RecipesImplementation, failure path, check

Express · requirements checked during agent edits

Build an Express API that caps discounts before calculation

The handler checks type, finiteness, and both boundaries before doing arithmetic. Testing only the successful 20% case will not catch an agent removing the upper bound. The rule preserves that particular requirement through later refactors.

Implement the requirement

Save this as server.cjs:

const express = require("express");
const app = express();
app.use(express.json({ limit: "10kb" }));

app.post("/checkout", (request, response) => {
  const { subtotalCents, discountPercent = 0 } = request.body;
  if (!Number.isSafeInteger(subtotalCents) || subtotalCents <= 0) {
    return response.status(400).json({ error: "subtotalCents must be a positive integer" });
  }
  if (!Number.isFinite(discountPercent) || discountPercent < 0 || discountPercent > 20) {
    return response.status(400).json({ error: "discountPercent must be between 0 and 20" });
  }
  return response.json({ totalCents: Math.round(subtotalCents * (1 - discountPercent / 100)) });
});

if (require.main === module) app.listen(3000);
module.exports = app;

The requirement is adapted from Perch’s public order-service example at commit 4af67af. The Express implementation below is an illustrative adaptation, not a claim about that source project’s framework.

Create the Express project

mkdir checked-express
cd checked-express
npm init -y
npm install express
npm install --save-dev supertest
git init -b tutorial

Save the implementation above in the new project.

Start the development server with node server.cjs. Follow the Express setup documentation for platform prerequisites. The framework API used here is documented in the official reference.

Test the broken and corrected behavior

Save this as server.test.cjs:

const test = require("node:test");
const assert = require("node:assert/strict");
const request = require("supertest");
const app = require("./server.cjs");

test("discount boundary and input types", async () => {
  const good = await request(app).post("/checkout").send({subtotalCents: 10000, discountPercent: 20});
  assert.equal(good.status, 200);
  assert.equal(good.body.totalCents, 8000);
  for (const discountPercent of [21, -1, "20", null]) {
    const bad = await request(app).post("/checkout").send({subtotalCents: 10000, discountPercent});
    assert.equal(bad.status, 400);
  }
});

Run node --test server.test.cjs. Then make this deliberate regression: replace || discountPercent > 20 with nothing. The boundary test should fail. Restore the original before continuing.

Save the requirement as a Perch rule

Perch is an open-source semantic linter powered by SystemOne decision models. Save the following rule in .perch/rules/perch-guide-express-intro.yaml at the repository root, keeping your existing perch.yaml:

- name: discount-limit
  where: "server.cjs"
  ensure: >-
    The checkout route rejects discountPercent unless it is a finite number in the inclusive range 0 to 20, before calculating the discounted total.

Install the CLI and sign in, then check the working file:

npm install -g @lakeday/perch
perch login
perch check server.cjs --rules discount-limit --json

Use Node 22 or newer. perch login opens the authentication workflow. perch check reads the file currently on disk, so the agent can check an uncommitted edit. A rule finding exits 3; exits 1 and 2 mean the check did not complete. Read the JSON and the code before deciding what to change.

Captured Perch check

On October 14, 2026, we ran the rule above through Perch using jev-latest on the deliberately broken and corrected files.

Control Command exit Outcome
Broken implementation 3 Rule finding reported
Corrected implementation 0 No rule finding reported

The rule distinguished these two controls in this captured run. Fresh judgments can vary. Captured command output and the broken and corrected source are available for inspection.

Install the rule pack and configure your agent

Download this rule pack ZIP · Download the YAML rules · Agent task. The pack contains the rule, its example paths and the repair/recheck instructions. Review the where selector before using it in an existing project. Keep your root perch.yaml; install this rule as a separate file under .perch/rules/.

  1. Install Perch using Node 22 or newer and sign in once:
npm install -g @lakeday/perch
perch login
  1. Download the pack, inspect it, and extract it from your repository root. The extraction preserves any existing files:
curl -fL 'https://frameworkrecipes.dev/express/intro/rule-pack.zip' -o 'perch-guide-express-intro.zip'
unzip -n 'perch-guide-express-intro.zip'

The installed rule is .perch/rules/perch-guide-express-intro.yaml. Its selector matches server.cjs in this example; adapt the selector to your application file before checking an existing project.

  1. Configure the coding agent you use. Run one of these commands:

Claude Code:

perch setup claude-code

Codex:

perch setup codex

Cursor:

perch setup cursor

Pi:

perch setup pi
  1. Ask the agent to edit the implementation and check the saved requirement:

Use the installed Perch skill while editing server.cjs. Preserve discount-limit. Run perch check server.cjs --rules discount-limit --json after the edit. Read each finding and the affected code, fix the implementation, and run the identical check again after the fix. Keep the rule and regression tests intact. Run the application's relevant regression tests and report the commands and actual results. Exit 3 means a rule finding; exit 1 or 2 means the check did not complete. Report an unavailable or incomplete check as blocked. Do not claim the application tests ran if only the Perch check ran.

  1. After repairing a finding, run the same check again and inspect the result:
perch check server.cjs --rules discount-limit --json

Commit the downloaded rule and the installed agent configuration with your application. The rule file adds no guarantee of correctness; keep the regression tests from this guide.

Keep the checks within their scope

For a real checkout, fetch prices from your own catalog rather than trusting a client subtotal. Define the currency rounding policy explicitly and use provider idempotency when charging money. This tutorial keeps the example focused on the discount invariant.

Fresh model judgments can vary. Unchanged requests can reuse cached answers. A passing rule check does not prove the application correct; keep the executable test as a separate check. Relevant source code is sent to the configured analysis service.

Published by Perch / Verglas LLC. Framework names identify the software used; this guide is not an official framework publication.