-
Notifications
You must be signed in to change notification settings - Fork 583
Helidon Declarative Validation #10755
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
base: main
Are you sure you want to change the base?
Conversation
f1a274e
to
f7ee02b
Compare
.../codegen/src/main/java/io/helidon/declarative/codegen/validation/ValidatedTypeGenerator.java
Outdated
Show resolved
Hide resolved
propertyName = propertyName.substring(3); | ||
if (propertyName.length() == 1) { | ||
propertyName = propertyName.toLowerCase(Locale.ROOT); | ||
} else if (!Character.isUpperCase(propertyName.charAt(1))) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is a mistake. You wanted the character at the index 0, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, if you add the check for the 4tch character to be upper case as I wrote before, this check would be redundant :-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not redundant, nor is it a mistake - this is for cases such as getADSL
- we want to keep all capital...
.../codegen/src/main/java/io/helidon/declarative/codegen/validation/ValidatedTypeGenerator.java
Outdated
Show resolved
Hide resolved
New module, fixes to codegen to support all required features (such as annotations on type declaration)
passes tests, checkstyle, spotbugs and copyright
…the machine, and cannot be changed
Removed as much as possible that can be hidden in the implementation from the public API. Added module to error handle validation exception into a bad request status code
06b0271
to
1a30719
Compare
Examples of generated files:
ValidatedType__Validated.java |
…issing dependencies. This resolves a pipeline build issue, where a module could be compiled before the codegen itself.
Resolves #10735
validation/tests/validation/src/test/java/io/helidon/validation/tests/validation/ValidationTest.java
and
validation/tests/validation/src/test/java/io/helidon/validation/tests/validation/ValidatorTest.java
for usage (not really examples, but what I am testing right now)
Declarative Validation
Validation provides capabilities to validate types and method parameters/return values.
Both features require code-generation.
Type validation code-generation of type validators is triggered by the presence of the
@Validation.Validated
annotation on a type.Method validation code-generation of interceptors is triggered by the presence of an annotation "meta-annotated"
with
@Validation.Constraint
on a method, its parameter, or type arguments of its parameters or return type.In addition the
Check.Valid
annotation also triggers code-generation, and can be used to validate against a type validatormentioned above.
Declaration
Annotations:
io.helidon.validation.Validation
class to trigger validation using an interceptor (all meta-annotated with@Validation.Constraint
)@Validation.Validated
- on a type to generate type validator for a typeConfiguration
There is currently no configuration for validation.
Implementation
Each constraint has a dedicated validator provider service, with (default-weight - 30) weight.
The providers are annotated with a named annotation that matches the constraint annotation.
This allows our users to override the implementation using their custom services.
The validation process works as follows:
ValidationException
if validation failsThe type validation works as follows:
@Validation.Validated
annotationImportant types:
Validation
- a container class for validation annotationsValidationException
- throws when validation fails in an interceptorValidator
- programmatic API to validate instances and their properties (only for validated types), can be obtained from service registryConstraintValidatorProvider
- service registry service that validates a single constraint annotation typeConstraintValidator
- created for each annotated element using the type of the element and the constraint annotationvalidators
package contains built-in constraint validator providersSupported concepts:
@Validation.Valid
is a constraint annotationOptional
,Map
,Collection
- i.e.List<@Validation.String.NotBlank String>
will trigger an interceptor and will be validated@Validation.Validated
will be validated when used in a method or constructor that uses@Validation.Valid
on the typed parameterfields, and type arguments
if the element was directly annotated with the constraints
@Validation.Constraint
), such annotationsmay also be meta-annotated with additional constraints; a custom constraint annotation requires a custom validator provider