Skip to content

Add fluent Assertion builder for type-safe assertion definitions#38

Closed
gonace wants to merge 3 commits intomasterfrom
claude/review-readme-development-WXFHs
Closed

Add fluent Assertion builder for type-safe assertion definitions#38
gonace wants to merge 3 commits intomasterfrom
claude/review-readme-development-WXFHs

Conversation

@gonace
Copy link
Copy Markdown
Owner

@gonace gonace commented Mar 29, 2026

Summary

Implements a fluent builder API for Cronitor assertions, providing type safety and discoverability when defining monitor assertions instead of using raw strings.

Key Changes

  • New Assertion class in Cronitor.Constants with fluent builder pattern

    • Assertion.Metric provides builders for Duration, Count, and ErrorCount
    • Assertion.Response provides builders for Code, Time, Body, and parameterized Json() and Header() methods
    • Each builder supports comparison operators: Equals(), LessThan(), GreaterThan(), and Contains()
  • New AssertionRule class to represent assertion expressions

    • Implements implicit conversions to/from string for backward compatibility
    • Custom JSON converter (AssertionRuleConverter) for proper serialization/deserialization
    • Decorated with [JsonConverter] attribute for automatic handling
  • Updated Monitor.Assertions property to use IEnumerable<AssertionRule> instead of IEnumerable<string>

    • Maintains backward compatibility through implicit string conversion
  • Updated tests and builders to use the new fluent API

    • Added comprehensive AssertionTests class with 11 test cases covering all assertion types and serialization
    • Updated MonitorTypeTests, JobBuilder, and request tests to use AssertionRule type
  • Updated README with usage examples and marked the assertion language feature as complete

Notable Implementation Details

  • The AssertionBuilder class uses a private constructor pattern to enforce fluent API usage
  • Assertion strings are built dynamically with proper spacing and operator placement
  • JSON serialization treats AssertionRule objects as simple strings for API compatibility
  • The builder supports both simple assertions (e.g., metric.duration < 30s) and parameterized ones (e.g., response.json user.count > 10)

claude added 2 commits March 29, 2026 05:54
Implement a fluent API for building Cronitor assertion strings with
type safety and discoverability, replacing raw string construction.

https://claude.ai/code/session_01XFQ49AHWaNZH3pyffR78LN
The Assertions property now accepts AssertionRule objects instead of
raw strings. AssertionRule serializes to/from JSON as a plain string
via a custom JsonConverter, so the API payload is unchanged. Implicit
conversions between string and AssertionRule are supported for
backwards compatibility.

https://claude.ai/code/session_01XFQ49AHWaNZH3pyffR78LN
@gonace gonace force-pushed the claude/review-readme-development-WXFHs branch from c725347 to 772c627 Compare March 29, 2026 06:08
The AssertionContainer test class lacked a [JsonPropertyName] attribute,
so System.Text.Json could not match the lowercase JSON key to the
PascalCase property, leaving it null.

https://claude.ai/code/session_01XFQ49AHWaNZH3pyffR78LN
@gonace gonace closed this Mar 29, 2026
@gonace gonace deleted the claude/review-readme-development-WXFHs branch March 29, 2026 06:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants