dibbo
dibbo
Aarktype
Created by dibbo on 11/3/2024 in #questions
Generic middleware validator
I think that should show the issue, thanks for looking into this
6 replies
Aarktype
Created by dibbo on 11/3/2024 in #questions
Generic middleware validator
6 replies
Aarktype
Created by dibbo on 11/3/2024 in #questions
Generic middleware validator
Sure. I've moved the call outside of the middleware chain i.e.
const test = validateBody(schema);
const test = validateBody(schema);
and I'm still seeing the error so I think we can rule out use/handler interfering with it. Here are the other types
// From @middy/core
declare type MiddlewareFn<
TEvent = any,
TResult = any,
TErr = Error,
TContext extends LambdaContext = LambdaContext,
TInternal extends Record<string, unknown> = {}
> = (request: Request<TEvent, TResult, TErr, TContext, TInternal>) => any

export interface MiddlewareObj<
TEvent = unknown,
TResult = any,
TErr = Error,
TContext extends LambdaContext = LambdaContext,
TInternal extends Record<string, unknown> = {}
> {
before?: MiddlewareFn<TEvent, TResult, TErr, TContext, TInternal>
after?: MiddlewareFn<TEvent, TResult, TErr, TContext, TInternal>
onError?: MiddlewareFn<TEvent, TResult, TErr, TContext, TInternal>
name?: string
}

// My own ValidatedBody type
type ValidatedBody<T> = Omit<APIGatewayProxyEventV2, "body"> & {
body: T;
};
// From @middy/core
declare type MiddlewareFn<
TEvent = any,
TResult = any,
TErr = Error,
TContext extends LambdaContext = LambdaContext,
TInternal extends Record<string, unknown> = {}
> = (request: Request<TEvent, TResult, TErr, TContext, TInternal>) => any

export interface MiddlewareObj<
TEvent = unknown,
TResult = any,
TErr = Error,
TContext extends LambdaContext = LambdaContext,
TInternal extends Record<string, unknown> = {}
> {
before?: MiddlewareFn<TEvent, TResult, TErr, TContext, TInternal>
after?: MiddlewareFn<TEvent, TResult, TErr, TContext, TInternal>
onError?: MiddlewareFn<TEvent, TResult, TErr, TContext, TInternal>
name?: string
}

// My own ValidatedBody type
type ValidatedBody<T> = Omit<APIGatewayProxyEventV2, "body"> & {
body: T;
};
Here's a CodeSandbox link to the ark schemas and their usage
6 replies
Aarktype
Created by PIat on 9/17/2024 in #questions
Dynamic type based on another value
As always, thank you for the help 🙏
24 replies
Aarktype
Created by PIat on 9/17/2024 in #questions
Dynamic type based on another value
Although thinking about it, I would also like to use it as a Type in the application, so I'm guessing I'll have to do the oring
24 replies
Aarktype
Created by PIat on 9/17/2024 in #questions
Dynamic type based on another value
Runtime (I think - its used to validate incoming request bodies)
24 replies
Aarktype
Created by PIat on 9/17/2024 in #questions
Dynamic type based on another value
const example = type({
status: "'Available'|'Unavailable'|'Reclaimed'|'Needs Reclaimed'",
species: "'Dog'|'Cat'|'Other'",
"dateOfBirth?": "Date", // Should be required when status is Available|Unavailable
"breed?": "string", // Should be required when species is Dog
"crossBreed?": "string", // Optional but only to be supplied when species is Dog
"summary?": "string", // Required when status is Available|Needs Reclaimed
});
const example = type({
status: "'Available'|'Unavailable'|'Reclaimed'|'Needs Reclaimed'",
species: "'Dog'|'Cat'|'Other'",
"dateOfBirth?": "Date", // Should be required when status is Available|Unavailable
"breed?": "string", // Should be required when species is Dog
"crossBreed?": "string", // Optional but only to be supplied when species is Dog
"summary?": "string", // Required when status is Available|Needs Reclaimed
});
24 replies
Aarktype
Created by PIat on 9/17/2024 in #questions
Dynamic type based on another value
Sure, 2 secs
24 replies
Aarktype
Created by PIat on 9/17/2024 in #questions
Dynamic type based on another value
Is there an alternative approach to this available via arktype at all? I have run into a similar scenario but with many possible key/value combinations. Rather than writing a specific type for each scenario and oring them, can I configure a property to be required/optional based on the value(s) of other properties?
24 replies
Aarktype
Created by dibbo on 8/29/2024 in #questions
Optional property with const string array
Oh nice, I didn’t know that was an option. Thank you for the replies
6 replies
Aarktype
Created by dibbo on 8/27/2024 in #questions
Indexed Access Types
Nice, thank you for the help!
9 replies
Aarktype
Created by dibbo on 8/27/2024 in #questions
Indexed Access Types
Thank you! Good question... I've been looking at this for so long I can't remember 😂 Let me give a more detailed example: I'm trying to create a type to be used to validate the payload in a search request. I've landed on this as the implementation
const status = ["Available", "Unavailable", "Sold"] as const;
const size = [
{ label: "Big", value: "100kg" },
{ label: "Small", value: "10kg" },
] as const;

const searchPayload = type({
"page?": "number",
"sort?": "string",
filters: {
"status?": type(["===", ...status]).array(),
"size?": type(["===", ...size.map((s) => s.value)]).array(),
},
});

type SearchPayload = typeof searchPayload.infer;
// Evaluates to the type I want
// type SearchPayload = {
// filters: {
// status?: ("Available" | "Unavailable" | "Sold")[];
// size?: ("100kg" | "10kg")[];
// };
// page?: number;
// sort?: string;
// }
const status = ["Available", "Unavailable", "Sold"] as const;
const size = [
{ label: "Big", value: "100kg" },
{ label: "Small", value: "10kg" },
] as const;

const searchPayload = type({
"page?": "number",
"sort?": "string",
filters: {
"status?": type(["===", ...status]).array(),
"size?": type(["===", ...size.map((s) => s.value)]).array(),
},
});

type SearchPayload = typeof searchPayload.infer;
// Evaluates to the type I want
// type SearchPayload = {
// filters: {
// status?: ("Available" | "Unavailable" | "Sold")[];
// size?: ("100kg" | "10kg")[];
// };
// page?: number;
// sort?: string;
// }
I was wondering if there was a cleaner/preferred way to pick out the value prop from the size array with arktype, rather than creating a new array via the map.
9 replies
Aarktype
Created by dibbo on 8/16/2024 in #questions
Latest "keys:distilled" equivalent?
No problem at all, thank you for the (impressively fast) response! 👍
11 replies