-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
217-missing-validator-for-common-type (#219)
* 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
Showing
6 changed files
with
75 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 38 additions & 2 deletions
40
modules/core/src/main/scala/dev/cheleb/scalamigen/Validator.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters