bufbuild / protovalidate

Protocol Buffer Validation - Go, Java, Python, and C++ Beta Releases!
https://buf.build/bufbuild/protovalidate
Apache License 2.0
932 stars 37 forks source link

[Feature Request] Typescript Support #67

Open elliotmjackson opened 1 year ago

elliotmjackson commented 1 year ago

Feature description: Introduce TypeScript support to the protovalidate library for performing runtime validation of Protocol Buffers messages in TypeScript projects. This was a feature request in protovalidates predecessor bufbuild/protoc-gen-validate#71

Problem it solves or use case: The absence of TypeScript support in the protovalidate library hinders developers who work with Protocol Buffers and TypeScript. By adding TypeScript support, developers would be able to validate Protocol Buffers messages at runtime within their TypeScript applications, enhancing data integrity and reducing potential bugs related to data validation.

Proposed implementation or solution: The proposed solution involves extending the existing protovalidate library to include TypeScript bindings and support. This includes defining TypeScript type definitions that correspond to Protocol Buffers message structures, and implementing runtime validation functions that work seamlessly with TypeScript types.

Contribution: I am willing to contribute to this feature request by actively participating in discussions, assisting in defining TypeScript type definitions, and collaborating on the implementation of runtime validation functions for TypeScript. If necessary, I can also assist in writing tests and documentation to ensure the feature is thoroughly covered and well-documented.

Examples or references: While protovalidate doesn't currently have TypeScript support, similar libraries like protobufjs and protobuf-ts provide inspiration for how TypeScript bindings and runtime validation can be integrated for Protocol Buffers messages in a type-safe manner.

Additional context: Enabling TypeScript support in the protovalidate library would bridge the gap for developers who use Protocol Buffers in TypeScript projects, allowing them to perform runtime validation without relying on separate code generation processes. This aligns with the trend of utilizing TypeScript's strong typing and safety features in modern software development.

nickwinger commented 1 year ago

Good to hear that, maybe i can also contribute. It seems like zod is popular these days, you would instantly also have validation up to the frontend: https://zod.dev/

akosyakov commented 1 year ago

@elliotmjackson Hi there! Is there already something available to try out?

nicksnyder commented 1 year ago

Working on this is on our roadmap, but progress is blocked on a solution to https://github.com/bufbuild/protobuf-es/issues/397 (which is also on our roadmap). We will share any updates when we have them.

kwhitehouse commented 11 months ago

Hi there! Having typescript support for protovalidate would be fantastic.

I noticed in the linked blocker above that @protobuf-ts correctly includes options in the generated typescript. I was able to successfully test this out locally, such that my generated Typescript code contained custom proto options I defined using protovalidate syntax.

Would you folks be open to adding library functions that don't depend on protobuf-es while that blocker exists? For example, instead of providing a function public ValidationResult validate(Message msg) within the library, you could instead expect that:

I believe with this structure, your library wouldn't have any need to inspect what options were defined on a given proto message. Instead, clients would be responsible for doing this themselves, and your protovalidate Typescript library would simply provide the validation component. Long-term it would obviously be wonderful to address https://github.com/bufbuild/protobuf-es/issues/397 and have a simple validate method available that could parse custom proto options, similar to what's present in the Python and Golang and Java libraries you folks have developed. But it would definitely be helpful to have a stop-gap solution here that at least provided partial support for users of your protovalidate ecosystem!

nicksnyder commented 11 months ago

Would you folks be open to adding library functions that don't depend on protobuf-es while that blocker exists?

No, we are focusing our attention on improving protobuf-es and are actively working on a solution to the blocking issue.

kwhitehouse commented 11 months ago

Would you folks be open to adding library functions that don't depend on protobuf-es while that blocker exists?

No, we are focusing our attention on improving protobuf-es and are actively working on a solution to the blocking issue.

Thanks Nick, that's great to hear! Do you folks have any ETA on when you might have the blocking issue resolved, and when it might be possible to start developing the protovalidate-typescript library? (I'd be happy to help out with the latter if you're looking for contributors!)

nicksnyder commented 11 months ago

Rough guess is that we will be unblocked sometime in Q1. Appreciate the offer to help and will post to the issue if we could use help when the time comes.

