mirror of
https://github.com/BradNut/boredgame
synced 2025-09-08 17:40:22 +00:00
Fixing zod validation on search form data.
This commit is contained in:
parent
45ddd0a286
commit
4bda219c22
2 changed files with 15 additions and 12 deletions
|
|
@ -26,6 +26,7 @@ export const search_schema = z.object({
|
||||||
exactMaxPlayers: z.boolean().optional(),
|
exactMaxPlayers: z.boolean().optional(),
|
||||||
})
|
})
|
||||||
.superRefine(({ minPlayers, maxPlayers, minAge, exactMinAge, exactMinPlayers, exactMaxPlayers }, ctx) => {
|
.superRefine(({ minPlayers, maxPlayers, minAge, exactMinAge, exactMinPlayers, exactMaxPlayers }, ctx) => {
|
||||||
|
console.log({ minPlayers, maxPlayers });
|
||||||
if (minPlayers && maxPlayers && minPlayers > maxPlayers) {
|
if (minPlayers && maxPlayers && minPlayers > maxPlayers) {
|
||||||
ctx.addIssue({
|
ctx.addIssue({
|
||||||
code: 'custom',
|
code: 'custom',
|
||||||
|
|
|
||||||
|
|
@ -17,13 +17,12 @@ export const actions: Actions = {
|
||||||
default: async ({ request }: RequestEvent): Promise<any> => {
|
default: async ({ request }: RequestEvent): Promise<any> => {
|
||||||
console.log("In search action specific")
|
console.log("In search action specific")
|
||||||
// Do things in here
|
// Do things in here
|
||||||
const formData = await request.formData();
|
const formData = Object.fromEntries(await request.formData());
|
||||||
const data = Object.fromEntries(formData);
|
console.log('passed in limit:', formData?.limit)
|
||||||
console.log('passed in limit:', data?.limit)
|
console.log('passed in skip:', formData?.skip)
|
||||||
console.log('passed in skip:', data?.skip)
|
const limit = formData?.limit || 10;
|
||||||
const limit = data?.limit || 10;
|
const skip = formData?.skip || 0;
|
||||||
const skip = data?.skip || 0;
|
|
||||||
console.log('action form', data);
|
|
||||||
const queryParams: SearchQuery = {
|
const queryParams: SearchQuery = {
|
||||||
order_by: 'rank',
|
order_by: 'rank',
|
||||||
ascending: false,
|
ascending: false,
|
||||||
|
|
@ -36,7 +35,7 @@ export const actions: Actions = {
|
||||||
|
|
||||||
// TODO: Check name length and not search if not advanced search
|
// TODO: Check name length and not search if not advanced search
|
||||||
|
|
||||||
const random = data?.random === 'on';
|
const random = formData?.random === 'on';
|
||||||
|
|
||||||
if (random) {
|
if (random) {
|
||||||
queryParams.random = random;
|
queryParams.random = random;
|
||||||
|
|
@ -58,6 +57,8 @@ export const actions: Actions = {
|
||||||
exactMinPlayers,
|
exactMinPlayers,
|
||||||
exactMaxPlayers
|
exactMaxPlayers
|
||||||
} = search_schema.parse(formData);
|
} = search_schema.parse(formData);
|
||||||
|
|
||||||
|
console.log('Parsed', search_schema.parse(formData))
|
||||||
|
|
||||||
if (minAge) {
|
if (minAge) {
|
||||||
if (exactMinAge) {
|
if (exactMinAge) {
|
||||||
|
|
@ -86,9 +87,10 @@ export const actions: Actions = {
|
||||||
queryParams.name = name;
|
queryParams.name = name;
|
||||||
}
|
}
|
||||||
} catch (error: unknown) {
|
} catch (error: unknown) {
|
||||||
|
|
||||||
if (error instanceof ZodError) {
|
if (error instanceof ZodError) {
|
||||||
const { fieldErrors: errors } = error.flatten();
|
const { fieldErrors: errors } = error.flatten();
|
||||||
return invalid(400, { formData, errors });
|
return invalid(400, { data: formData, errors });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -112,7 +114,7 @@ export const actions: Actions = {
|
||||||
'content-type': 'application/json'
|
'content-type': 'application/json'
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
console.log('board game response', response);
|
// console.log('board game response', response);
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
console.log('Status not 200', response.status);
|
console.log('Status not 200', response.status);
|
||||||
|
|
@ -121,7 +123,7 @@ export const actions: Actions = {
|
||||||
|
|
||||||
if (response.status === 200) {
|
if (response.status === 200) {
|
||||||
const gameResponse = await response.json();
|
const gameResponse = await response.json();
|
||||||
console.log('gameResponse', gameResponse);
|
// console.log('gameResponse', gameResponse);
|
||||||
const gameList = gameResponse?.games;
|
const gameList = gameResponse?.games;
|
||||||
const totalCount = gameResponse?.count;
|
const totalCount = gameResponse?.count;
|
||||||
console.log('totalCount', totalCount);
|
console.log('totalCount', totalCount);
|
||||||
|
|
@ -130,7 +132,7 @@ export const actions: Actions = {
|
||||||
games.push(mapAPIGameToBoredGame(game));
|
games.push(mapAPIGameToBoredGame(game));
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log('returning from search', games)
|
// console.log('returning from search', games)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
games,
|
games,
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue