StaffCoder
.NET
.NET.NET 10
12 modules · 48 challengesSign up
Module 10 · Middleware, Filters and Error Handling

Endpoint Validation Filter

medium—No data yet

Problem statement

Complete the implementation of ValidationEndpointFilter, and attach it to the booking intake group, so that a booking breaking any of its rules is turned away with a validation problem and never reaches a handler.

Sample Data

A booking payload carries three fields, and the rules each of them has to satisfy are already declared on it:

{
  "guestName": "Marta Vella",
  "seats": 2,
  "sessionCode": "CLY-204"
}
  • guestName has to be there, message A guest name is required., and has to be 2 to 40 characters long, message A guest name must be 2 to 40 characters long.
  • seats has to be between 1 and 6, message Seats must be between 1 and 6.
  • sessionCode has to be there, message A session code is required., and has to read as three capital letters, a hyphen and three digits, message A session code must look like ABC-123., so CLY-204 satisfies it while cly-204, CLY204 and CLY-2045 do not.

Service

The Booking Intake Group

The group /bookings holds four endpoints, each of which takes a booking payload and answers 200 with the three fields it was handed plus a stage of its own:

  • POST /bookings/holds answers a stage of hold.
  • POST /bookings/confirmations answers a stage of confirmation.
  • POST /bookings/waitlist answers a stage of waitlist.
  • POST /bookings/priority answers a stage of priority.

POST /bookings/drafts takes the same payload and answers a stage of draft, and it shares the group's path without belonging to the group. A handler counts one call the moment it starts, and GET /diagnostics/handler-calls reports that running total without counting a call of its own.

ValidationEndpointFilter

Implement InvokeAsync so that it runs ahead of the handler of whichever endpoint it is attached to and does the deciding itself, on the payload it is handed and the call it is given to pass that payload on to:

  • Take the booking payload out of the endpoint's bound arguments. It is not always the first of them: POST /bookings/confirmations and POST /bookings/priority take it second.
  • Check the payload against the rules declared on it. One that breaks nothing reaches the handler exactly as it arrived, and the handler's own answer is the answer the caller gets.
  • One that breaks a rule never reaches the handler, so the handler call count reads the same after the refusal as it did before it, and a refusal changes nothing else about the application: the endpoint that refused a payload takes the next acceptable one, and the rest of the application answers as it did.
  • Refuse it with 400 and a body of type application/problem+json, carrying a title of One or more validation errors occurred., a status of 400 and an errors object.
  • errors holds one entry for every field that broke a rule and no entry for a field that broke none, so a payload breaking two rules names both fields and one breaking all three names all three. The key is the field's name as it is declared on the payload, GuestName, Seats and SessionCode, rather than the lower case spelling the request body uses, and the value lists that field's messages.

BookingEndpoints

MapBookingEndpoints opens the /bookings group and maps three of its endpoints. The fourth, POST /bookings/priority, is mapped onto that same group afterwards, once MapBookingEndpoints has returned.

Attach the filter to the group itself, so that it guards every endpoint the group holds however late that endpoint was mapped into it. Endpoints outside the group go on taking whatever they are sent, including a payload the group would refuse, and POST /bookings/drafts is one of them, so what the filter guards is the group's membership rather than the /bookings path.

Sample Requests & Responses

POST /bookings/holds

Request:

{
  "guestName": "Marta Vella",
  "seats": 2,
  "sessionCode": "CLY-204"
}

Response code: 200

{
  "stage": "hold",
  "guestName": "Marta Vella",
  "seats": 2,
  "sessionCode": "CLY-204"
}

The same call with guestName left out is refused, and the hold handler never runs:

Response code: 400

{
  "type": "https://tools.ietf.org/html/rfc9110#section-15.5.1",
  "title": "One or more validation errors occurred.",
  "status": 400,
  "errors": {
    "GuestName": [
      "A guest name is required."
    ]
  }
}

GET / answers 200 with Backend server running on port 8000.

More in Middleware, Filters and Error Handling

See the module
Global Exception HandlerThis problem covers turning every failure the application raises into one clear answer that says what went wrong and nothing about how the application is built.—No data yetmediumApi Rate LimiterThis problem covers holding a public endpoint to an allowance of calls per client inside a time window, turning away the calls that go past it while leaving the rest of the application alone.—No data yetmediumHardened Public API PipelineThis problem covers putting the guards of a public API together in one working order, so that bad input is turned away before any work starts, callers who ask too often are held back, failures come back as a tidy document that gives nothing away, and every answer can be traced back to the request that caused it.—No data yetmedium

We use cookies that are necessary to run Staffcoder, and optional analytics cookies to see which pages get used. Analytics stays off unless you accept. Read our Cookie Policy.