Required values and Unique

Make members optional with defaults or nullable types, and fail the build on duplicate slugs with Unique.

On this page
csharp
public sealed record Guide(
    [Unique(IgnoreCase = true)] string Slug,
    string Title,
    string Summary = "",
    string? Subtitle = null);

Required and optional values

Every format (JSON, YAML, XML, Markdown) is strict. A value without a default and without ? is required. A missing one fails the build with BEVEL092. Give the parameter a default (string Summary = "") or make it nullable (string? Summary) to make it optional.

  • Optional: a constructor parameter with a default value, a nullable member (string?, int?), members Bevel fills (Slug, SourcePath, RawMarkdown for Markdown), [Reference] members (they have their own Required setting) and [Process] members.
  • Required: any other constructor parameter, and required properties. A null for a non-nullable member counts as missing.
  • When the item type has a record-level [Process], the check before processing is skipped, because the processor completes the item.
  • After all processors ran, any non-nullable reference-type member (string, arrays, lists, classes) that is still null fails with BEVEL092. [Reference] members are exempt. Value types are not checked, since a default cannot be told from a value.

Unique values

[Unique] requires a member's value to be unique across a list dataset. Put it on a constructor parameter or a property. It works on any [DataSource] list: Markdown, JSON, YAML and XML. Datasets from C# loaders are not checked.

csharp
public sealed record Post([Unique(IgnoreCase = true)] string Slug, [Unique] string Title, string RawMarkdown);
  • A duplicate fails the build with BEVEL079: Dataset 'Posts': 'Slug' value 'intro' is used by 'a.md' and 'guides/intro/index.md'. Item names are the SourcePath when the record has one, else item #n.
  • IgnoreCase defaults to false. Strings compare with Ordinal, or OrdinalIgnoreCase when true. Other types use their default equality. IgnoreCase = true on a non-string member fails with BEVEL091.
  • Several members can be [Unique]. Each is checked on its own.
  • The check runs after processors and OrderBy, on the final values. Null values are skipped.
  • Without IgnoreCase, Intro and intro are different values. Bevel warns with BEVEL089 because file names and URLs built from them overwrite each other on Windows and macOS. It is a real MSBuild warning at the attribute, so WarningsAsErrors works. Use IgnoreCase = true for slugs.
  • [Unique] on a single-object dataset fails with BEVEL090.
  • Declare it in one place, like the other member attributes (BEVEL078).