[Ledger-API] Stricter version of the ValueValidate (#15650)

that does not strip trailling 0 when validating Numeric
This commit is contained in:
Remy 2022-11-22 17:45:14 +01:00 committed by GitHub
parent ff6e776da0
commit 8358aab22c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -15,10 +15,13 @@ import io.grpc.StatusRuntimeException
import scalaz.std.either._
import scalaz.syntax.bifunctor._
object ValueValidator {
abstract class ValueValidator {
import ValidationErrors._
import FieldValidations._
protected def validateNumeric(s: String): Option[Numeric]
private[validation] def validateRecordFields(
recordFields: Seq[api.RecordField]
)(implicit
@ -45,9 +48,6 @@ object ValueValidator {
fields <- validateRecordFields(rec.fields)
} yield Lf.ValueRecord(recId, fields)
private val validNumericString =
"""[+-]?\d{1,38}(\.\d{0,37})?""".r.pattern
def validateValue(v0: api.Value)(implicit
contextualizedErrorLogger: ContextualizedErrorLogger
): Either[StatusRuntimeException, domain.Value] = v0.sum match {
@ -56,15 +56,12 @@ object ValueValidator {
.fromString(cId)
.bimap(invalidArgument, Lf.ValueContractId(_))
case Sum.Numeric(value) =>
def err =
invalidArgument(s"""Could not read Numeric string "$value"""")
if (validNumericString.matcher(value).matches())
Numeric
.fromUnscaledBigDecimal(new java.math.BigDecimal(value))
.left map (_ => err) map Lf.ValueNumeric
else
Left(err)
validateNumeric(value) match {
case Some(numeric) =>
Right(Lf.ValueNumeric(numeric))
case None =>
Left(invalidArgument(s"""Could not read Numeric string "$value""""))
}
case Sum.Party(party) =>
Ref.Party
.fromString(party)
@ -161,6 +158,25 @@ object ValueValidator {
}
// Standard version of the Validator use by the ledger API
object ValueValidator extends ValueValidator {
private[this] val validNumericPattern =
"""[+-]?\d{1,38}(\.\d{0,37})?""".r.pattern
protected override def validateNumeric(s: String): Option[Numeric] =
if (validNumericPattern.matcher(s).matches())
Numeric.fromUnscaledBigDecimal(new java.math.BigDecimal(s)).toOption
else
None
}
// Version of the ValueValidator that is stricter for syntax for Numeric but preserves their precision.
// Use by canton's Repair service
object StricterValueValidator extends ValueValidator {
protected override def validateNumeric(s: String): Option[Numeric] =
Numeric.fromString(s).toOption
}
object NoLoggingValueValidator {
def validateRecord(rec: api.Record): Either[StatusRuntimeException, Lf.ValueRecord] =