PIat
PIat
Aarktype
Created by PIat on 10/9/2024 in #questions
Type definitions must be strings or objects (was undefined)
Hello! When importing a module that is using Arktype, I get the error Type definitions must be strings or objects (was undefined). I am not sure what it means. What should I be looking for to solve it?
18 replies
Aarktype
Created by PIat on 10/8/2024 in #questions
Conform to an existing type
Hello! Is there a way to force a def to conform to an existing typescript type?
export const chatDef = type({
id: 'number',
chatType: "private' | 'public'",
title: 'string',
username: 'string'
// force this
}) satisfies Pick<Jsonify<Chat>, 'id' | 'chatType' | 'title' | 'username'>
export const chatDef = type({
id: 'number',
chatType: "private' | 'public'",
title: 'string',
username: 'string'
// force this
}) satisfies Pick<Jsonify<Chat>, 'id' | 'chatType' | 'title' | 'username'>
29 replies
Aarktype
Created by PIat on 10/1/2024 in #questions
Node of kind optional is not valid as a required definition
Hello! Whenever I use a type with an optional key with a default value like this:
const def = type({
'agree?': 'boolean = true',
})
const def = type({
'agree?': 'boolean = true',
})
I get the error Node of kind optional is not valid as a required definition. What could it mean? Note that I'm also updating the type via map, but removing that mapping appeared to have no effect so far. Removing the default value throws no error
42 replies
Aarktype
Created by PIat on 9/17/2024 in #questions
ParseError: undefined must be a PropertyKey or stringifyNonKey must be passed to options
Hello! When key isn't provided in the .map function, the above error is thrown. Not working code:
const mapTypes = <T extends typeof typee>(t: T) =>
t.map((prop) => {
const key = prop.key
const value = prop.value

if (value.extends(type.number)) {
return [prop, { value: value.or('string.numeric.parse') }]
}

if (value.extends('boolean')) {
return [prop, { value: value.or(['string', '=>', (v) => v === 'on']) }]
}

return prop
})
const mapTypes = <T extends typeof typee>(t: T) =>
t.map((prop) => {
const key = prop.key
const value = prop.value

if (value.extends(type.number)) {
return [prop, { value: value.or('string.numeric.parse') }]
}

if (value.extends('boolean')) {
return [prop, { value: value.or(['string', '=>', (v) => v === 'on']) }]
}

return prop
})
Working code (simply added key to return object):
const mapTypes = <T extends typeof typee>(t: T) =>
t.map((prop) => {
const key = prop.key
const value = prop.value

if (value.extends(type.number)) {
return [prop, { key, value: value.or('string.numeric.parse') }]
}

if (value.extends('boolean')) {
return [
prop,
{ key, value: value.or(['string', '=>', (v) => v === 'on']) },
]
}

return prop
})
const mapTypes = <T extends typeof typee>(t: T) =>
t.map((prop) => {
const key = prop.key
const value = prop.value

if (value.extends(type.number)) {
return [prop, { key, value: value.or('string.numeric.parse') }]
}

if (value.extends('boolean')) {
return [
prop,
{ key, value: value.or(['string', '=>', (v) => v === 'on']) },
]
}

return prop
})
Is there a reason for this behavior? I would expect the key to already be present on the prop
27 replies
Aarktype
Created by PIat on 9/17/2024 in #questions
Dynamic type based on another value
Given a theoretical example like this:
const formDef = type({
agree: 'boolean',
clauses: ['===', 'first', 'second'],
})
const formDef = type({
agree: 'boolean',
clauses: ['===', 'first', 'second'],
})
Is it possible to only have clauses be required if agree is true?
24 replies
Aarktype
Created by PIat on 9/14/2024 in #questions
Extracting defaults at runtime
Hello! What is the canon way to extract the default values of the type on runtime? I'd like to uuse the defaults from the type as defaults in a form.
const formDef = type({
cars: '5 < number < 100 = 50',
name: ['string[]', '=', []],
active: 'boolean = true',
})

<Form
defaultValue={
// ...defaults from formDef
}
>
const formDef = type({
cars: '5 < number < 100 = 50',
name: ['string[]', '=', []],
active: 'boolean = true',
})

<Form
defaultValue={
// ...defaults from formDef
}
>
4 replies
Aarktype
Created by PIat on 9/14/2024 in #questions
Ark attest cannot find module error
Hello! In a pnpm monorepo, with @ark/attest installed in the workspace root and running pnpx run attest trace ., I get the following output:
...
Found & ignored ...
...
Starting: attest trace .
Debugger listening on ws://127.0.0.1:43611/c9521cad-1760-4b47-aa82-6525ed118c96
For help, see: https://nodejs.org/en/docs/inspector
Debugger attached.
Waiting for the debugger to disconnect...
node:internal/modules/cjs/loader:1148
throw err;
^

Error: Cannot find module '/home/p/Documents/Projects/web/attest'
at Module._resolveFilename (node:internal/modules/cjs/loader:1145:15)
at Module._load (node:internal/modules/cjs/loader:986:27)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:174:12)
at node:internal/main/run_main_module:28:49 {
code: 'MODULE_NOT_FOUND',
requireStack: []
}
...
Found & ignored ...
...
Starting: attest trace .
Debugger listening on ws://127.0.0.1:43611/c9521cad-1760-4b47-aa82-6525ed118c96
For help, see: https://nodejs.org/en/docs/inspector
Debugger attached.
Waiting for the debugger to disconnect...
node:internal/modules/cjs/loader:1148
throw err;
^

