TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Designing the perfect TypeScript schema validation library

98 点作者 colinmcd大约 5 年前

19 条评论

cjdell大约 5 年前
I built something like this a few months ago but struggled to explain to almost everyone except die hard TypeScript fans what the big deal was.<p>I explained it was all about the type inference from the schema so I could make guarantees across runtime boundaries.<p>This is what I came up with, though this library looks like it may be more comprehensive than what I wrote. Struggled to come up with a good name to represent exactly what it did...<p><a href="https:&#x2F;&#x2F;www.npmjs.com&#x2F;package&#x2F;type-safe-validator" rel="nofollow">https:&#x2F;&#x2F;www.npmjs.com&#x2F;package&#x2F;type-safe-validator</a><p>The ability of the TypeScript compiler to allow this stuff continues to make it one of my favourite languages to work with, despite it still being JavaScript underneath.
评论 #22563507 未加载
评论 #22563169 未加载
Gehinnn大约 5 年前
I&#x27;ve been working on something similar, also struggeling with `io-ts`. However, I want the schema to be serializable (possibly compatible with json schema) so that a rich ecosystem of tools can be build upon it. There is still a long way to go though.<p>My library `@hediet&#x2F;cli` [1] uses this technology to offer a browser based UI for CLI applications [2]. In the long term, I want to replace my use of `io-ts` with this technology in my JSON RPC implementation [3], so that I can implement something similar to Swagger for JSON RPC.<p>I always felt the possibilities of JSON Schema are not really explored, which I think might be due to bad design of JSON Schema (it&#x27;s really hard to consider all these denormalizations, it&#x27;s like HTML 20 years ago).<p>I&#x27;ll have a more detailed look at your library once I find time!<p>[1] <a href="https:&#x2F;&#x2F;github.com&#x2F;hediet&#x2F;ts-cli&#x2F;blob&#x2F;master&#x2F;cli&#x2F;README.md" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;hediet&#x2F;ts-cli&#x2F;blob&#x2F;master&#x2F;cli&#x2F;README.md</a> [2] <a href="https:&#x2F;&#x2F;github.com&#x2F;hediet&#x2F;ts-cli&#x2F;blob&#x2F;master&#x2F;cli&#x2F;docs&#x2F;gui.png" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;hediet&#x2F;ts-cli&#x2F;blob&#x2F;master&#x2F;cli&#x2F;docs&#x2F;gui.pn...</a> [3] <a href="https:&#x2F;&#x2F;github.com&#x2F;hediet&#x2F;typed-json-rpc" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;hediet&#x2F;typed-json-rpc</a>
hn_throwaway_99大约 5 年前
While I think this is pretty cool, given his use case (wanting to use a strongly typed client and server), I can&#x27;t see why one wouldn&#x27;t just use GraphQL with TypeScript on both the client and server. I&#x27;ve done this and I love it:<p>1. Define the GraphQL interface with GraphQL&#x27;s typedef language. GraphQL then takes care of validating all the request and responses at runtime.<p>2. Use something like <a href="https:&#x2F;&#x2F;github.com&#x2F;dotansimha&#x2F;graphql-code-generator" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;dotansimha&#x2F;graphql-code-generator</a> to generate all your TypeScript types from your GraphQL schema. Place these types on your resolvers and then you get compile-time type checking.<p>3. Since GraphQL actually exposes your schema as part of the endpoint, clients can use the same tool to keep their TypeScript types in sync, and get the same typing benefits when writing the client code.
评论 #22567396 未加载
评论 #22677993 未加载
eyelidlessness大约 5 年前
This is great. I recently built a fairly large amount of functionality <i>around</i> io-ts, adding support for mixed required&#x2F;optional fields, eliminating the need to deal with `Either` monads, and a whole lot of other stuff that&#x27;s mostly just relevant to my company&#x27;s usage. I think if this had been available at the time I was looking for an underlying library, it would have been a no brainer to choose this one.<p>Worth noting, another option is Runtypes[1], which also looks great. I can&#x27;t remember off the top of my head why I ultimately picked io-ts over Runtypes, but it&#x27;s another one for folks to consider (and I&#x27;d be curious what the Zod author thinks of it).<p><a href="https:&#x2F;&#x2F;github.com&#x2F;pelotom&#x2F;runtypes" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;pelotom&#x2F;runtypes</a>
评论 #22562334 未加载
评论 #22562891 未加载
评论 #22563238 未加载
评论 #22562987 未加载
jrimbault大约 5 年前
I&#x27;d really like something like a compiler step&#x2F;plugin to generate the validators from the declared typescript types.
评论 #22562665 未加载
评论 #22562584 未加载
评论 #22562581 未加载
franky47大约 5 年前
Adding to the pile of similar approaches, there&#x27;s TypeBox [1] which uses JSON Schema as an intermediate artifact (validation can happen with ajv or other libs), and extracts static types for TypeScript.<p>Having the JSON Schema intermediate is useful when using Fastify [2], so that request validation can happen with less boilerplate.<p>[1] <a href="https:&#x2F;&#x2F;github.com&#x2F;sinclairzx81&#x2F;typebox" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;sinclairzx81&#x2F;typebox</a><p>[2] <a href="https:&#x2F;&#x2F;www.fastify.io&#x2F;docs&#x2F;latest&#x2F;Validation-and-Serialization&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.fastify.io&#x2F;docs&#x2F;latest&#x2F;Validation-and-Serializat...</a>
nateabele大约 5 年前
Also did something similar[1]. My motivations were a better API (mine is modeled off of Elm&#x27;s), and better error messages: I have a use case where user&#x27;s need to be shown raw, dev-tools-style interactive data dumps, with error information overlaid on top. The next version will also enable data generation from schemas.<p>Also, and perhaps most importantly, this library has no respect for undefined, because particularly in the context of data modeling, undefined is complete nonsense.<p>[1] <a href="https:&#x2F;&#x2F;github.com&#x2F;ai-labs-team&#x2F;ts-utils#decoder" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;ai-labs-team&#x2F;ts-utils#decoder</a>
评论 #22567639 未加载
janpot大约 5 年前
You might want to take a look at ts-json-validator [1] as well. It creates type-aware validators for json-schema.<p>[1] <a href="https:&#x2F;&#x2F;github.com&#x2F;ostrowr&#x2F;ts-json-validator" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;ostrowr&#x2F;ts-json-validator</a>
random_savv大约 5 年前
There is also class-validator, which I&#x27;ve used and I find elegant and working well:<p><a href="https:&#x2F;&#x2F;github.com&#x2F;typestack&#x2F;class-validator" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;typestack&#x2F;class-validator</a>
评论 #22562829 未加载
__michaelg大约 5 年前
No mention of runtypes which looks pretty similar? <a href="https:&#x2F;&#x2F;github.com&#x2F;pelotom&#x2F;runtypes" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;pelotom&#x2F;runtypes</a>
评论 #22562726 未加载
sakagami0大约 5 年前
Wow I made the same thing that I put v1 up a couple days ago.<p><a href="https:&#x2F;&#x2F;github.com&#x2F;tetranoir&#x2F;presi" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;tetranoir&#x2F;presi</a><p>A main difference is I tried to get rid of having a separate line to create the Type and I tried to get rid of having to learn a new libary&#x27;s api as much as possible.
评论 #22562842 未加载
seanwilson大约 5 年前
Are there any plans for building validation support like this directly into TypeScript? If not, why not?<p>TypeScript seems designed to be incrementally added to large existing projects. Why then isn&#x27;t there a standard way to validate objects coming from the untyped parts of your project?
评论 #22567863 未加载
mirekrusin大约 5 年前
Something similar, but for flow and focused more on functional combinators <a href="https:&#x2F;&#x2F;github.com&#x2F;appliedblockchain&#x2F;assert-combinators" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;appliedblockchain&#x2F;assert-combinators</a>
评论 #22563760 未加载
AriaMinaei大约 5 年前
I&#x27;ve used a few similar libraries [0][1][2] and wrote one for a personal project. If we categorize them as embedded DSLs for runtime type checking with <i>some</i> support for static interop, they all share three major flaws:<p>1. Sub-optimal developer experience: significantly noisier syntax compared to pure typescript, convoluted typescript errors, and slower type checking.<p>2. Unfixable edge cases in static type checking: Features like conditional types work less reliably on the types produced by the library.<p>3. Some typescript features can&#x27;t be supported in the library (again, conditional types).<p>I think a better way to approach the runtime+static type checking is to do it as a babel plugin, which would fix the DX and edge-case problems and also gracefully degrade in the case of typescript features that can&#x27;t work in runtime.<p>Since babel can now parse typescript, it is trivial to write a babel plugin that takes regular typescript files and converts their type annotations into runtime values [4]. Those values can then be fed into a simpler type checking library, giving us runtime type checking on top of the native static type checking experience.<p>[0] <a href="https:&#x2F;&#x2F;github.com&#x2F;gcanti&#x2F;io-ts" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;gcanti&#x2F;io-ts</a><p>[1] <a href="https:&#x2F;&#x2F;github.com&#x2F;pelotom&#x2F;runtypes" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;pelotom&#x2F;runtypes</a><p>[2] mobx-state-tree also has a schema validation library that works to some extent statically: <a href="https:&#x2F;&#x2F;github.com&#x2F;mobxjs&#x2F;mobx-state-tree&#x2F;" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;mobxjs&#x2F;mobx-state-tree&#x2F;</a><p>[3] <a href="https:&#x2F;&#x2F;github.com&#x2F;gcanti&#x2F;io-ts#branded-types--refinements" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;gcanti&#x2F;io-ts#branded-types--refinements</a><p>[4] <a href="https:&#x2F;&#x2F;gist.github.com&#x2F;AriaMinaei&#x2F;2f1229178abad4363f5180db238dd8b3" rel="nofollow">https:&#x2F;&#x2F;gist.github.com&#x2F;AriaMinaei&#x2F;2f1229178abad4363f5180db2...</a>
manuisin大约 5 年前
A while ago, I used this library called ts-interface-builder to generate runtime validation functions from TS types. It worked really well, I was able to use the types from my front-end without any modifications for back-end validation.<p>Also, used the inferred response types from endpoint functions and passed them back to the front-end to use with front-end fetch calls. This was probably the biggest improvement in terms of productivity and improving accuracy. Imagine every time you change an endpoint&#x27;s response, any fetch calls that are impacted would just show errors at compile time&#x2F;in your editor.
chris_st大约 5 年前
Possibly stupid question, but why does the dog array validate:<p><pre><code> const dogsList = z.array(dogSchema); dogSchema.parse([ { name: &#x27;Cujo&#x27;, neutered: null }, { name: &#x27;Fido&#x27;, age: 4, neutered: true }, ]); &#x2F;&#x2F; passes </code></pre> Since &#x27;Cujo&#x27; doesn&#x27;t have an age? Assuming it&#x27;s the same dogSchema as in the previous block, age is required, right?<p>Oh, and a minor typo: <i>This lets you confidently This way you can confidently...</i>.
评论 #22563020 未加载
评论 #22562683 未加载
Ezku大约 5 年前
You mention creating object types with optional keys is cumbersome in io-ts. How is that solved in zod, exactly? What allows you to map `foo: union([bar, undefined])` to `foo?: bar | undefined` (note the question mark on the left hand side)? There’s nothing in the declaration to give away why this wouldn’t yield `foo: bar | undefined` which is what I believe you’d get out of io-ts.<p>Looks useful - I would have an easier time introducing this than io-ts.
评论 #22562570 未加载
评论 #22562534 未加载
adriancooney大约 5 年前
Can&#x27;t seem to spot a link to Zod&#x27;s code Colin. Fancy making it a bit more obvious in the post?<p>Edit: Found it: <a href="https:&#x2F;&#x2F;github.com&#x2F;vriad&#x2F;zod" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;vriad&#x2F;zod</a>
评论 #22562200 未加载
donatj大约 5 年前
Call me old fashioned, but &quot;mission-critical&quot; and &quot;rock-solid&quot; are inherently at odds with anything that runs a on a clients machine. Doubly so for something that runs in an interpreted language, especially one that doesn&#x27;t ship with it&#x27;s own interpreter to said client.<p>In these cases I&#x27;d far rather see an old fashioned reliable server-side application built in a language with a lot of built in safety. The less you ask the client to do, the more reliable the application.
评论 #22567269 未加载