arktype

A

arktype

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

Join

Tried to initialize an $ark registry in a monorepo

Hello! In a pnpm workspace, I have 2 packages depending on arktype. This means that each package depends on arktype separately. However, when I import them into a central vite project, I get the error: ``` Tried to initialize an $ark registry but one already existed. This probably means you are either depending on multiple versions of an arktype package, or importing the same package from both ESM and CJS. Review package.json versions across your repo to ensure consistency....

Adding comment to object key

Hello! What would be the right way to add comments to an object's key, like you do with /** */? I tried this, but it didn't work: ```ts output: type({...

Optional key

Hello! How can I define an array, which is also not required as an optional key? Is this the easiest way? https://discord.com/channels/957797212103016458/957804102685982740/1213198676349026355 ```ts // Goal: {...

Convert string to number with constraints

Hi, I'm looking at switching from Zod to Arktype mainly based on the serializable nature of types (lots of database driven forms). I'm stuck on something I think should be simple... - I'm passing FormData to the server. - Some of the fields are numeric, but are passed as strings (as FormData does)...

Are there examples of how to create mapped types?

e.g. a { [K in <SomeOtherType>]: SomeValueType } type, where SomeValueType is either a static type or is computed from K. I currently have a very basic setup where I have: ```ts type MappedEnum<T extends string> = { readonly [K in T]: T };...

ArkType mutates original input?

I was always under the assumption that ArkType leaves the original input intact. However: ```ts import { type } from "arktype"; const original = { value: "100" };...

How to accept a generic non-empty const string array for use in `type`?

Note: this may be a TS question, feel free to send me there instead. I have the following type: PgEnum<TValues extends [string, ...string[]]> { enumValues: TValues; } I'm attempting to create a function which accepts this object and returns an arktype instance of the union of the strings in the array, which is const with well-known values....

ParseError: Root of kind union should be one of alias,intersection,unit,domain,proto

```ts import { type } from "arktype"; const Amount = type("string|number") .pipe((s, ctx) => {...

Type Magic: validation on type property

How to validate defs in object type properties? ```ts export type validateSchema< def, schema,...

Usage for HTTP validation

I'm looking to use Arktype as a Request/Response validator This comes with the following requirements: - Request body should be validated with deep reject unknownKeys so invalid requests are immediately 422 - Response should be validated with deep delete unknownKeys so local information can't passthrough - Response input should be cloned rather then inline-morphed (including key deletion) so you can pass in input objects as-is...

Can you create types with dynamic literals?

Say I have this function: ```ts const createType = <T extends string>(code: T) => { const literal: string = code; return type({...

Cannot assign to Type

```ts import { scope, Type } from "arktype"; const ValidationErrorResponse = scope({ ErrorDescription: {...

How to validate `Record<string, string>`?

My naïve attempt: ```ts const Response = type({ "*": "string", });...

Property 'name' is missing in type

```ts const MyErrorResponse = scope({ Error: { message: "string", code: "string",...

Type 'distillOut<T>' is not assignable to type 'T'.

```ts import { type, Type } from "arktype"; const test = <T>(validator: Type<T>): T => { const result = validator({});...

Mapping types

Hello, is it possible do to code like this? ```ts const x = type({ a: 'string', b: 'string',...

How to improve error messages?

address must be a string and less than length 200 and more than length 50 and valid according to an anonymous predicate and valid according to an anonymous predicate and valid according to an anonymous predicate (was missing)
This is an example of a validation error message returned. How could I go about making that a little more human friendly?...

Pipe required before narrow

const myFunc = (v: string) => true;
const MyString = type("50<string<200").narrow((s) => myFunc(s));
const myFunc = (v: string) => true;
const MyString = type("50<string<200").narrow((s) => myFunc(s));
Results in error:...

Error transforming object

I'm having some trouble doing transforming some data

Problem with Out type

Type '(In: string) => Out<bigint>' is not assignable to type 'bigint'
Type '(In: string) => Out<bigint>' is not assignable to type 'bigint'