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

Annotation to provide validation features on top of types #239

Open
jclark opened this issue Jun 22, 2019 · 7 comments
Open

Annotation to provide validation features on top of types #239

jclark opened this issue Jun 22, 2019 · 7 comments
Labels
Area/Langext Relates to annotations providing language extras graphql Relevant to GraphQL support Type/Improvement Enhancement to language design
Milestone

Comments

@jclark
Copy link
Collaborator

jclark commented Jun 22, 2019

We could get quite a lot of mileage from a simple @validate annotation with fields describing the kinds of validation to perform, e.g.

@validate { minInclusive = 1 };
type PositiveInt int;

For numeric types we could follow XML Schema part 2 and allow:

  • minInclusive
  • maxInclusive
  • minExclusive
  • maxExclusive

Other obvious things:

  • maxLength (for anything with a length, including strings and lists)
  • minLength (similarly)
  • pattern/regexp (on strings)

A more general and sophisticated way of doing this would be refinement types (#238), but that is orders of magnitude more work.

This doesn't deal with cases where you really want to transform the data as well notably:

  • date/time-related types including durations
  • strings that represents lists using whitespace or other delimiter to delimit items (as with XML Schema 2 list datatype)
@jclark jclark added Type/Improvement Enhancement to language design Area/Langext Relates to annotations providing language extras labels Jun 22, 2019
@jclark jclark added this to the 2019Rn milestone Jun 22, 2019
@jclark
Copy link
Collaborator Author

jclark commented Jan 13, 2020

This can also be useful in conjunction with automated data mapping.

@validate is perhaps the wrong word, given that validation is now only one use case. Maybe @constrain would be more neutral.

@sanjiva
Copy link
Contributor

sanjiva commented Jan 13, 2020

+1.

@srinathperera
Copy link

Looking from just data mapping point of view, statistics such as mean, standard deviation etc also useful ( for numerical fields).

Also, another angle is that although user does not provide this, in Choreo, we can trace, measure, and annotate in the background.

@jclark
Copy link
Collaborator Author

jclark commented Jun 29, 2020

This is potentially relevant to GraphQL. Simple types with validation constraints can be exposed as user-defined scalars.

@jclark jclark added the graphql Relevant to GraphQL support label Jun 29, 2020
@jclark
Copy link
Collaborator Author

jclark commented Sep 10, 2020

I think we should perhaps do this in a more first-class way using refinement types #238.

@jclark
Copy link
Collaborator Author

jclark commented Feb 5, 2022

@shafreenAnfar mentions JSR 303 https://beanvalidation.org/1.0/spec/

@shafreenAnfar
Copy link
Contributor

shafreenAnfar commented Feb 5, 2022

Yes. And Bean Validation 2.0 is available under https://beanvalidation.org/2.0/spec/ (formerly JSR 380). I feel Bean validation is an integral part of writing REST APIs with Spring Boot.

The list of built-in constraints can be found here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area/Langext Relates to annotations providing language extras graphql Relevant to GraphQL support Type/Improvement Enhancement to language design
Projects
None yet
Development

No branches or pull requests

4 participants