Gathering detailed insights and metrics for @ark/schema
Gathering detailed insights and metrics for @ark/schema
Gathering detailed insights and metrics for @ark/schema
Gathering detailed insights and metrics for @ark/schema
TypeScript's 1:1 validator, optimized from editor to runtime
npm install @ark/schema
@arktype/util@0.25.0
Published on 26 Nov 2024
@ark/util@0.25.0
Published on 26 Nov 2024
@ark/type@2.0.0-rc.25
Published on 26 Nov 2024
arktype@2.0.0-rc.25
Published on 26 Nov 2024
@arktype/schema@0.25.0
Published on 26 Nov 2024
@ark/schema@0.25.0
Published on 26 Nov 2024
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
3,959 Stars
978 Commits
62 Forks
15 Watching
2 Branches
27 Contributors
Updated on 28 Nov 2024
TypeScript (93.38%)
MDX (4.19%)
JavaScript (1.85%)
CSS (0.39%)
Astro (0.19%)
Cumulative downloads
Total Downloads
Last day
10.1%
8,346
Compared to previous day
Last week
8.4%
44,882
Compared to previous week
Last month
12.4%
173,373
Compared to previous month
Last year
0%
560,941
Compared to previous year
1
ArkType is a runtime validation library that can infer TypeScript definitions 1:1 and reuse them as highly-optimized validators for your data.
With each character you type, you'll get immediate feedback from your editor in the form of either a fully-inferred Type
or a specific and helpful ParseError
.
This result exactly mirrors what you can expect to happen at runtime down to the punctuation of the error message- no plugins required.
Check out how it works or scroll slightly to read about installation.
npm install arktype
(or whatever package manager you prefer)
Our types are tested in strict-mode with TypeScript version 5.4+
, although you will likely have success with other versions after 5.0.
If your types work but you notice errors in node_modules, this could be due to tsconfig
incompatibilities- please enable compilerOptions/skipLibCheck
(docs).
Defining basic types in ArkType is just like TypeScript, so if you already know how to do that, congratulations! You already know most of ArkType's syntax 🎉
For an ever better in-editor developer experience, try the ArkDark VSCode extension for syntax highlighting.
1import { type } from "arktype" 2 3// Definitions are statically parsed and inferred as TS 4export const user = type({ 5 name: "string", 6 device: { 7 platform: "'android'|'ios'", 8 "version?": "number" 9 } 10}) 11 12// Validators return typed data or clear, customizable errors. 13export const out = user({ 14 name: "Alan Turing", 15 device: { 16 // errors.summary: "device/platform must be 'android' or 'ios' (was 'enigma')" 17 platform: "enigma" 18 } 19}) 20 21if (out instanceof type.errors) { 22 // a clear, user-ready error message, even for complex unions and intersections 23 console.log(out.summary) 24} else { 25 // your valid data! 26 console.log(out) 27}
Lots more docs are on the way, but I want to highlight some of the most useful syntax patterns/features that are carried over from alpha as well as those new to the 2.0 release.
1// Syntax carried over from 1.0 + TS 2export const currentTsSyntax = type({ 3 keyword: "null", 4 stringLiteral: "'TS'", 5 numberLiteral: "5", 6 bigintLiteral: "5n", 7 union: "string|number", 8 intersection: "boolean&true", 9 array: "Date[]", 10 grouping: "(0|1)[]", 11 objectLiteral: { 12 nested: "string", 13 "optional?": "number" 14 }, 15 tuple: ["number", "number"] 16}) 17 18// available syntax new to 2.0 19 20export const upcomingTsSyntax = type({ 21 keyof: "keyof object", 22 variadicTuples: ["true", "...", "false[]"] 23}) 24 25// runtime-specific syntax and builtin keywords with great error messages 26 27export const validationSyntax = type({ 28 keywords: "email|uuid|creditCard|integer", // and many more 29 builtinParsers: "parse.date", // parses a Date from a string 30 nativeRegexLiteral: /@arktype\.io/, 31 embeddedRegexLiteral: "email&/@arktype\\.io/", 32 divisibility: "number%10", // a multiple of 10 33 bound: "alpha>10", // an alpha-only string with more than 10 characters 34 range: "1<=email[]<100", // a list of 1 to 99 emails 35 narrows: ["number", ":", n => n % 2 === 1], // an odd integer 36 morphs: ["string", "=>", parseFloat] // validates a string input then parses it to a number 37}) 38 39// root-level expressions 40 41const intersected = type({ value: "string" }, "&", { format: "'bigint'" }) 42 43// chained expressions via .or, .and, .narrow, .pipe and much more 44// (these replace previous helper methods like union and intersection) 45 46const user = type({ 47 name: "string", 48 age: "number" 49}) 50 51const parseUser = type("string").pipe(s => JSON.parse(s), user) 52 53// type is fully introspectable and traversable, displayed as: 54type ParseUser = Type< 55 (In: string) => Out<{ 56 name: string 57 age: number 58 }> 59> 60 61const maybeMe = parseUser('{ "name": "David" }') 62 63if (maybeMe instanceof type.errors) { 64 // "age must be a number (was missing)" 65 console.log(maybeMe.summary) 66}
There's so much more I want to share but I want to get at least an initial version of the 2.0 branch merged tonight so look forward to that next week!
ArkType supports many of TypeScript's built-in types and operators, as well as some new ones dedicated exclusively to runtime validation. In fact, we got a little ahead of ourselves and built a ton of cool features, but we're still working on getting caught up syntax and API docs. Keep an eye out for more in the next couple weeks ⛵
In the meantime, check out the examples here and use the type hints you get to learn how you can customize your types and scopes. If you want to explore some of the more advanced features, take a look at our unit tests or ask us on Discord if your functionality is supported. If not, create a GitHub issue so we can prioritize it!
ArkType can easily be used with tRPC via the assert
prop:
1t.procedure.input( 2 type({ 3 name: "string", 4 "age?": "number" 5 }).assert 6)
ArkType's isomorphic parser has parallel static and dynamic implementations. This means as soon as you type a definition in your editor, you'll know the eventual result at runtime.
If you're curious, below is an example of what that looks like under the hood. If not, close that hood back up, npm install arktype
and enjoy top-notch developer experience 🧑💻
1export const parseOperator = (s: DynamicState): void => { 2 const lookahead = s.scanner.shift() 3 return ( 4 lookahead === "" ? s.finalize() 5 : lookahead === "[" ? 6 s.scanner.shift() === "]" ? 7 s.rootToArray() 8 : s.error(incompleteArrayTokenMessage) 9 : isKeyOf(lookahead, Scanner.branchTokens) ? s.pushRootToBranch(lookahead) 10 : lookahead === ")" ? s.finalizeGroup() 11 : isKeyOf(lookahead, Scanner.comparatorStartChars) ? 12 parseBound(s, lookahead) 13 : lookahead === "%" ? parseDivisor(s) 14 : lookahead === " " ? parseOperator(s) 15 : throwInternalError(writeUnexpectedCharacterMessage(lookahead)) 16 ) 17} 18 19export type parseOperator<s extends StaticState> = 20 s["unscanned"] extends Scanner.shift<infer lookahead, infer unscanned> ? 21 lookahead extends "[" ? 22 unscanned extends Scanner.shift<"]", infer nextUnscanned> ? 23 state.setRoot<s, [s["root"], "[]"], nextUnscanned> 24 : error<incompleteArrayTokenMessage> 25 : lookahead extends Scanner.BranchToken ? 26 state.reduceBranch<s, lookahead, unscanned> 27 : lookahead extends ")" ? state.finalizeGroup<s, unscanned> 28 : lookahead extends Scanner.ComparatorStartChar ? 29 parseBound<s, lookahead, unscanned> 30 : lookahead extends "%" ? parseDivisor<s, unscanned> 31 : lookahead extends " " ? parseOperator<state.scanTo<s, unscanned>> 32 : error<writeUnexpectedCharacterMessage<lookahead>> 33 : state.finalize<s>
We accept and encourage pull requests from outside ArkType.
Depending on your level of familiarity with type systems and TS generics, some parts of the codebase may be hard to jump into. That said, there's plenty of opportunities for more straightforward contributions.
If you're planning on submitting a non-trivial fix or a new feature, please create an issue first so everyone's on the same page. The last thing we want is for you to spend time on a submission we're unable to merge.
When you're ready, check out our guide to get started!
This project is licensed under the terms of the MIT license.
I'd love to hear about what you're working on and how ArkType can help. Please reach out to david@arktype.io.
We will not tolerate any form of disrespect toward members of our community. Please refer to our Code of Conduct and reach out to david@arktype.io immediately if you've seen or experienced an interaction that may violate these standards.
We've been working full-time on this project for over a year and it means a lot to have the community behind us.
If the project has been useful to you and you are in a financial position to do so, please chip in via GitHub Sponsors.
Otherwise, consider sending me an email (david@arktype.io) or message me on Discord to let me know you're a fan of ArkType. Either would make my day!
sam-goodwin | fubhy |
---|---|
tmm | mishushakov | mewhhaha | codeandcats | drwpwrs |
---|---|---|---|---|
Timeraa | Phalangers | WilliamConnatser | JameEnder | |
No vulnerabilities found.
No security vulnerabilities found.