kwhitehouse commented 11 months ago

Awesome, thanks, I'll check back in on this ticket in Q1!

svdney4 commented 9 months ago

Since protovalidate relies heavily on CEL does this mean that a CEL typescript evaluator is being developed as well? And will there be a standalone version of the evaluator? We have the need for a javascript/typescript CEL evaluator in our project but we can't seem to find a robust implementation at the moment.

nicksnyder commented 9 months ago

Yes, supporting Protovalidate in TypeScript entails having a high quality open source CEL interpreter that works in all the environments we want (aka doesn't require Node.js APIs and doesn't require WASM). We are also unaware of any existing open source project that meets these requirements and so have already invested some time building our own implementation. We would open source this CEL implementation at the same time that we would open source support for Protovalidate TypeScript (which currently doesn't have a concrete timeline associated with it).

shoshannamb commented 6 months ago

Is there a clearer timeline on this? We're adding some protobuf-based metadata and being able to add validation would be really helpful. But we do need Typescript support.

nicksnyder commented 6 months ago

We do not have a clearer timeline for this at this point. As we have been working on https://github.com/bufbuild/protobuf-es/issues/397 and adding support for Protobuf Editions, it became apparent that we need to release a v2 of protobuf-es to support both of these things, so the rough timeline I gave earlier is no longer accurate. The good news is there is an alpha pre-release of protobuf-es v2 already, but there is more work to do. Once protobuf-es v2 is done and https://github.com/bufbuild/protobuf-es/issues/397 is closed, work on Protovalidate TypeScript will not be blocked.

shoshannamb commented 5 months ago

Will we need to be on protobuf-es in order to use protovalidate? We currently use protobuf-ts.

nicksnyder commented 5 months ago

Yes, you will need to be using protobuf-es. The runtimes differ to much for us to support both.

loeffel-io commented 2 months ago

Hey, any update on this? thank you

soisyourface commented 1 month ago

@nicksnyder,

looks like a bunch of progress has been made on protobuf-es v2, including a release of not just v2.0.0 but also v2.1.0. congrats on those huge milestones!

i'm assuming this means that protovalidate-ts is also in flight as it seems the identified blockers have been removed.

is there anything needed by the buf from the community on testing this or any other help?

nicksnyder commented 1 month ago

In addition to releasing protobuf-es v2 we have also needed to follow through with connect-es v2 because that depends on protobuf-es. That body of work is wrapping up and we have started to internally discuss our plans to move Protovalidate TS forward, but don't have anything concrete to share publicly yet. I know there are a lot of people interested in this and we want it too!

singh-hovr commented 3 weeks ago

any updates?

jafaircl commented 3 weeks ago

If it's helpful at all (even if just for prior art), I'm working on a CEL typescript implementation based on the cel-go package. The parser and checker work. Just need to finish implementing the interpreter. I know a CEL interpreter is a prerequisite for protovalidate and one doesn't currently exist. https://github.com/jafaircl/bearclaw/tree/issue-35/packages/cel

StevenACoffman commented 1 week ago

@jafaircl Very exciting! I would love to see your CEL interpreter used for this, and I have a couple other personal use cases I'd like it for! I also found your https://github.com/jafaircl/cel

For others, it looks like you might be able to track this work https://github.com/jafaircl/bearclaw/issues/35

nicksnyder commented 2 days ago

We just shipped Connect-ES v2.0 and our intent is to start work on this next.

loeffel-io commented 2 days ago

@nicksnyder amazing! would be great btw if the we can keep the deps list tiny (against other ts/js libs out there).

do you think protovalidate-ts could be available in the next 3 months? (we want to use this in the browser btw for frontend validations)

loeffel-io commented 2 days ago

another one: i have no idea how the process works but this will be a protoc plugin which generates the ts/js files right? it would be really helpful if this plugin is written in go/rust or something that ca be statically compiled to make the integration into bazel easy

timostamm commented 2 days ago

The TypeScript implementation will work the same way that other implementations do: They do not have a plugin and do not generate additional code. Instead, they retrieve the rules for a message from the descriptor embedded into the generated code via reflection.