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

Adding Type and Nullability Validation to exposed fields #386

Open
dhruvCW opened this issue Jun 22, 2024 · 6 comments
Open

Adding Type and Nullability Validation to exposed fields #386

dhruvCW opened this issue Jun 22, 2024 · 6 comments

Comments

@dhruvCW
Copy link

dhruvCW commented Jun 22, 2024

Hello 👋

I am creating this ticket to ask if it would be possible to introduce type validation to exposed fields.

Currently there is not checking if the exposed fields are of a specified type. We can add documentation to specify a type (that is then exposed as part of the swagger docs) but if there is a mismatch there is no way for the server to validate this break in contract.

I am thinking we can add type validation (using dry-types or dry-validation) to the exposed fields (maybe after formatting). This would of course be opt-in with the expose DSL method accepting two new options

type (this is the type the field should be or be coercible to) a dry type or standard ruby type like Numeric.
nullable a boolean field to validate if the value can be nil or not.
These options then can be used to populate the swagger documentation automatically without specifying them in the documents hash.

I am happy to start contribute this feature but first want to check if the maintainers @dblock & @LeFnord are okay with it 😅

@dblock
Copy link
Member

dblock commented Jun 24, 2024

Does params: API::Entities::Status.documentation not expose those parameters with validation (Grape has extensive support for that)?

And yes, validation would be definitely very useful if the above doesn’t work. When used with grape I’d want to rely on its type checking mechanisms for consistency.

@LeFnord
Copy link
Member

LeFnord commented Jun 28, 2024

mmh, grape-entity is a presenting layer, so the data should be valid before it will be presented,
but yeah, if want to have a validation in place it should be based on grape validations

@dblock
Copy link
Member

dblock commented Jun 30, 2024

@LeFnord i think the idea is that you can use entities to receive data in the same format as presented

@dhruvCW
Copy link
Author

dhruvCW commented Jun 30, 2024

mmh, grape-entity is a presenting layer, so the data should be valid before it will be presented

While I tend to agree. In the case of grape-entity the entity is effectively a presentation as well as DTO entity. Thus I think having basic validation around the coerced type as well as nullability would be useful.

@dhruvCW
Copy link
Author

dhruvCW commented Jun 30, 2024

Does params: API::Entities::Status.documentation not expose those parameters with validation (Grape has extensive support for that)?
And yes, validation would be definitely very useful if the above doesn’t work. When used with grape I’d want to rely on its type checking mechanisms for consistency.

Ah I see there is some confusion. I agree the parameters can indeed be modeled with grape-entity, but this is about validating the data that is produced by an API rather than what is received. This is particularly useful in situations where the data source is not typed (from another upstream API or redis, etc).

@dhruvCW
Copy link
Author

dhruvCW commented Jun 30, 2024

When used with grape I’d want to rely on its type checking mechanisms for consistency.

could you elaborate how this would work 🤔

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

3 participants