Skip to content

Conversation

@bargergo
Copy link
Contributor

String length can be validated using annotations (Length, MaxLength or MinLength)

Copy link
Collaborator

@Wicpar Wicpar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems OK to me except for the added model, as it won't work because the model generated for string is an instance of SchemaModelLitteral so it will always fail at if (model is SchemaModel.SchemaModelString && types.contains(type))
To test it the best is to launch the test server from the tests

@Wicpar
Copy link
Collaborator

Wicpar commented May 25, 2020

I'm not quite sure how to edit your code while preserving your contribution history...

@bargergo bargergo changed the title Add string length validators, string schemamodel Add string length validators, add string attributes to SchemaModelLitteral May 25, 2020
@bargergo bargergo requested a review from Wicpar May 25, 2020 21:38
@bargergo bargergo changed the title Add string length validators, add string attributes to SchemaModelLitteral Add string length validators May 26, 2020
@bargergo
Copy link
Contributor Author

bargergo commented May 27, 2020

Please let me know if more changes are needed, or something is missing.

@Wicpar
Copy link
Collaborator

Wicpar commented May 27, 2020

Hi @bargergo , Sorry for the delay,
I went a bit over my head on a freelance mission and am on the brink of falling behind schedule, it may take until next Wednesday to get any spare time, is it essential to you that the changes are added to the main repo before then ?

@bargergo
Copy link
Contributor Author

No problem, it can wait until next Wednesday.

I have some more ideas:

  • string pattern validator using regular expressions
  • common ValidationException base class for simpler exception handling with StatusPages
  • optional error message parameter for all built-in validators
  • add example usage of the new validators to the test server

Can I add them to this PR?

@Wicpar
Copy link
Collaborator

Wicpar commented May 28, 2020

Yes those changes would be great :)

@bargergo bargergo changed the title Add string length validators Add validators for strings May 28, 2020
@bargergo bargergo force-pushed the string-length-validators branch from 2948bbc to a3c2171 Compare May 28, 2020 21:22
Comment on lines -300 to +303
class B(val i: @Min(0) @Max(2) Int) : Base()
class B(@Min(0) @Max(2) val i: Int) : Base()

@WithExample
class C(val l: @Clamp(0, 10) Long) : Base() {
class C( @Clamp(0, 10) val l: Long) : Base() {
Copy link
Contributor Author

@bargergo bargergo May 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Were these annotations used on purpose between the name and the type of the constructor parameter? I modified this, because it caused problems. This way the annotation instance didn't have an errorMessage property, only a value property.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, at the beginning the annotations were only supported on types due to some issue, i believe it is fixed now and didn't really change that to the more natural syntax.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, it was new for me. I didn't see any annotations on types before.
It seems like the annotation parameters with default values are ignored if I use the annotation on the type.

It causes this error on start:

kotlin.reflect.jvm.internal.KotlinReflectionInternalError: Method is not supported: public abstract java.lang.String com.papsign.ktor.openapigen.annotations.type.string.length.Length.errorMessage() (args: [])

In the getConstraint method of the LengthProcessor class I can see with the debugger, that there are values for min and max only, the errorMessage is missing.

@Wicpar Wicpar merged commit 729aef7 into papsign:master Jun 3, 2020
@bargergo
Copy link
Contributor Author

bargergo commented Jun 3, 2020

Thanks for the merge, but I think there is one issue that should be solved somehow.

The annotation parameters with default values (errorMessage) are ignored when the annotation is used on a type and no value is specified. This leads to a runtime error when starting the application, because these parameters are missing from the annotation, when it is processed.

Solution 1: Remove type from annotation targets in validation annotations, so only properties can annotated
Solution 2: The user of the annotations should always specify all parameters, when annotating type

I think the first solution would be better, because users tend to forget such rules and I don't know if there is a reason to keep the option to annotate types.

@Wicpar
Copy link
Collaborator

Wicpar commented Jun 4, 2020

It would not be the first time this library gets the kotlin team to work on their bugs.
Option 3: create an issue: https://youtrack.jetbrains.com/issue/KT-39369

@bargergo
Copy link
Contributor Author

bargergo commented Jun 4, 2020

Ok, this option didn't occur to me, but yes, this can be a good solution. :)

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.

2 participants