Set UnmappedMemberHandling to Disallow to catch typos like prot instead of port.
dotnet add package OneYamlTargets .NET 8 and .NET 10 (used in the download).
using System; using OneYaml; var options = new YamlSerializerOptions { UnmappedMemberHandling = YamlUnmappedMemberHandling.Disallow, SourceName = "app.yaml", }; try { YamlSerializer.Deserialize<App>( "host: api\nprot: 8080\n", options); } catch (YamlException error) { Console.WriteLine(error.Message); } record App(string Host, int Port = 80);
Unzip the example, then dotnet run.
dotnet run prints:
app.yaml: The key 'prot' is not a member of App. (line 2, column 1, path /prot)Deserialize throws YamlException at the unexpected key. app.yaml is a label set by SourceName; this example reads the YAML from the string shown in the code, not from disk.
Variations
Allow different capitalization, but still reject typos. Add PropertyNameCaseInsensitive = true, to the options and replace the contents of the try block with:
var app = YamlSerializer .Deserialize<App>( "HOST: api\nPORT: 8080\n", options)!; Console.WriteLine(app.Port);
8080Related recipes
The short answer
An unmapped key is a YAML key that matches no member of the C# type you’re reading into. SourceName adds the filename or path you supply to the error message, so readers know which file to fix. With Disallow, the application gets an error instead of an object that silently uses defaults. Catch YamlException to show the key, its line and column, and its YAML path.
What happens to unknown YAML properties during deserialization?
The default is Skip. In this example host matches Host, but prot matches nothing, so skipping the typo would leave Port at 80 rather than set it to 8080. Strict binding refuses that input. This is not a YAML syntax error: prot: 8080 is a valid YAML entry. The error depends on which C# type you ask OneYaml to bind it to.
Before treating every unexpected key as a typo, check how your model names its keys. The default naming policy maps Port to port and matches case. If you change the policy or use [YamlPropertyName], the resulting YAML name is the one that must match. Turning on case-insensitive matching lets PORT match port; it still won’t make prot a match.
How do I reject extra YAML keys without requiring every property?
Disallow checks keys that are present. It doesn’t require port when there’s no such key in the input. Keep defaults for settings that are genuinely optional. For a setting the user must supply, mark its member with [YamlRequired]. You can use both controls on the same model: one catches a key you didn’t expect, the other catches a key you did expect but didn’t get.
Use strict binding when the app owns the file format and a typo should stop startup. For a file shared with another version of the app, rejecting every new key means an older reader may refuse a file written by a newer one. If you need to accept and write back extra plugin settings, capture them as extension data instead of using this strict model.