Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support Validator preservation #3408

Open
eanea opened this issue Dec 18, 2023 · 1 comment
Open

support Validator preservation #3408

eanea opened this issue Dec 18, 2023 · 1 comment
Assignees

Comments

@eanea
Copy link

eanea commented Dec 18, 2023

Tapir version: 1.9.5
Scala version: 2.13.12

Describe the bug
Can't find a way to keep the validator in case of an optional field

How to reproduce?
https://scastie.scala-lang.org/GVBxeZypSA6DiBcUa1dFGw

Additional information
it will be great if validator.contramap supported option transformation:

None => Validator.pass
Some(enum) => schema.validator
@adamw
Copy link
Member

adamw commented Dec 21, 2023

I'm not sure what you're trying to achieve, but I think there are two ways. In your code above you explicitly use .get in the .map invocation, so that's what is being used.

Option 1 is to use the macro-based derivation:

case class LongString(@validate(Validator.minLength(3)) v: String)
  case class OptionWrapper[A](opt: Option[A])

  val s = implicitly[Schema[OptionWrapper[LongString]]]

  println(s.applyValidation(OptionWrapper(Some(LongString("aaa")))))
  println(s.applyValidation(OptionWrapper(Some(LongString("aa")))))
  println(s.applyValidation(OptionWrapper(None)))

this prints (as expected)

List()
List(ValidationError(MinLength(3,false),aa,List(FieldName(opt,opt), FieldName(v,v)),None))
List()

Option 2 is more manual, and involves mirroring what implicit Option schema derivation does - constructing an SOption schema, with the provided toOption method:

val longStringSchema = Schema.schemaForString.validate(Validator.minLength(3))
  val longStringWrappedSchema: Schema[OptionWrapper[String]] = Schema(
    schemaType = SOption(longStringSchema)(_.opt),
    isOptional = true,
    format = longStringSchema.format
  )

@adamw adamw self-assigned this Dec 22, 2023
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

No branches or pull requests

2 participants