Skip to content

Commit

Permalink
217-missing-validator-for-common-type (#219)
Browse files Browse the repository at this point in the history
* fix: css issue

* feat: include validators for additional types, such as Float, BigInt, and BigDecimal. These validators are used as base validations in Iron derivations.
  • Loading branch information
cheleb authored Oct 5, 2024
1 parent 131bebc commit 301827a
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 20 deletions.
20 changes: 16 additions & 4 deletions examples/client/src/main/scala/samples/SimpleSample.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@ package samples
import com.raquo.laminar.api.L.*
import dev.cheleb.scalamigen.*

import io.github.iltotore.iron.*
import io.github.iltotore.iron.constraint.all.*

val simple = {

case class Cat(name: String, weight: Int, kind: Boolean = true)
case class Cat(
name: String,
weight: Int,
hairsCount: BigInt :| GreaterEqual[100000],
kind: Boolean = true
)

val simpleVar = Var(Cat("Scala le chat", 6))
val simpleVar = Var(Cat("Scala le chat", 6, BigInt(100000).refineUnsafe))
Sample(
"Simple",
simpleVar.asForm,
Expand All @@ -19,9 +27,13 @@ val simple = {
}
),
"""
|case class Cat(name: String, weight: Int, kind: Boolean = true)
|case class Cat(
| name: String,
| weight: Int,
| hairsCount: BigInt :| GreaterEqual[100000],
| kind: Boolean = true)
|
|val simpleVar = Var(Cat("Scala le chat", 6))
|val simpleVar = Var(Cat("Scala le chat", 6, BigInt(100000).refineUnsafe))
|
|simpleVar.asForm
""".stripMargin
Expand Down
10 changes: 9 additions & 1 deletion examples/client/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,17 @@ body {
margin: 0;
}

.srf-form {
border: 1px solid gray;
}

.srf-panel {
border: 1px solid gray;
}

border: 1px solid #d9d9d9;
.srf-table {
border: 1px solid gray;
border-radius: 10px;
}

.srf-field {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ object Form extends AutoDerivation[Form] {
.renderPanel(panel.label)
.amend(
className := panel.panelCss,
cls := "srf-form",
// cls := "srf-form",
if panel.asTable then renderAsTable()
else renderAsPanel()
)
Expand Down
40 changes: 38 additions & 2 deletions modules/core/src/main/scala/dev/cheleb/scalamigen/Validator.scala
Original file line number Diff line number Diff line change
@@ -1,14 +1,50 @@
package dev.cheleb.scalamigen

import scala.util.Try

trait Validator[A] {
def validate(str: String): Either[String, A]
}

/** Validators for common types. They are the base validation used by Iron
* derivations.
*/
object Validator {

/** A validator for strings.
*/
given Validator[String] with
def validate(str: String): Either[String, String] =
Right(str)

/** A validator for doubles.
*/
given Validator[Double] with
def validate(str: String): Either[String, Double] =
str.toDoubleOption.toRight("Not a number")
str.toDoubleOption.toRight("Not a double")

/** A validator for integers.
*/
given Validator[Int] with
def validate(str: String): Either[String, Int] =
str.toIntOption.toRight("Not a number")
str.toIntOption.toRight("Not a int")

/** A validator for longs.
*/
given Validator[Float] with
def validate(str: String): Either[String, Float] =
str.toFloatOption.toRight("Not a float")

/** A validator for big integers.
*/
given Validator[BigInt] with
def validate(str: String): Either[String, BigInt] =
Try(BigInt.apply(str)).toEither.left.map(_.getMessage)

/** A validator for big decimals.
*/
given Validator[BigDecimal] with
def validate(str: String): Either[String, BigDecimal] =
Try(BigDecimal.apply(str)).toEither.left.map(_.getMessage)

}
20 changes: 9 additions & 11 deletions modules/core/src/main/scala/dev/cheleb/scalamigen/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package dev.cheleb.scalamigen
import com.raquo.airstream.state.Var

import com.raquo.laminar.api.L.*
import org.scalajs.dom.HTMLDivElement
import com.raquo.laminar.nodes.ReactiveHtmlElement
import org.scalajs.dom.HTMLElement

def stringForm[A](to: String => A) = new Form[A]:
override def render(
Expand Down Expand Up @@ -108,12 +108,11 @@ extension [A](va: Var[A])
*/
def asForm(using wf: WidgetFactory)(using
Form[A]
): ReactiveHtmlElement[HTMLDivElement] = {
): ReactiveHtmlElement[HTMLElement] = {
val errorBus = new EventBus[(String, ValidationEvent)]()
div(
cls := "srf-form",
Form.renderVar(va, () => ())(using wf, errorBus)
)
Form
.renderVar(va, () => ())(using wf, errorBus)
.amend(cls := "srf-form")
}

/** Render a form for the variable.
Expand All @@ -126,11 +125,10 @@ extension [A](va: Var[A])
*/
def asForm(errorBus: EventBus[(String, ValidationEvent)])(using
wf: WidgetFactory
)(using Form[A]): ReactiveHtmlElement[HTMLDivElement] =
div(
cls := "srf-form",
Form.renderVar(va, () => ())(using wf, errorBus)
)
)(using Form[A]): ReactiveHtmlElement[HTMLElement] =
Form
.renderVar(va, () => ())(using wf, errorBus)
.amend(cls := "srf-form")

/** Buid an error bus for the variable that will be used to display errors.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ object UI5WidgetFactory extends WidgetFactory:
_.headerText := headerText,
_.headerLevel := TitleLevel.H3
)
case None => Panel()
case None =>
div(cls := "srf-table")

override def renderSelect(f: Int => Unit): HtmlElement = Select(
_.events.onChange
Expand Down

0 comments on commit 301827a

Please sign in to comment.