, , , , and more. Also, even though this is slightly different, validation libraries like , and others also stand really close.
Is ReScript Schema faster than all of them?
Yes. It's ~100 times faster than or ). But often, besides validation, you want to transform data incoming to your system, and here, ReScript Schema overperforms any solution existing in the JavaScript ecosystem.
What's ReScript? Isn't the library for JavaScript/TypeScript?
under the hood. You needn't worry about the code's dangerous execution, but some environments, like Cloudflare Workers, won't work. In 99% of cases, you don't need to be concerned about this. I just think it's my duty as a creator to let you know about the 1% beforehand.
What's the plan?
I'm going to provide an overview of the basic ReScript Schema API and mental model. Then, we'll discuss what makes it stand out from millions of similar libraries (and this is not only performance). I'll also look at some more advanced use cases and discuss the ecosystem, performance, and where it stands with other libraries.
I hope you'll enjoy it. 😊
Follow me on from Zod's docs. If you're curious about when and why you need to parse data in your application, let me know in the comments. I can write a big article about it, but for now, I assume you are already familiar with the concept.
Let's finally take a look at the code. I'll go with the TypeScript example first, so it's more familiar for most readers. Everything starts with defining a schema of the data you expect:
CODEimport * as S from "rescript-schema";
const filmSchema = S.schema({
id: S.number,
title: S.string,
tags: S.array(S.string),
rating: S.union(["G", "PG", "PG13", "R"])
})
The schema here is like a type definition that exists in runtime. If you hover over the
filmSchema, you'll see the following type:
CODES.Schema<{
id: number;
title: string;
tags: string[];
rating: "G" | "PG" | "PG13" | "R";
}>
This is a
Schematype that inferred the film object definition. I recommend extracting its value into its own type. This way, you'll have the schema as the source of truth and theFilmtype always matching the schema:
CODEtype Film = S.Output<typeof filmSchema>
After we've defined our
Filmtype using the schema, we can parse unknown data entering our application to guarantee that it matches what we expect:
CODES.parseOrThrow(
{
id: 1,
title: "My first film",
tags: ["Loved"],
rating: "S",
},
filmSchema,
);
//? Throws RescriptSchemaError with message `Failed parsing at ["rating"]. Reason: Expected "G" | "PG" | "PG13" | "R", received "S"`
S.parseOrThrow(validDataWithUnknownType, filmSchema)
//? Returns value of the Film type
// If you don't want to throw, you can wrap the operations in S.safe and get S.Result as a return value
S.safe(() => S.parseOrThrow(data, filmSchema))
Done! We have valid data here 🙌
Some experienced users may have noticed that the API is similar to one, the parser will perform in the most optimized way. I personally have seen this kind of DX only in
, it's getting impossible to extract the filmtype from the schema (even though it can infer it). But there's a built-in way to prevent boilerplate code in ReScript. You can use ReScript Schema PPX to generate schemas for your types automatically. Just annotate them with@schemaattribute.
CODE@schema
type rating =
| @as("G") GeneralAudiences
| @as("PG") ParentalGuidanceSuggested
| @as("PG13") ParentalStronglyCautioned
| @as("R") Restricted
@schema
type film = {
id: float,
title: string,
tags: array<string>,
rating: rating,
}
Does the
ratingtype look scary to you? Don't worry, this is a ReScript and does and maybe even faster 😎
100 Operations
If you want the best possible performance or the built-in operations don't cover your specific use case, you can use
S.compileto create fine-tuned operation functions.
CODEconst operation = S.compile(S.string, "Any", "Assert", "Async");
//? (input: unknown) => Promise<void>
await operation("Hello world!");
In the example above, we've created an async assert operation, which is not available by default.
With the API, you can get 100 different operation combinations, each of which might make sense for your specific use case. This is like to confirm yourself. If you see and the people behind it for allowing me to write very performant and safe code.
Talking about , which combines the DX of . I also built many powerful tools around ReScript Schema, but I have to admit that I haven't added TS support yet. Let me know if you find something interesting to use, and I'll do this asap 😁
Also, ReScript Schema supports , , and X follow 🙏
Let's see how the future of schema libraries will turn out. Maybe I'll rename ReScript Schema to something dope and become more popular than Zod? Cheers 😁
↗ Original-Artikel auf dev.to lesenVollständiger Original-BerichtAusführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.Wie bewertest du diesen Beitrag?1 Klick FeedbackTeilen mit Netzwerk & Team:Hat Ihnen dieser Tipp / Anleitung geholfen?Community-Analysen & Experten-Meinungen 0
Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf „ Eigene Analyse verfassen“!Community Pulse: Relevanz-Einschätzung1 Klick Experten-Votum🔴 Akute Relevanz 0%🟡 In Evaluierung 0%🟢 Keine Auswirkung 0%Spannende Innovation 0%Port 8095 EngineVerwandte Story-Cluster & Quellen (Vektor-KI)
Tipp: Mit Pfeiltasten [ ← ] und [ → ] blättern![]()
Ähnliche Beiträge
🔍 Verwandte NewsAuch interessante Nachrichten JavaScript schema library from the Future 🧬
Thematisch verwandte Begriffe: JavaScript, schema, library, from · 6 Treffer
🔧 Programmierung DEV CommunityProtect Kubernetes Services with OAuth2 Proxy, Gateway API, Traefik, and Pocket ID
🔧 AI Nachrichten DEV CommunityThe best n8n fix I found this month was boring: lower your agent concurrency settings before touching the prompt
🔧 AI Nachrichten NYT > Artificial IntelligencPresident Trump Called Nvidia’s Jensen Huang About A.I. Slowdown
🔧 AI Nachrichten NYT > Artificial IntelligencN.Y.C. Council Members Call for More Restrictions on Tech in Schools
Laden...Videos werden geladen ...
Laden...Beiträge werden geladen ...
Laden...Videos werden geladen ...
Laden...Beiträge werden geladen ...
Laden...Videos werden geladen ...
Laden...Beiträge werden geladen ...
Laden...Videos werden geladen ...
Laden...Beiträge werden geladen ...
Laden...Videos werden geladen ...
🔖 Gespeicherte Artikel
📂 Keine gespeicherten Artikel vorhanden.News ⏱️ 3 Min vor 10 MinArtikeldaten werden geladen...Zum Aktualisieren ziehen![]()
myDraft App
Offline-Lesen, Eilmeldungen & 0ms LadezeitInstalliere die App direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.
Nächster BeitragCommunity Radar & Live Chat
Sentinel Bot online • Live-StreamDein Cluster: Security ExplorerMatch:lädt…Aktivitäten deiner Analysten
lädt…Neues Thema oder Eilmeldung einreichen
Reiche interessante Links, Zero-Days oder Debatten ein. Die Community entscheidet per Upvote über die Veröffentlichung.
Heiß diskutierte Einreichungen
SOCIAL SHARE CARD GENERATOR