arktype

A

arktype

This is a friendly space centered around ArkType, TypeScript's 1:1 validator (https://github.com/arktypeio/arktype)

Join

Represent the File class

Hi! I want to make a schema for validating a form, and one of the elements of the form is going to be a file upload, so I would like to validate that the form is sending an instance of the File class, and then validate that this file size is smaller than a certain amount. Is there a way to represent class instances in Arktype? An example of doing this in Zod would be: ```ts const schema = z.object({ image: z...

Any way to tell if a custom description has been provided?

Is there any way to tell if a custom description has been created for a type? For example, let's say I have this, which I'm using to validate some user input: ```javascript...

Converting Zod to Arktype

Hello! I'm really interested in using Conform instead of React Hook Form for better DX and server-side capabilities. However, it doesn't have Ark support yet, so I'll have to create my own simple patch. Are there by any chance some examples or comparisons between Zod and Arktype architectures to go off of when translating these files, namely to extract the name of the constraints like Array in runtime? ...

Record with number as key?

const A = type("Record<number, string>");
const A = type("Record<number, string>");
This throws error: ```...

Is there a way to rename the key in parse result

const t = type({name:"string"}) const result = t({name:"ankit"}) i want the result to be {alias:"ankit"} how can i replace the key "name" with "alias"...

Can I add a default value to a type if everything other satisfies in a union type

const type1 = type({name: "string"}) const type2 = type({city: "string"}) const union = type1.or(type2) const result1 = union({name:"ankit"}) const result2 = union({city:"Ajmer"})...

How to type an expressjs-like route handler based on the path pattern.

If I define an application like: get('/path/:id?q=:query', handler) is it possible to look at the structure of the string and infer the type of the parameters that are passed to the handler function as { id: string; query: string;}. Given the prevalence of this pattern, I'd expect that someone has attempted it before, but I can't seem to find it in the docs and/or discord....

Compiling to JavaScript results in `any` type

I have a TypeScript library that compiles down into JavaScript, and one of the d.ts files looks like this:
declare const Address: import("arktype/out/subtypes/string").StringType<import("arktype/out/keywords/ast").string.narrowed, {}>;
type Address = typeof Address.in.infer;
declare const Address: import("arktype/out/subtypes/string").StringType<import("arktype/out/keywords/ast").string.narrowed, {}>;
type Address = typeof Address.in.infer;
...

Was `any` removed?

I used to be able to do: type("any");. Now it's throwing error Argument of type '"any"' is not assignable to parameter of type '"'any' is unresolvable "'

Type instantiation is excessively deep and possibly infinite

const m = <T, U>(type: Type<T, U>) => {
return type.describe("test");
}
const m = <T, U>(type: Type<T, U>) => {
return type.describe("test");
}
...

Best way to pass `Type` around

Hello! First, thanks for building this really cool library 🙏 I first heard of arktype while I was looking at the code of a framework called MUD, and while I was discussing CLI libraries with @frolic the other day, I thought that maybe it'd be a cool idea to have a CLI library that leverages arktype for arguments and options validation/parsing. I basically would like to have a function that receives args that are generated from arktype definitions. This is the code I have so far: ```ts import { type Type, type, validateAmbient } from "arktype"; import { type ErrorMessage } from "@ark/util";...

Default number

Hello! Setting this brings the expected result, when ran through inferAmbient: ```ts...

Optional property with const string array

Hello, the following
const x = ["a", "b", "c"] as const;
const myType = type({
"optional?": type("===", ...x),
});
const x = ["a", "b", "c"] as const;
const myType = type({
"optional?": type("===", ...x),
});
...

Extending types similar to an interface

How would one go about extending types similar to how one would extend an interface in TypeScript? It's really the only thing I feel like is missing for ArkType to be a viable alternative. A minimal example of what I want for ArkType, but written in TypeScript: ```typescript interface BaseTitle {...

Indexed Access Types

Hello, is it possible to recreate the SizeValues type below in arktype? ```ts const sizes = [ { label: "Big", value: "100kg" }, { label: "Medium", value: "50kg" }, { label: "Small", value: "10kg" },...

Accept number in string type

Hello! I'm parsing each value in a form with JSON.parse to convert string values like false to actual booleans. Unfortunately, this introduces the issue, that if someone inputs 123 into a text field, it gets parsed into a number type by JSON.parse. Then if the field is type string. An error is thrown, since number is not assignable to string. How should a type look like, if it should handle such a situation?...

Automatically applying parse.integer

Let's say I have a type I'm using to validate some API, e.g. ``` const personSchema = type({ name:'string', age:'0<=number<=120'...

Extract type from or

Hello! Is it possible, using the following schema: ```ts const userType = type({ intent: "'profile'", name: 'string>0',...

Get type without constraints

Hello! inferAmbient retuns types with constraints like moreThanLength<0>, which when I try to access values to, I get Type 'string' is not assignable to type 'moreThanLength<0>'. Is it possible to extract a more basic type?...

codecs

with morphs, I am thinking of arktype types as sort of input/output functions for const t = type(...) t.allows(input) tells if you if input is the right shape t(input) takes input and turns it into output (or errors) ...