驗證器與邊
領域驗證器把自己的規則加進匯入驗證。邊提供者則宣告 Core 參照掃描器看不到的連結。兩者都是選用的,需要時才實作。如果自訂型別的值裡藏著鍵值,兩者都需要。
新增領域驗證器
Core 的驗證固定為四種,分別是鍵值、參照、@overlap 與資源鍵值。這四種以外的規則,交給領域驗證器。
| 規則種類 | 例子 |
|---|---|
| 跨欄 | 若 type 為 Custom,script 必填 |
| 跨分頁 | 檢查參照到的記錄在意義上是否合理 |
實作 ISheetForgeValidatorPlugin,並逐一註冊驗證器。
using SheetForge.Core.Model;
using SheetForge.Core.Plugins;
using SheetForge.Core.Validation;
public sealed class SkillsPlugin : ISheetForgePlugin, ISheetForgeValidatorPlugin
{
// ... Name / RegisterEnums / RegisterCellParsers unchanged ...
public void RegisterValidators(DomainValidatorRegistry validators)
{
validators.Register(new CustomEffectRequiresScriptValidator());
}
}
public sealed class CustomEffectRequiresScriptValidator : IDomainValidator
{
public string Name => "CustomEffectRequiresScript";
public void Validate(DomainValidationContext ctx)
{
if (!ctx.Tables.TryGetValue("ExampleEffects", out var effects)) return;
if (!effects.Schema.TryGetField("script", out var scriptField)) return;
foreach (var rec in effects.Records)
{
if (!(rec["type"].Value is EnumValue ev) || ev.MemberName != "Custom") continue;
var scripts = rec["script"].AsList;
if (scripts != null && scripts.Count == 0)
ctx.Errors.Add(new ImportError(ImportErrorCode.DomainRuleViolation,
new CellCoordinate("ExampleEffects", rec.RowNumber, scriptField.ColumnNumber, "script"),
/* what */ rec["codeName"].Value.ToString(),
/* why */ "A Custom effect must specify a script to run, but 'script' is empty.",
/* how */ "Put a script address in the 'script' column, or change 'type'."));
}
}
}註冊好的驗證器自動加入匯入驗證,也加入編寫時的事前驗證。
驗證器的規則
違規一律以 ImportErrorCode.DomainRuleViolation 回報到 ctx.Errors。不要擲出例外。驗證器擲出的例外會由 SheetForge 攔下,改以 DomainRuleViolation 錯誤回報,其他驗證器照常執行。
錯誤的四個要素都要填。
| 要素 | 屬性 |
|---|---|
| 位置 | CellCoordinate |
| 內容 | ActualValue |
| 原因 | Expected |
| 處理方式 | Suggestion |
「處理方式」的文字原封不動顯示出來,成為告訴使用者怎麼處理的那句說明。
驗證器自動沿用匯入的兩條規則。第一,驗證一次收集所有問題,執行一次就全部列出。第二,任何地方只要有一個錯誤,就不會組裝中間表示(IR)。
情境提供的資料
ctx 把這次執行剖析好的資料交給規則使用。
| 成員 | 內容 |
|---|---|
Tables | 剖析好的所有表格 |
KeyIndices | 鍵值索引 |
AssetKeys | 資源鍵值。null 表示這次略過了資源驗證 |
其他活頁簿的分頁
ctx 也透過 ReferenceTables 與 ReferenceKeyIndices,提供專案裡其他活頁簿的唯讀分頁。沒有其他活頁簿時,兩者都是空的,不會是 null。
規則在這裡查鍵值的方式,和在 Tables 裡查的方式一樣。這些分頁的內容不會進入這次執行本身的結果。
專案怎麼容納多個活頁簿,詳見一個設定資源就是一個活頁簿。
新增邊提供者
邊提供者宣告 Core 參照掃描器看不到的邊,例如迷你語法的值裡參照到的能力值。
如果要在資料圖上建工具,或想讓記錄畫布畫出領域裡的連結,就宣告這些邊。
public sealed class SkillsPlugin : /* ... */, ISheetForgeEdgePlugin
{
public void RegisterEdgeContributors(EdgeContributorRegistry contributors)
{
contributors.Register(new ModifierStatEdgeContributor()); // effect → stat edges
}
}IEdgeContributor 拿到唯讀的跨分頁情境,並把 EdgeSpec 項目加進去。每個項目包含下列資訊:
- 起點與終點的分頁,以及各自的記錄 id。
- 欄位(選填)。
- 作為附帶內容的記錄。
- 標籤。
提供者不輸出診斷,因為邊是產生圖形投影的材料,不屬於驗證。詳見建構圖的型別。
值裡藏著鍵值的自訂型別
RecordId@Tab 是 Core 唯一看得懂的參照形狀。它不必額外設定,就享有四項服務:
- 完整性檢查。
- 圖上的邊。
- 最接近名稱的建議。
- 重新命名時連動更新。
自訂寫法可能把鍵值包在裡面,例如 attack:add:10、stat.hp>50 或 fire@0.4。這時 Core 只看到一個讀不懂的字串,這四項服務就進不到寫法裡面。
三個部分可以找回其中三項服務。這三個部分要成套地寫。只做了其中一個部分的寫法,可能匯入時沒有任何錯誤,裡面的鍵值卻沒有檢查,或在畫面上看不到。下表列出缺了每個部分會失去什麼。
三個部分
| 部分 | 合約 | 找回的功能 | 缺少時 |
|---|---|---|---|
| 1. 完整性 | IDomainValidator | 寫法裡不存在的鍵值會回報出來,指出座標並寫明處理方式 | 打錯字照樣匯入,到執行階段才出錯 |
| 2. 可見性 | IEdgeContributor | 埋在值裡的連結變成真正的邊 | 連結存在於資料裡,畫面上卻哪裡都看不到 |
| 3. 處理方式 | 部分 1 裡的 TextSuggestion.FindNearest | 與內建參照錯誤相同句型的建議 | 診斷正確,卻沒有辦法照著處理 |
有了部分 2,畫布就畫出這條邊,節點資訊面板的使用處數量把它算進去,參照索引也收錄它。
部分 3 產生的句子像這樣:「Unknown stat 'atack'. Did you mean 'attack'?」
驗證與建議寫在一起
部分 1 和部分 3 放在同一個類別裡。建議應該寫在驗證器裡,因為三個部分中只有驗證器會產生給人讀的句子。
// Piece 1 + 3 together — the validator is where the suggestion belongs, because it is the
// only one of the three that produces a sentence a person reads.
using SheetForge.Core.Model;
using SheetForge.Core.Validation;
public sealed class ModifierStatExistsValidator : IDomainValidator
{
public string Name => "ModifierStatExists";
public void Validate(DomainValidationContext ctx)
{
if (!ctx.KeyIndices.TryGetValue("Stats", out var stats)) return; // no target tab: nothing to check
if (!ctx.Tables.TryGetValue("Effects", out var effects)) return;
if (!effects.Schema.TryGetField("modifier", out var field)) return;
foreach (var rec in effects.Records)
foreach (string statKey in StatKeysIn(rec["modifier"])) // your notation's own split
{
if (stats.Contains(statKey)) continue;
string near = TextSuggestion.FindNearest(statKey, stats.Keys); // piece 3
ctx.Errors.Add(new ImportError(ImportErrorCode.DomainRuleViolation,
new CellCoordinate("Effects", rec.RowNumber, field.ColumnNumber, "modifier"),
/* what */ statKey,
/* why */ "This modifier points at a stat that does not exist in 'Stats'.",
/* how */ near != null
? "Did you mean '" + near + "'? Fix the stat name in the modifier value."
: "Add that record to 'Stats', or correct the stat name."));
}
}
}共用一個拆分器
寫法的拆分邏輯只寫一份,三方共用。剖析器、驗證器與邊提供者,對鍵值從哪裡開始、到哪裡結束的判斷必須一致。如果各自留一份拆分邏輯,三份遲早會對不上。
包裝型別天生就有這一點,因為 TrySplit 就是共用的拆分器。
重新命名時連動更新
第四項服務是重新命名時的連動更新,這一項還要多做一件事。重新命名記錄時,只有在 Core 能從文字裡找到鍵值的地方,參照它的儲存格才會改寫。Core 找得到的地方如下:
RecordId@Tab欄位。- 由這種欄位值組成的清單。
TrySplit把鍵值拆成獨立元素的包裝型別。
Core 無法自己猜出自訂語法裡子字串的邊界。處理的方式有兩種。
| 做法 | 要做的事 | 結果 |
|---|---|---|
| 告訴 Core 怎麼找 | 實作 IReferencingCellType | 只要加入這一項,就能取代整套三部分做法,一次找回全部四項服務 |
| 接受這個邊界 | 不必再做什麼 | 下次匯入時,部分 1 會回報已經失效的鍵值,並指出座標、提供建議 |
IReferencingCellType 的說明見參照對等。
如果接受這個邊界,重新命名就不會改寫寫法裡的鍵值。參照斷了會回報出來,不會默默放著。
仍需三部分做法的情況
欄沒有 @target 時,三部分做法仍然是正確的選擇,因為鍵值並不屬於某個特定的分頁。
隨附的範例裡,List<Modifier> 沒有指定目標,所以 Core 無從得知 attack 該解析到哪裡。ModifierStatEdgeContributor 手動建立這些邊。
替欄加上目標,例如 List<Modifier@Stats>,之後就改由參照對等處理。