One line param checking question

in javascript, you want to: 1. inside of a function (let's call this functionA), run a function once (let's cal this function functionB) 2. check the return value of functionB 3. IF the return value of functionB is truthy or meets a certain criteria, you return from the parent scope that called it (functionA). one example could be checking required params for an API endpoint and returning early with an error message if any are missing. it's trivial to abstract out of the route handler a generic param checker. I just wish there was a way to make param checking just one line in the route handler. are there any clever tricks I'm not thinking of?
1 Reply
JulieCezar
JulieCezar5w ago
function funcA(){
if(funcB()) return
}
function funcA(){
if(funcB()) return
}
you mean this? but continuing on you idea, I would create a generic function that takes in the URL and a zod schema... then use JS method with zod to validate if all is good. Something like:
function ValidateParams(url: string, schema) {
const _url = new URL(url);
const paramsIterator = new URLSearchParams(_url.search.slice(1));
const params = {};

for (const [key, value] of paramsIterator) {
params[key] = value;
}

const parseResult = schema.safeParse(params);

// or return w/e you want
if (parseResult.success === true) {
return {
success: parseResult.success,
data: parseResult.data,
};
} else {
return {
success: false,
error: parseResult.error,
};
}
}

// or with TS
function ValidateParams<T extends z.ZodSchema>(
url: string,
schema: T
): z.SafeParseReturnType<z.infer<T>, z.infer<T>> {
const _url = new URL(url);
const paramsIterator = new URLSearchParams(_url.search.slice(1));
const params: Record<string, string> = {};

for (const [key, value] of paramsIterator) {
params[key] = value;
}

const parseResult = schema.safeParse(params);

// or return w/e you want
if (parseResult.success === true) {
return {
success: true,
data: parseResult.data,
};
} else {
return {
success: false,
error: parseResult.error,
};
}
}
function ValidateParams(url: string, schema) {
const _url = new URL(url);
const paramsIterator = new URLSearchParams(_url.search.slice(1));
const params = {};

for (const [key, value] of paramsIterator) {
params[key] = value;
}

const parseResult = schema.safeParse(params);

// or return w/e you want
if (parseResult.success === true) {
return {
success: parseResult.success,
data: parseResult.data,
};
} else {
return {
success: false,
error: parseResult.error,
};
}
}

// or with TS
function ValidateParams<T extends z.ZodSchema>(
url: string,
schema: T
): z.SafeParseReturnType<z.infer<T>, z.infer<T>> {
const _url = new URL(url);
const paramsIterator = new URLSearchParams(_url.search.slice(1));
const params: Record<string, string> = {};

for (const [key, value] of paramsIterator) {
params[key] = value;
}

const parseResult = schema.safeParse(params);

// or return w/e you want
if (parseResult.success === true) {
return {
success: true,
data: parseResult.data,
};
} else {
return {
success: false,
error: parseResult.error,
};
}
}
and then you have something like:
import { z } from "zod";

const mySchema = z.object({
id: z.number(),
name: z.string(),
});

if(ValidateParams("https://.....", mySchema).success === false) return
import { z } from "zod";

const mySchema = z.object({
id: z.number(),
name: z.string(),
});

if(ValidateParams("https://.....", mySchema).success === false) return
Although this might be a bit better since you will probably reuse the params:
import { z } from "zod";

const mySchema = z.object({
id: z.number(),
name: z.string(),
});

const result = ValidateParams("https://.....", mySchema)

if(result.success === false) return

console.log(result.data)
import { z } from "zod";

const mySchema = z.object({
id: z.number(),
name: z.string(),
});

const result = ValidateParams("https://.....", mySchema)

if(result.success === false) return

console.log(result.data)
Want results from more Discord servers?
Add your server