03 · Explain and fix

A finding is only useful if you can find out why the rule exists and who decided it. Ask Redline, then fix the code so the problem is gone, not just the warning.

About 6 minutesNeeds GitHubFiles after this step ↗Pull request #3 ↗

✓

You will learn

  • How to look up any rule from its id
  • The difference between fixing a finding and silencing it
  • How to confirm the fix with the same review

Ask why

Step 02 left findings on your pull request. Before you change any code, find out what a rule asks for and why. Take the rule id from the brackets of a finding:

terminal
$ pnpm redline explain core/unsafe-assertion

BLOCKER core/unsafe-assertion

  No unsafe assertions (`as unknown as X`, force casts) used to silence an error. Where one is
  genuinely required, the justification is a `SAFETY:` comment on the same line or the line above,
  stating why the assertion holds (`// SAFETY: payload parsed by the schema above`). Any other
  comment does not count.

  decided by   a checker, with no model call
  defined in   standards/core.md:87
  applies to   every file — the core standard is not scoped by stack
  reaches      javascript-typescript, web-react, web-nextjs, … every profile
  • decided by tells you whether a checker or a model applies it, so you know whether the result can vary.
  • defined in is where the rule is written down. If you think a rule is wrong, that file is where you propose changing it, in a pull request, like code.
  • pnpm redline explain --list prints every rule id.
New term Standard

The written set of rules, kept in one versioned repository and owned by people, not by a tool. Redline renders it into each repository's assistant files and checks against it. Your team can add its own rules on top, which is the next step.

Fix it, don't silence it

There are two ways to make a finding disappear. Compare them:

Check the parsed JSON with a type predicate. Once the code has proved the value is a User, there is nothing left to assert or suppress, and the TODO and the unknown alias go with it. The error in requireUser stops naming anyone: the caller knows who asked, and decides what is safe to log. In src/users.ts, replace everything you added in step 02 with:

src/users.ts — replaces the step 02 code
function isUser(value: unknown): value is User {
  return (
    typeof value === 'object' &&
    value !== null &&
    'id' in value &&
    typeof value.id === 'string' &&
    'name' in value &&
    typeof value.name === 'string' &&
    'email' in value &&
    typeof value.email === 'string'
  )
}

export function loadUser(raw: string): User {
  const parsed: unknown = JSON.parse(raw)
  if (!isUser(parsed)) throw new Error('payload is not a User')
  return parsed
}

export function requireUser(directory: UserDirectory, id: string): User {
  const user = directory.find(id)
  if (!user) throw new Error('the requested user does not exist')
  return user
}

A payload with a missing field now throws a clear error, at the boundary where it arrived, instead of becoming a User with an undefined name somewhere far away. Pin all three outcomes with a test. In src/users.test.ts, change the import line to:

src/users.test.ts — the import line
import { UserDirectory, loadUser, requireUser } from './users.js'

and add these at the end:

src/users.test.ts — add at the end
test('a valid payload becomes a User', () => {
  const user = { id: 'u1', name: 'Ada', email: '[email protected]' }
  assert.deepEqual(loadUser(JSON.stringify(user)), user)
})

test('a payload missing a field is refused', () => {
  assert.throws(() => loadUser('{"id":"u1","name":"Ada"}'), /not a User/)
})

test('a missing user is refused without naming anyone', () => {
  assert.throws(() => requireUser(new UserDirectory(), 'u1'), /^Error: the requested user does not exist$/)
})

Check it

The tests first: the two from step 00 and the three you just added.

terminal
$ pnpm test

ℹ pass 5
ℹ fail 0

Then the same review that found the problems:

terminal
$ git add src/users.ts src/users.test.ts
$ pnpm redline review

profile javascript-typescript — rules in scope: core, typescript
no findings

The checker's four findings are gone. The fifth, line 30, needs a model, so run /redline-review in your assistant again and expect no [core/customer-data-in-logs] finding: the error no longer names anyone.

Merge it

The fix goes onto the same branch, so it lands in the pull request from step 02:

terminal
$ git commit -m "fix: validate the user payload, and keep personal data out of errors"
$ git push
$ gh pr checks --watch

✓  test
✓  redline-gate / Deterministic policy
X  redline-gate / PR checklist                                the box you left unticked
✓  redline-gate / ADR required for significant changes
✓  redline-gate / Dependency review
✓  redline-gate / Secret scan (diff)
✓  redline-gate / gate

redline-gate / Deterministic policy is green now. On GitHub, tick the input-validation item you left unticked in step 02, honestly this time, because the code validates. Ticking it re-runs the checklist, and every check is green. Then merge, and bring main back to your machine for the next step:

terminal
$ gh pr merge --squash
$ git checkout main
$ git pull

Check your understanding

A teammate adds // SAFETY: it works above an as unknown as User. What should a reviewer do?