Z
Zod8mo ago
Aidan647

Aidan647 - how can I validate other1 and other2...

how can I validate other1 and other2? (function validation is optional)
type demoFunction = {
(data: string): number[]
other1: number
other2: string
}


const demoFunction = z.object({
other1: z.number(),
other2: z.string()
})
type demoFunction = {
(data: string): number[]
other1: number
other2: string
}


const demoFunction = z.object({
other1: z.number(),
other2: z.string()
})
9 Replies
Scott Trinh
Scott Trinh8mo ago
import { z } from "zod";

type demoFunction = {
(data: string): number[]
other1: number
other2: string
}

declare const func: demoFunction;

func("data");

const schema = z.object({
other1: z.number(),
other2: z.string()
}).and(z.function(z.tuple([z.string()])));

declare const func2: z.infer<typeof schema>;

func2("data");
func2.other1;
// ^?
import { z } from "zod";

type demoFunction = {
(data: string): number[]
other1: number
other2: string
}

declare const func: demoFunction;

func("data");

const schema = z.object({
other1: z.number(),
other2: z.string()
}).and(z.function(z.tuple([z.string()])));

declare const func2: z.infer<typeof schema>;

func2("data");
func2.other1;
// ^? (property) other1: number
import { z } from "zod";

type demoFunction = {
(data: string): number[]
other1: number
other2: string
}

declare const func: demoFunction;

func("data");

const schema = z.object({
other1: z.number(),
other2: z.string()
}).and(z.function(z.tuple([z.string()])));

declare const func2: z.infer<typeof schema>;

func2("data");
func2.other1;
// ^?
import { z } from "zod";

type demoFunction = {
(data: string): number[]
other1: number
other2: string
}

declare const func: demoFunction;

func("data");

const schema = z.object({
other1: z.number(),
other2: z.string()
}).and(z.function(z.tuple([z.string()])));

declare const func2: z.infer<typeof schema>;

func2("data");
func2.other1;
// ^? (property) other1: number
Something like that? I've never used Zod to parse a function like that though, so I'd be a little wary of doing that. otherwise, I guess you can pass it like:
const functionOther = demoFunction.parse({
other1: func.other1,
other2: func.other2,
});
const functionOther = demoFunction.parse({
other1: func.other1,
other2: func.other2,
});
Aidan647
Aidan6478mo ago
"message": "Expected object, received function" tried z.function and z.object and z.object and z.function
Scott Trinh
Scott Trinh8mo ago
Yeah, I think you'll have to separate the function and object parsing.
Aidan647
Aidan6478mo ago
I don't need to parse function just object part in function
Scott Trinh
Scott Trinh8mo ago
Cool, then yeah, something like my other solution where you build an object out of the properties of the function should work.
import { z } from "zod";

type demoFunction = {
(data: string): number[];
other1: number;
other2: string;
};

declare const func: demoFunction;

func("data");

const otherSchema = z.object({
other1: z.number(),
other2: z.string(),
});

const other = otherSchema.parse({
other1: func.other1,
other2: func.other2,
});

other.other1;
// ^?
import { z } from "zod";

type demoFunction = {
(data: string): number[];
other1: number;
other2: string;
};

declare const func: demoFunction;

func("data");

const otherSchema = z.object({
other1: z.number(),
other2: z.string(),
});

const other = otherSchema.parse({
other1: func.other1,
other2: func.other2,
});

other.other1;
// ^?
Aidan647
Aidan6478mo ago
you gave me an idea and it works so basically I validate other1 and other2 separately
testOther1.parse(func.other1)
testOther2.parse(func.other2)
testOther1.parse(func.other1)
testOther2.parse(func.other2)
Scott Trinh
Scott Trinh8mo ago
Yeah, basically. But, note: parse does not narrow the type of the input data, it produces a value of the output type, if that makes sense. So you'll want to capture the result of parsing and use that rather than using the original func.other1 unless you trust that it exists anyway.
Aidan647
Aidan6478mo ago
I have it in pretty odd place where I don't save the result of parse, I just need it to crash if it does not have them
Want results from more Discord servers?
Add your server