Error: Cannot find module '/home/p/Documents/Projects/web/attest'
at Module._resolveFilename (node:internal/modules/cjs/loader:1145:15)
at Module._load (node:internal/modules/cjs/loader:986:27)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:174:12)
at node:internal/main/run_main_module:28:49 {
code: 'MODULE_NOT_FOUND',
requireStack: []
}
What should I change to run it without issues?
156 replies
Aarktype
Created by PIat on 9/6/2024 in #questions
Extracting intent into runtime
Hello! What is the canon way to extract keys from types like these:
const type1 = type({
intent: '"hello"',
})

const type2 = type({
intent: '"hello"',
}).or({
intent: '"bye"',
})
const type1 = type({
intent: '"hello"',
})

const type2 = type({
intent: '"hello"',
}).or({
intent: '"bye"',
})
In a way that the resulting object is
{
intent: {
hello: 'hello'
}
}

{
intent: {
hello: 'hello',
bye: 'bye'
}
}
{
intent: {
hello: 'hello'
}
}

{
intent: {
hello: 'hello',
bye: 'bye'
}
}
? Would it be achieved with internal.distribute?
7 replies
Aarktype
Created by PIat on 9/4/2024 in #questions
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? expression might be "key"? pun intended https://github.com/edmundhung/conform/blob/main/packages/conform-zod/constraint.ts https://github.com/edmundhung/conform/blob/main/packages/conform-zod/parse.ts Also, is there an way to do something like superRefine for custom run-time conditions? https://zod.dev/?id=superrefine
174 replies
Aarktype
Created by PIat on 8/30/2024 in #questions
Default number
Hello! Setting this brings the expected result, when ran through inferAmbient:
const queryType = type({
page: 'number',
})

// Result:
number
const queryType = type({
page: 'number',
})

// Result:
number
But adding a default value returns a function signature:
const queryType = type({
page: 'number = 1',
})

// Result
page?: ((In?: number | undefined) => Default<1>) | undefined
const queryType = type({
page: 'number = 1',
})

// Result
page?: ((In?: number | undefined) => Default<1>) | undefined
How can I get the number type in the editor when providing a default value?
133 replies
Aarktype
Created by PIat on 8/23/2024 in #questions
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?
187 replies
Aarktype
Created by PIat on 8/22/2024 in #questions
Extract type from or
Hello! Is it possible, using the following schema:
const userType = type({
intent: "'profile'",
name: 'string>0',
surname: 'string>4',
})
.or({
intent: "'private'",
password: 'string>0',
})
const userType = type({
intent: "'profile'",
name: 'string>0',
surname: 'string>4',
})
.or({
intent: "'private'",
password: 'string>0',
})
to extract one of the types ("find" it by the intent field)? I'd like to use it for multiple forms on a page. It's possible to split it into multiple types, but having it in one type is more readable to me
25 replies
Aarktype
Created by PIat on 8/22/2024 in #questions
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?
15 replies
Aarktype
Created by PIat on 8/19/2024 in #questions
must be an instance of FormData (was object)
Hello! This code:
const formData = await request.clone().formData();

const formSchema = type({
name: 'string>0',
surname: 'string>0'
})

const parseForm = type('parse.formData').pipe(formSchema)

const formResult = parseForm(formData)
const formData = await request.clone().formData();

const formSchema = type({
name: 'string>0',
surname: 'string>0'
})

const parseForm = type('parse.formData').pipe(formSchema)

const formResult = parseForm(formData)
Throws following error:
must be an instance of FormData (was object)
must be an instance of FormData (was object)
However, formData is instanceof FormData and the log also says it's FormData:
ArkError {
input: {
code: 'proto',
description: 'an instance of FormData',
proto: [class FormData]
},
' arkKind': 'error',
path: [],
data: FormData { name: 'Plat', surname: '' },
...
ArkError {
input: {
code: 'proto',
description: 'an instance of FormData',
proto: [class FormData]
},
' arkKind': 'error',
path: [],
data: FormData { name: 'Plat', surname: '' },
...
What am I doing wrong?
38 replies
Aarktype
Created by PIat on 8/16/2024 in #questions
Error internationalization (i18n)
Hello! Is it possible to translate the errors into other languages? In my previous setup I was using this for such funtionality: https://github.com/aiji42/zod-i18n I saw this issue opened for individual fields, which would really be great: https://github.com/arktypeio/arktype/issues/722 Is this the way for "general" errors? https://github.com/arktypeio/arktype/issues/404
124 replies
Aarktype
Created by PIat on 8/13/2024 in #questions
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.
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.
Both packages are compiled to ESM, and the arktype version is identical, so the issue must be in them clashing inside of the vite project. What would be a good way to avoid this error?
203 replies
Aarktype
Created by PIat on 8/12/2024 in #questions
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:
output: type({
/** Image id */
id: type('string').describe('Image id'),
})
output: type({
/** Image id */
id: type('string').describe('Image id'),
})
33 replies
Aarktype
Created by PIat on 8/11/2024 in #questions
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
// Goal:
{
steps?: ...[]
}

// This still needs `steps: undefined`
steps:
type({
key: "'first'",
})
.or({
key: "'second'",
})
.array()
.or('undefined')
// Goal:
{
steps?: ...[]
}

// This still needs `steps: undefined`
steps:
type({
key: "'first'",
})
.or({
key: "'second'",
})
.array()
.or('undefined')
10 replies