Dimava
Dimava
Aarktype
Created by Dimava on 9/16/2024 in #questions
Generic with Narrow/Morph
I want to make something like
type UniqueArray<T, K extends keyof T>


export const UniqueArray = brandsScope.type.generic('T', 'K extends keyof T', ['T[]', ':', (v, ctx) => {
let vals = new Set(v.map(e => e[erm what]))
if (vals.length !== v.length) return ctx.mustBe(`array with uniue $K`)
return true
}])
type UniqueArray<T, K extends keyof T>


export const UniqueArray = brandsScope.type.generic('T', 'K extends keyof T', ['T[]', ':', (v, ctx) => {
let vals = new Set(v.map(e => e[erm what]))
if (vals.length !== v.length) return ctx.mustBe(`array with uniue $K`)
return true
}])
Please help me to make it work
4 replies
Aarktype
Created by Dimava on 9/12/2024 in #questions
`string.integer.parsed` with limits
I need a simple port number, 1 < integer < 9999 Hovewer, I want to parse it from env string so I need string.integer.parse How do I make the limited parsed integer?
37 replies
Aarktype
Created by Dimava on 6/18/2024 in #questions
Type Magic: validation on type property
How to validate defs in object type properties?
export type validateSchema<
def,
schema,
> = {
[K in keyof schema]:
K extends keyof def
? validateTypeRoot<def[K]>
: validateTypeRoot<unknown>
}

function test<const def>(
def: validateSchema<def, { a: 1 }>,
) { }

test({
a: 'str',
})
export type validateSchema<
def,
schema,
> = {
[K in keyof schema]:
K extends keyof def
? validateTypeRoot<def[K]>
: validateTypeRoot<unknown>
}

function test<const def>(
def: validateSchema<def, { a: 1 }>,
) { }

test({
a: 'str',
})
I did that three days ago but I forgot how
31 replies
Aarktype
Created by Dimava on 6/15/2024 in #questions
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
let User = type({ name: string, meta: { bio: 'string' } })
let CreateUser = type([ User, '&', { password: string, meta: { age: 'number' } } ])
let UserModel = type([ CreateUser, '&', { meta: { isAdult: 'boolean' } } ])

let oneUser: UserModel;
function addUser(user: CreateUser) {
// should throw on inputs with extra keys on user and on user.meta
let newUser = AT_asHttpIn(CreateUser).assert(user)
newUser.meta.isAdult = newUser.meta.age >= 18
oneUser = AT_strict(UserModel).assert(newUser)
}
function getUser() {
// should remove password and age
// should not remove them on actual data
return AT_asHttpOut(User).assert(oneUser)
}
let User = type({ name: string, meta: { bio: 'string' } })
let CreateUser = type([ User, '&', { password: string, meta: { age: 'number' } } ])
let UserModel = type([ CreateUser, '&', { meta: { isAdult: 'boolean' } } ])

let oneUser: UserModel;
function addUser(user: CreateUser) {
// should throw on inputs with extra keys on user and on user.meta
let newUser = AT_asHttpIn(CreateUser).assert(user)
newUser.meta.isAdult = newUser.meta.age >= 18
oneUser = AT_strict(UserModel).assert(newUser)
}
function getUser() {
// should remove password and age
// should not remove them on actual data
return AT_asHttpOut(User).assert(oneUser)
}
Please list recommended AT APIs for this Please link the related undone issues is any
64 replies
Aarktype
Created by Dimava on 8/18/2023 in #questions
Context-sensitive validation / validation with parameters
interface Thing {
bars: string[];
foo: { baz: { bar: string }[]; }[];
}
interface Thing {
bars: string[];
foo: { baz: { bar: string }[]; }[];
}
What is the best/easiest way to write a validator which ensures that all bars are present in bars?
41 replies
Aarktype
Created by Dimava on 5/15/2023 in #questions
Custom type-making functions
I want to make a chainable that can create types the same way type does How do I implement that?
thing // Thing<never, never>
.input('number%1>0') // Thing<number, never>
.output({ items: 'string[]' }) // Thing<number, {items: string[]}>
thing // Thing<never, never>
.input('number%1>0') // Thing<number, never>
.output({ items: 'string[]' }) // Thing<number, {items: string[]}>
2 replies
Aarktype
Created by Dimava on 5/4/2023 in #questions
How to access current scope types with `type()` / `morph()` / `narrow()` / `arrayOf()` inside scope?
How to access current scope types with type() / morph() / narrow() / arrayOf() inside scope?
const $ = scope({
blah: "1 < number < 10",
blahArray: () => $.type("blah[]")
})

const types = $.compile()
const $ = scope({
blah: "1 < number < 10",
blahArray: () => $.type("blah[]")
})

const types = $.compile()
2 replies