arktype

A

arktype

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

Join

default value for optional field

Hey! I have a question about handling optional fields with defaults. I want to define a type where a property is optional in the input, but its value should never be undefined. If the field is missing entirely, I want to transform the input so that the field is present in the result with a default value....

How to collect validation errors and custom errors in pipe

I have a pipe in which I want to add errors to the arkerrors array. But when there is an validation error, then only the validation error is outputted. ``` const pipedType = type({foo: 'string', bar: 'number'}).pipe((t, ctx) => { if (t.foo === 'foo') { ctx.error({...

Custom errors with morphs

Hello, I am trying to have custom error messages on morphs. Here is an example for a Port type. The error message is still the original one from Arktype. ...

Stringified array

Is there a type for stringified arrays i.e. elements joined with comma: "item1,item2,item3"?

Custom error message with conditions and i18n

Hi i am new here, and i try to create translated error messages. i have a schema like this. ```ts const Thing = type({ price: type('string.numeric>0').configure({...

Minimal array length - how to?

This page https://arktype.io/docs/objects#arrays says one can use [] > 1 to constrain minimal array length. However, this doesn't work: ```ts import {type} from 'arktype' ...

Help with Generic infered type

https://arktype.io/playground?code=import%2520%257B%2520type%2520%257D%2520from%2520%2522arktype%2522%253B%250A%250Aexport%2520type%2520RectItem%2520%253D%2520typeof%2520t.Rect.infer%253B%250A%250Aconst%2520%2524%2520%253D%2520type.scope%28%257B%250A%2520%2520%2520%2520Item%253A%2520%2522Rect%2522%252C%250A%2520%2520%2520%2520%2522Prop%253Ct%2520extends%2520string%2520%257C%2520number%2520%257C%2520boolean%253E%2522%253A%2520%255B%250A%2520%2520%2520%2520%2520%2520%2520%2520%2522t%2520%257C%2520null%2522%252C%250A%2520%2520%2520%2520%2520%2520%2520%2520%2522%253D%253E%2522%252C%250A%2520%2520%2520%2520%2520%2520%2520%2520%28prop%29%2520%253D%253E%2520prop%2520%253F%253F%2520undefined%252C%250A%2520%2520%2520%2520%255D%252C%250A%2520%2520%2520%2520SharedProps%253A%2520%257B%250A%2520%2520%2520%2520%2520%2520%2520%2520id%253A%2520%2522string%2522%252C%250A%2520%2520%2520%2520%2520%2520%2520%2520artboardId%253A%2520%2522string%2522%252C%250A%2520%2520%2520%2520%2520%2520%2520%2520parentId%253A%2520%2522string%2520%257C%2520null%2520%253D%2520null%2522%252C%250A%2520%2520%2520%2520%257D%252C%250A%2520%2520%2520%2520Rect%253A%2520%257B%250A%2520%2520%2520%2520%2520%2520%2520%2520%2522...%2522%253A%2520%2522SharedProps%2522%252C%250A%2520%2520%2520%2520%2520%2520%2520%2520name%253A%2520%2522%27rect%27%2522%252C%250A%2520%2520%2520%2520%2520%2520%2520%2520fill%253A%2520%2522Prop%253Cstring%253E%2522%252C%250A%2520%2520%2520%2520%257D%252C%250A%257D%29%253B%250A%250Aexport%2520const%2520t%2520%253D%2520%2524.export%28%29%253B%250A RectItem is infering prop as: ``` { fill: string | number | boolean | undefined; }...

Prevent JsonSchema for type enumerated to become anyOf

Hello, when I have type.enumerated('foo', 'bar').or(type.enumerated('foo', 'bar').array()).toJsonSchema(), the returned schema is: ``` { "$schema": "https://json-schema.org/draft/2020-12/schema", "anyOf": [...

"string.email>0" custom message not working

Hey for most types i use .configure({message: "Some error message"}) to customize error messages for fields. When i use it on email type it seems configure does not work the same: ```ts const stringEmail = type("string.email>0")...

Can I export and share all types on the global scope by default?

Heya, so I have a monorepo — it's a flat and ever-widening hierarchy; all directories have a direct connection to the root-level deno.jsonc, which is also where the arktype import happens. I would love it if I could just turn the loosey goosey dials up to max and use every type() declaration as an alias in any other part of the repo. Is there a built-in and/or elegant way to do this?...

[attest] Error scanning node_modules. How can I make attest trace work?

I'm trying to run attest trace to analyze my Next.js 15 monorepo package. However, I'm getting an error that attest cannot find a module. ```...

Auto throw if invalid

I have been looking through the docs and the API manually, but I haven't found something elegant that I am looking for. Is there a way to strictly throw an error if the validation doesn't pass? While I don't normally mind the explicitness of instanceof type.errors, I kinda prefer the method opt-in from Zod (parse vs safeParse) to make that choice. To be clear this would purely be a DX improvement to prevent typing out the error catching manually for type inference....

Modeling string template type

I have a type ``ts type T = { foo: '' | /${string}` }...

Discriminated unions with common field but different constraints

Hello, I am having trouble setting up a schema-system with my types. My code is type-first, so the schemas must be written in accordance to my types. My entities have a type property, which should be the discriminating property. Two types are possible: typeA and typeB. Both types of entities have a field named someField. By default, someField is nullable, meaning new types in the future should have it nullable. But typeA entities require someField not to be null. Creating the corresponding schemas lead to a type error when trying to match my types to the schema's types. ...

Throw an ArkError programmatically

Is there an easy way to throw an ArkError programmatically without defining a schema? I'd like to ensure a provided value is unique against my DB, so I'm looking for an alternative to this: ```ts...

Equivalent of Object.values for unions

hi there 👋 i'm trying to extract the values of an object in order to have it as a union of options and ended up with this right here is there a better maybe more elegant solutions to this? 😅...
No description

Is this a bug or user error? Re: Generic Argument Validation

``` import { scope } from "arktype" const versionScope = scope({ ProductHashKey: "string",...

Bracket Notation on Generics

Is there any way to do something like this? 🤔 ``` scope({ BaseType: { entityType: "string" },...
No description

Performance comparison to Zod for piped string

Hello! I'm looking to implement a validator in a pretty performance critical area - so have been comparing some of the data contracts used, and in each test thrown together, ArkType is easily faster.... apart from one where a string is piped through a couple of stages The Zod equivilant is const zodTest = z.string().trim().toLowerCase().email().startsWith("info"); And for ArkType I've got const arkTest = type("string.trim |> string.lower |> string.email & /^info/");...
No description

does arktype understand "minimum 0 but not -0"?

in zod, even if you put .nonnegative it will still accept negative zero. would the same happen with AT?
No description