Skip to content

Add support for default values in Forms4s#19

Open
LSUDOKO wants to merge 1 commit intobusiness4s:mainfrom
LSUDOKO:add_support_for_default_value
Open

Add support for default values in Forms4s#19
LSUDOKO wants to merge 1 commit intobusiness4s:mainfrom
LSUDOKO:add_support_for_default_value

Conversation

@LSUDOKO
Copy link

@LSUDOKO LSUDOKO commented Jan 27, 2026

implements support for default values in both Scala-derived forms and JSON Schema-derived forms. It ensures that default values defined in case classes or JSON schemas are correctly used to pre-fill forms on creation

forms4s-jsonschema:
Updated
FormFromJsonSchema.scala
to extract the default value from JSON Schema properties (specifically handling ExampleSingleValue) and pass it to the FormElement core.
Added a new test case in
FormFromJsonSchemaSpec.scala
to verify that default values from a JSON schema are correctly propagated to the generated form elements.

all tests passed
solve issue #8

Summary by CodeRabbit

  • New Features

    • Added support for default values in form elements.
    • Automatically derives defaults from case class parameters.
    • Reads and applies defaults from JSON Schema definitions.
    • Form initialization now populates fields with their configured defaults.
  • Tests

    • Added tests verifying default value propagation in generated form elements.
    • Added tests ensuring initial form state reflects specified defaults from models and JSON Schema.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link

coderabbitai bot commented Jan 27, 2026

📝 Walkthrough

Walkthrough

This PR adds compile-time default value extraction and propagation throughout the forms4s library. A new macro-based DefaultValues object extracts defaults from case classes, which are then threaded through form element creation and state initialization, replacing hardcoded defaults with element-specific values.

Changes

Cohort / File(s) Summary
Macro infrastructure
forms4s-core/src/main/scala/forms4s/DefaultValues.scala
New compile-time macro object that extracts default-valued fields from case classes using Scala 3 reflection, producing a Map[String, Any] of field names to their default value method references.
Form element structure
forms4s-core/src/main/scala/forms4s/FormElement.scala
Added optional defaultValue: Option[Any] = None parameter to the Core case class to carry element-level default values.
Form state & derivation
forms4s-core/src/main/scala/forms4s/FormElementState.scala, forms4s-core/src/main/scala/forms4s/ToFormElem.scala
Updated form state initialization to derive defaults from element cores instead of hardcoded literals (Text, Select, Checkbox, Number). Extended ToFormElem to invoke DefaultValues.extract, compute per-field defaults, and propagate them via updated updateCore signature.
JSON schema integration
forms4s-jsonschema/src/main/scala/forms4s/jsonschema/FormFromJsonSchema.scala
Enhanced to extract default values from JSON schema and pass them into FormElement.Core constructor.
Test coverage
forms4s-core/src/test/scala/forms4s/ToFormElemTest.scala, forms4s-jsonschema/src/test/scala/forms4s/jsonschema/FormFromJsonSchemaSpec.scala
Added tests validating that default values are correctly extracted from case class definitions and JSON schemas, and properly initialized in form elements and form state.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Poem

🐰 Defaults now flow through every form,
Extracted at compile time, fresh and warm,
From case class to state, each field knows its way,
No more hardcoded values to decay! 🎉

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and directly summarizes the main feature addition across the entire changeset.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Comment @coderabbitai help to get the list of available commands and usage tips.

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.

1 participant