Upgrading to 20.0
What changes in Spiderly 20.0.0, and what to do in your project for each change.
Overview
Spiderly 20.0.0 is a major release. In 20.0, a save runs in three steps: Normalize, Validate and Write. Each generated save and each generated upload can do a dry run. Each refusal points at its field, and the admin shows it there.
Six public contracts change. Each section below shows the contract before (19.x) and after (20.0), and what you must do.
| # | Change | Who must act |
|---|---|---|
| 1 | A unique violation answers 422 validation_failed, not 409 unique_violation. | Each client that reads a 409 from a save |
| 2 | Each fieldErrors key is a JSON Pointer into the request body. | Each client that reads fieldErrors, and your own throws |
| 3 | The admin shows a field error at its control. Only an error with no control is a toast. | Your custom admin forms |
| 4 | A refusal goes in Validate{Entity}Async, and normalizing in Normalize{Entity}. | Each OnBefore… hook that throws or changes the body |
| 5 | Each generated Save and upload takes dryRun. | Each override of a generated Save or upload |
| 6 | A refused upload answers 422 at /file, not 400. | Each client that reads a 400 from an upload |
To upgrade, update each Spiderly NuGet package and the spiderly npm package to 20.0.0. Then rebuild the backend, so the source generators write the new services and controllers. Then do the steps below.
The examples use a Book entity with a unique Isbn and an ordered list of Chapter items.
1. A unique violation answers 422
In 19.x, a save that broke a unique index answered 409 Conflict. The body held one message and no field.
In 20.0, Spiderly rolls the save back. Then it runs Normalize and Validate again in a new scope:
- When your Validate refuses the body, the response is that refusal, as a 422.
- When it does not refuse, Spiderly finds the fields of the violated index in the EF model. It answers a 422 field error with the message key
UniqueValueTaken. - When the model does not hold the index, the response is a 422 with a generic message.
Spiderly never retries the save. A foreign-key violation and a concurrency conflict still answer 409. The Exceptions page lists each status.
Before (19.x):
PUT /api/Book/SaveBook
HTTP/1.1 409 Conflict
{
"statusCode": 409,
"message": "A record with this value already exists.",
"errorCode": "unique_violation"
}After (20.0):
PUT /api/Book/SaveBook
HTTP/1.1 422 Unprocessable Entity
{
"statusCode": 422,
"message": "Validation failed.",
"errorCode": "validation_failed",
"fieldErrors": {
"/bookDTO/isbn": ["This value is already taken."]
}
}What to do:
- Find each client that reads a 409 or
unique_violationafter a save. Make it read a 422validation_failed, and take the message fromfieldErrors. - Translate the new
UniqueValueTakenmessage in each file of a language other than English. Until then, its English text shows (Translation). - For a clear message, refuse the clash in
Validate{Entity}Async(section 4). Then a clash in a race gets the same response as a plain clash. - Mark each unique column that the client never sends, for example a key that the write derives from another property, with
[DerivedFrom(nameof(Source))]. Then its error points at the source field (Exceptions).
2. Field error keys are JSON Pointers
In 19.x, a fieldErrors key was the bare property name, for example Isbn. The key did not tell which DTO or which list item held the field.
In 20.0, each key is a JSON Pointer (RFC 6901) into the request body, as RFC 9457 recommends. Each segment is the camelCase name that the body uses, or a list index. The empty key "" is the whole body. The generated FluentValidation errors use the same form.
Before (19.x):
{
"fieldErrors": {
"Isbn": ["This value is already taken."],
"Title": ["'Title' must not be empty."]
}
}After (20.0):
{
"fieldErrors": {
"/bookDTO/isbn": ["This value is already taken."],
"/orderedChaptersSaveBodyDTO/2/chapterDTO/title": ["'Title' must not be empty."]
}
}What to do:
- Make each client that reads
fieldErrorsuse the pointer. It can find the field without parsing the key. - Build each key that you throw with
JsonPointer, and never write it by hand:
// Before (19.x)
throw new SpiderlyValidationException(new Dictionary<string, string[]>
{
["Isbn"] = new[] { "This ISBN is already taken." }
});
// After (20.0)
FieldErrors fieldErrors = new();
string bookPointer = JsonPointer.Child("", nameof(BookSaveBodyDTO.BookDTO));
fieldErrors.Add(JsonPointer.Child(bookPointer, nameof(BookDTO.Isbn)), "This ISBN is already taken.");
fieldErrors.ThrowIfAny();JsonPointer.Child(pointer, index) adds a list index, for example /orderedChaptersSaveBodyDTO/2.
3. The admin shows a field error at its control
In 19.x, the admin's HTTP interceptor showed each 422 as one generic error toast. The form showed no field error.
In 20.0, the interceptor has a 422 branch. The form shows each message in red under the control at its pointer. The control gets a red border. The message clears when the user edits that control, and at the next save. The form scrolls to the first error and focuses it. An error with no control on the screen goes to the toast, with its own message. When the form holds that control but does not show it, the toast names the control's label, for example "Description: …".
Before (19.x):
Toast: "An error occurred" — "Our team has been notified…"
Form: no field shows an error.After (20.0):
ISBN [ 978-0-00-000000-0 ]
This ISBN is already taken. ← at the control of /bookDTO/isbn
Toast: an error whose pointer has no control, with its own message.What to do:
- Generated forms need no change.
- A custom form gets each field error when its controls follow the shape of the body. Give each control the property name that the body uses.
- Show each field with a Spiderly control component (
<spiderly-textbox [control]="…">and the others). A field that a plain<input [formControl]>shows is not on the screen for Spiderly, so its error goes to the toast. - A form that saves through
BaseFormComponent.onSaveneeds no change. A form that subscribes to its own save callsbaseFormService.showFieldErrors(formGroup, error)in its error callback. - Other code can show a 422 itself. It reads the errors with
FieldErrors.of(error)in its error callback. It callsclaim(pointer)there for each error that it shows. The toast shows each error that is not claimed. - Remove each
catchErrorthat read a 422 and showed its message. The form shows it now, so the user would see it twice.
4. Refusals go in Validate, normalizing in Normalize
In 19.x, a refusal went in a write hook: OnBeforeSave{Entity}AndReturnMainUIFormDTO, OnBefore{Entity}IsMapped, OnBefore{Entity}Insert or OnBefore{Entity}Update. These hooks run inside the write transaction. A dry run cannot run them.
In 20.0, each generated service has two new hooks:
Normalize{Entity}(saveBodyDTO)changes the body in memory, for example to trim or upper-case a value. It never reads or writes the database or the network.Validate{Entity}Async(saveBodyDTO)returns aFieldErrors. It may read the database and the network. It never writes.
A save runs authorization, then Normalize{Entity}, the generated validators, Validate{Entity}Async, and then the write hooks. Validate{Entity}Async runs only when the generated validators pass. When it returns a refusal, the save answers 422 and writes nothing. The OnBefore… hooks stay, but only for the write. The Backend Customization page lists each hook.
Before (19.x):
protected override async Task OnBeforeSaveBookAndReturnMainUIFormDTO(BookSaveBodyDTO saveBodyDTO)
{
saveBodyDTO.BookDTO.Isbn = saveBodyDTO.BookDTO.Isbn?.Trim();
bool taken = await _context.DbSet<Book>()
.AnyAsync(x => x.Isbn == saveBodyDTO.BookDTO.Isbn && x.Id != saveBodyDTO.BookDTO.Id);
if (taken)
throw new BusinessException("This ISBN is already taken.");
}After (20.0):
protected override void NormalizeBook(BookSaveBodyDTO saveBodyDTO)
{
saveBodyDTO.BookDTO.Isbn = saveBodyDTO.BookDTO.Isbn?.Trim();
}
protected override async Task<FieldErrors> ValidateBookAsync(BookSaveBodyDTO saveBodyDTO)
{
FieldErrors fieldErrors = new();
bool taken = await _context.DbSet<Book>()
.AnyAsync(x => x.Isbn == saveBodyDTO.BookDTO.Isbn && x.Id != saveBodyDTO.BookDTO.Id);
if (taken)
{
string bookPointer = JsonPointer.Child("", nameof(BookSaveBodyDTO.BookDTO));
fieldErrors.Add(JsonPointer.Child(bookPointer, nameof(BookDTO.Isbn)), "This ISBN is already taken.");
}
return fieldErrors;
}What to do:
- Move each refusal of a save out of its
OnBefore…hook intoValidate{Entity}Async. Return it under the pointer of its field, and do not throw aBusinessException. - Move each change of the body into
Normalize{Entity}, soValidate{Entity}Asyncsees the value that the write saves. - Keep each write in the
OnBefore…andOnAfter…hooks. Never write inValidate{Entity}Async: a dry run calls it. - Put a slow check, for example a file download, in
Validate{Entity}Async. Keep the upload of that file in the write hook. - Delete hooks do not change.
5. Each generated Save and upload takes dryRun
In 20.0, each generated Save endpoint and each generated upload endpoint takes dryRun in the query string. Its default is false, so a client that does not send it sees no change.
- A Save dry run authorizes, normalizes and validates the body as the save does. It opens no transaction and runs no write hook. It answers
204 No Content, or the same 422 that the save would answer. - An upload dry run reads the file and checks its size and its type (read from the content). For a raster image, it then runs
ValidateImageFor{Property}Of{Entity}. It runs noOnBefore…hook, keeps no file and writes no row. It answers 200 with an empty result, or a 422 at/file.
Before (19.x):
PUT /api/Book/SaveBook → writes the book
POST /api/Book/UploadCoverBlobForBook → keeps the filepublic virtual async Task<BookMainUIFormDTO> SaveBook(BookSaveBodyDTO saveBodyDTO)
public virtual async Task<string> UploadCoverBlobForBook([FromForm] IFormFile file)After (20.0):
PUT /api/Book/SaveBook?dryRun=true → 204, or 422 with fieldErrors; writes nothing
POST /api/Book/UploadCoverBlobForBook?dryRun=true → 200, or 422 at /file; keeps no filepublic virtual async Task<ActionResult<BookMainUIFormDTO>> SaveBook(BookSaveBodyDTO saveBodyDTO, [FromQuery] bool dryRun = false)
public virtual async Task<string> UploadCoverBlobForBook([FromForm] IFormFile file, [FromQuery] bool dryRun = false)What to do:
- Change the signature of each override of a generated Save or upload controller method, as above.
- In an override of a generated Save, send a dry run to the service's
DryRunSave{Entity}Async, and returnNoContent(). - Pass
dryRunto each generated upload service method that you call or override. - Never write in an override of
ValidateImageFor{Property}Of{Entity}: an upload dry run calls it. - To preview a write from a script or another client, send it with
?dryRun=true. Show the server's response, and do not copy its rules into the client.
6. A refused upload answers 422 at /file
In 19.x, an upload whose file was too large or of a wrong type answered 400 Bad Request, with one message and no field.
In 20.0, it answers 422 validation_failed, in a real upload and in a dry run. The message is under /file, the pointer of the multipart field. An accepted upload answers as before. The File Storage page shows the upload flow.
Before (19.x):
POST /api/Book/UploadCoverBlobForBook
HTTP/1.1 400 Bad Request
{
"statusCode": 400,
"message": "File size must not exceed 5 MB."
}After (20.0):
POST /api/Book/UploadCoverBlobForBook
HTTP/1.1 422 Unprocessable Entity
{
"statusCode": 422,
"message": "Validation failed.",
"errorCode": "validation_failed",
"fieldErrors": {
"/file": ["File size must not exceed 5 MB."]
}
}What to do:
- Find each client that reads a 400 from a generated upload. Make it read a 422
validation_failed, and take the message fromfieldErrors["/file"]. - The default
[ImageWidth]and[ImageHeight]check also answers 422 at/file. Your own override ofValidateImageFor{Property}Of{Entity}answers with the exception that it throws.
Checklist
- Update the Spiderly NuGet packages and the
spiderlynpm package to20.0.0, and rebuild (Overview). - Make each client read a unique clash as a 422
validation_failed, not a 409 (section 1). - Make each client read
fieldErrorskeys as JSON Pointers, and build your own keys withJsonPointer(section 2). - Remove each admin
catchErrorthat showed the message of a 422 (section 3). - Move each save refusal into
Validate{Entity}Async, and each body change intoNormalize{Entity}(section 4). - Change each override of a generated Save or upload to take
dryRun(section 5). - Make each client read a refused upload as a 422 at
/file, not a 400 (section 6).