oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
73.14k stars 2.68k forks source link

Integration of a Validation Library into Bun #6797

Open SK-FComputer opened 10 months ago

SK-FComputer commented 10 months ago

What is the problem this feature would solve?

Integrate a robust validation library into Bun to streamline data validation for developers, enhancing the utility and efficiency of using Bun. Additionally, offer TypeScript support to enable type inference from validation schemas. This would be imported like bun:sql; It would be bun:validation or whatever is a fitting name.

What is the feature you are proposing to solve the problem?

While Bun provides an excellent set of tools for development, a built-in validation library can add significant value. Currently, developers have to resort to external validation libraries or custom solutions, which can be cumbersome and inconsistent. Having a standardized, built-in solution would improve the development experience and ensure data integrity. Moreover, TypeScript support can facilitate better type safety and developer tooling.

  1. Validation Types: The library should offer various types of validations such as:

    • String length, format (e.g., email, URL)
    • Number range
    • Date format and range
    • Custom regular expressions
    • Required/Optional fields
  2. Nested Validation: Support for validating nested objects and arrays.

  3. Error Messages: Default and customizable error messages for each validation rule.

  4. Asynchronous Validation: Support for async validators for operations like checking the availability of a username.

  5. Configurable Strictness: Options to decide whether the validation should stop at the first error or collect all errors.

  6. TypeScript Support: The validation library should be fully compatible with TypeScript. This includes:

    • Type definitions for all validation rules and functions.
    • The ability to infer TypeScript types from validation schemas, enabling a seamless transition between validation and typed code.

Additional Context

Here is an independent list of benchmarks, for current "TypeScript runtime validators": https://github.com/moltar/typescript-runtime-type-benchmarks

What alternatives have you considered?

Using External Libraries: This is what developers currently do. However, it leads to inconsistent approaches and extra dependencies.

arthurfiorette commented 10 months ago

Another thing to take into consideration is support/compatibility with json schema. There are millions of features that already use json schema and would benefit from this validation.

shawshankkumar commented 10 months ago

This would be such an amazing feature to have 👐 Maybe something along the lines of adding validation for env would also be amazing.

SK-FComputer commented 10 months ago

I already thought about it, but the reason why i choose not to mention it was the TypeScript inference support. I guess there could be a import fromJsonSchema<T>(), but i haven't given it too much thought to come up with a good solution and that would not give "duplicated code".

SK-FComputer commented 10 months ago

@colinhacks sorry if the tag is not welcome, feel free to ignore if so.

As the creator of Zod and contributor to Bun, i think your opinion here would be good. Do you think this is a viable option, and should this be done in Zig, or "just JS" ? As Zod has become one of the more popular validation libraries, the interface would be familiar to many.

MarvinAmador7 commented 10 months ago

I think ElysiumJS does a great job with their validations implementation.

SK-FComputer commented 10 months ago

As far as i can see ElysiaJS uses typebox behind the scenes: https://github.com/elysiajs/elysia/blob/main/src/utils.ts

varna commented 10 months ago

Is it time to rewrite TypeScript in Zig? 🔥🔥🔥 With real types 😮

SK-FComputer commented 10 months ago

Anyone with experience in validation is welcome to chip in here.

wayjake commented 7 months ago

Is it time to rewrite TypeScript in Zig? 🔥🔥🔥

isn't that Bun?

wayjake commented 7 months ago

someone mentioned above, but i am going to try TypeBox out. That's one of the longest README files I've ever seen. lol.

varna commented 7 months ago

Is it time to rewrite TypeScript in Zig? 🔥🔥🔥

isn't that Bun?

The problem with TypeScript, is that it uses TypeScript. You know, like how C compiler is built on top of C? TS uses itself too, that's why it's so damn slow. You know SWC? It's a JS compiler written in Rust. It's fast and amazing. Currently most developers use SWC (or smth similar) for blazing fast dev times, and running TS compiler somewhere else (IDE or pipelines) to save time. There are some progress https://stc.dudy.dev/ for TS. But it's not like it will solve TS.

TS doesn't have types after compilation. You can create type guards (validations) via libraries like TypeBox (my favourite). But there's no native support for them in TS, because after compilation is complete, the types are gone. Simply removed. Fin.

TS also doesn't have static types. If I remember correctly, I think react native team is working on static hermes to solve that.

It would be pretty cool if Bun solved these two issues. But it's a lot of work, and TS has a lot of shitty baggage collected over the years. Bun team would probably need to make a new dialect (maybe BunScript? I always wanted some .bs files 🤣) to keep the dev cycle going and not drowning in backwards support. In theory BunScript could be compiled to assembly code for servers and wasm. But wasm IO performance sucks, so we also would need a compilation target for normal JS.

P.S. TS doesn't have warnings, only errors, that's why I use Biome. It's really useful while prototyping, to use some dynamic types or even any, just to keep going, especially when you don't know where you are going. After finalising your schema you can start chopping down the warnings, it feels more productive for me.

SK-FComputer commented 7 months ago

@varna I think you're missing the point of my post here. This request is made for validation library (What you call type guards), such that you can import from Bun:validator for .env vars, http endpoints (body, header, etc.) and whatever else needs this kind of validation.

I don't think Bun is going to make TypeScript static typed.

gitblit commented 7 months ago

Off-topic for this issue, but since @varna mentioned stc... there is an update.

wayjake commented 7 months ago

I always wanted some .bs files 🤣

@Jarred-Sumner do we have plans for this? asking for a friend!

wayjake commented 7 months ago

It's really useful while prototyping

honestly, you make a lot of really good points. thanks for taking the time to share. i only came across Biome last week. I tried it in the browser playground and it told me a bunch of annoying errors about my React effect dependencies. I couldn't clear the errors no matter how I tried. Maybe I'll go down that road later.

Overall i think that's a react typing issue in that effects can be used and misused easily but many times I seriously just don't want to listen for changes on a particular var... sue me!