kotlin-sum-types

Model Kotlin validation outcomes as sealed Valid/Invalid types with create() parsers.

14|2|Updated Sep 10, 2022
One-click install
npx skills add https://github.com/anderssv/the-example --skill kotlin-sum-types
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: kotlin-sum-types
Source: https://github.com/anderssv/the-example/tree/main/skills/domain/kotlin-sum-types
Command: npx skills add https://github.com/anderssv/the-example --skill kotlin-sum-types

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

It solves the problem of scattered validation and unsafe domain logic by forcing external input to be parsed into explicit valid/invalid types at system boundaries.

Core Features & Use Cases

  • Sealed valid/invalid modeling: Represents data as Valid* and Invalid* states so domain functions can safely assume correctness.
  • Parse, don’t validate: Uses factory create() methods (often with @JsonCreator) to transform untyped data into typed domain objects in one place.
  • Error collection with paths: Collects multiple validation errors while preserving original invalid values and composing nested error paths (e.g., email, address.city).
  • Controller-style boundary handling: Uses exhaustive when statements to translate validated domain results into OK/error responses.

Quick Start

Ask an AI to explain how to model a JSON payload as a sealed Kotlin type with a create() parser that returns both valid and invalid states while collecting field-specific error messages.

Frequently Asked Questions about kotlin-sum-types

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I model JSON deserialization in Kotlin to prevent invalid domain data?

Model JSON deserialization by using sealed classes with explicit Valid and Invalid state variants. Factory create() methods parse untyped input into type-safe domain objects at system boundaries, ensuring invalid data is unrepresentable.

What is the parse don't validate approach for type-safe validation in Kotlin?

Parse don't validate transforms untyped external data into typed domain objects in one place. Factory create() methods with @JsonCreator enforce validation at boundaries, returning sealed Valid or Invalid states rather than scattering checks across domain logic.

How do I aggregate multiple validation errors with nested paths in Kotlin?

Aggregate validation errors by implementing interfaces like InvalidDataClass to collect field-specific messages. This preserves original invalid values and composes nested error paths such as address.city while validating nested domain structures.

How do I handle controller responses with exhaustive when logic for Kotlin sealed classes?

Handle controller responses by using exhaustive when statements to evaluate sealed valid and invalid types. This translates validated domain results into OK or error responses, ensuring all possible validation outcomes are explicitly covered.

Can I use this domain modeling approach for nested JSON structures in Kotlin?

Yes, this approach supports nested JSON structures by composing nested validations within sealed class hierarchies. Factory create() parsers recursively process nested input, collecting field-specific errors with composed paths like email or address.city.

Why should I use sealed classes for Kotlin domain modeling instead of standard data classes?

Sealed classes force input to be parsed into explicit valid or invalid states at system boundaries. Unlike standard data classes, this allows domain functions to safely assume data correctness and guarantees invalid states are unrepresentable.