Skip to content

Commit

Permalink
bug fixed json strings not being parsed before zod schema validation
Browse files Browse the repository at this point in the history
  • Loading branch information
EricFrancis12 committed Jul 16, 2024
1 parent b693356 commit 177b018
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 27 deletions.
2 changes: 0 additions & 2 deletions src/app/dashboard/ReportView/Report.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ export function useRows(clicks: TClick[], itemName: EItemName) {

useEffect(() => {
const newRows = makeRows(clicks, itemName, makeEnrichWith(itemName, primaryData));
console.log(structuredClone(newRows));
setRows(newRows);
}, [clicks.length, itemName]);

Expand Down Expand Up @@ -100,7 +99,6 @@ function makeRows(clicks: TClick[], itemName: EItemName, enrichWith?: TEnrichWit
});
}
}
console.log(rows);
return rows;
}, []);

Expand Down
4 changes: 2 additions & 2 deletions src/data/AffiliateNetwork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import cache from "../lib/cache";
import db from "../lib/db";
import { affiliateNetworkSchema } from "../lib/schemas";
import { TAffiliateNetwork, TAffiliateNetwork_createRequest, TAffiliateNetwork_updateRequest } from "../lib/types";
import { initMakeRedisKey } from "../lib/utils";
import { initMakeRedisKey, safeParseJson } from "../lib/utils";

const makeKey = initMakeRedisKey("affiliateNetwork");

Expand All @@ -17,7 +17,7 @@ export async function getAffiliateNetworkById(id: number): Promise<TAffiliateNet

// If found in the cache, parse and return it
if (cachedResult != null) {
const { data, success } = await affiliateNetworkSchema.safeParseAsync(cachedResult);
const { data, success } = await affiliateNetworkSchema.spa(safeParseJson(cachedResult));
if (success) return data;
}

Expand Down
9 changes: 5 additions & 4 deletions src/data/Campaign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import cache from "../lib/cache";
import { parseRoute, parseRoutes } from ".";
import db from "../lib/db";
import { campaignSchema } from "../lib/schemas";
import { initMakeRedisKey } from "../lib/utils";
import { initMakeRedisKey, newRoute, safeParseJson } from "../lib/utils";
import { TCampaign, TCampaign_createRequest, TCampaign_updateRequest } from "../lib/types";
import { Campaign } from "@prisma/client";

Expand All @@ -22,7 +22,7 @@ export async function getCampaignById(id: number): Promise<TCampaign | null> {

// If found in the cache, parse and return it
if (cachedResult != null) {
const { data, success } = await campaignSchema.safeParseAsync(cachedResult);
const { data, success } = await campaignSchema.spa(safeParseJson(cachedResult));
if (success) return data;
}

Expand Down Expand Up @@ -108,8 +108,9 @@ export async function deleteCampaignById(id: number): Promise<TCampaign> {
}

async function makeClientCampaign(dbModel: Campaign): Promise<TCampaign> {
const mainRouteProm = parseRoute(dbModel.flowMainRoute);
const ruleRoutesProm = parseRoutes(dbModel.flowRuleRoutes);
const { flowMainRoute, flowRuleRoutes } = dbModel;
const mainRouteProm = flowMainRoute ? parseRoute(flowMainRoute) : newRoute();
const ruleRoutesProm = flowRuleRoutes ? parseRoutes(flowRuleRoutes) : [];
return {
...dbModel,
flowMainRoute: await mainRouteProm,
Expand Down
9 changes: 5 additions & 4 deletions src/data/Flow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import cache from "../lib/cache";
import db from "../lib/db";
import { savedFlowSchema } from "../lib/schemas";
import { TSavedFlow, TSavedFlow_createRequest, TSavedFlow_updateRequest } from "../lib/types";
import { initMakeRedisKey } from "../lib/utils";
import { initMakeRedisKey, newRoute, safeParseJson } from "../lib/utils";

const makeKey = initMakeRedisKey("flow");

Expand All @@ -21,7 +21,7 @@ export async function getFlowById(id: number): Promise<TSavedFlow | null> {

// If found in the cache, parse and return it
if (cachedResult != null) {
const { data, success } = await savedFlowSchema.safeParseAsync(cachedResult);
const { data, success } = await savedFlowSchema.spa(safeParseJson(cachedResult));
if (success) return data;
}

Expand Down Expand Up @@ -106,8 +106,9 @@ export async function deleteFlowById(id: number): Promise<TSavedFlow> {
}

async function makeClientFlow(dbModel: SavedFlow): Promise<TSavedFlow> {
const mainRouteProm = parseRoute(dbModel.mainRoute);
const ruleRoutesProm = parseRoutes(dbModel.ruleRoutes);
const { mainRoute, ruleRoutes } = dbModel;
const mainRouteProm = mainRoute ? parseRoute(mainRoute) : newRoute();
const ruleRoutesProm = ruleRoutes ? parseRoutes(ruleRoutes) : [];
return {
...dbModel,
mainRoute: await mainRouteProm,
Expand Down
4 changes: 2 additions & 2 deletions src/data/LandingPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import cache from "../lib/cache";
import db from "../lib/db";
import { landingPageSchema } from "../lib/schemas";
import { TLandingPage, TLandingPage_createRequest, TLandingPage_updateRequest } from "../lib/types";
import { initMakeRedisKey } from "../lib/utils";
import { initMakeRedisKey, safeParseJson } from "../lib/utils";

const makeKey = initMakeRedisKey("landingPage");

Expand All @@ -18,7 +18,7 @@ export async function getLandingPageById(id: number): Promise<TLandingPage | nul

// If found in the cache, parse and return it
if (cachedResult != null) {
const { data, success } = await landingPageSchema.safeParseAsync(cachedResult);
const { data, success } = await landingPageSchema.spa(safeParseJson(cachedResult));
if (success) return data;
}

Expand Down
4 changes: 2 additions & 2 deletions src/data/Offer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import cache from "../lib/cache";
import db from "../lib/db";
import { offersSchema } from "../lib/schemas";
import { TOffer, TOffer_createRequest, TOffer_updateRequest } from "../lib/types";
import { initMakeRedisKey } from "../lib/utils";
import { initMakeRedisKey, safeParseJson } from "../lib/utils";

const makeKey = initMakeRedisKey("offer");

Expand All @@ -17,7 +17,7 @@ export async function getOfferById(id: number): Promise<TOffer | null> {

// If found in the cache, parse and return it
if (cachedResult != null) {
const { data, success } = await offersSchema.safeParseAsync(cachedResult);
const { data, success } = await offersSchema.spa(safeParseJson(cachedResult));
if (success) return data;
}

Expand Down
4 changes: 2 additions & 2 deletions src/data/TrafficSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import db from "../lib/db";
import { parseToken, parseNamedTokens, makeBoilerplateToken } from ".";
import { trafficSourceSchema } from "../lib/schemas";
import { TTrafficSource, TTrafficSource_createRequest, TTrafficSource_updateRequest } from "../lib/types";
import { initMakeRedisKey } from "../lib/utils";
import { initMakeRedisKey, safeParseJson } from "../lib/utils";

const makeKey = initMakeRedisKey("trafficSource");

Expand All @@ -21,7 +21,7 @@ export async function geTTrafficSourceById(id: number): Promise<TTrafficSource |

// If found in the cache, parse and return it
if (cachedResult != null) {
const { data, success } = await trafficSourceSchema.safeParseAsync(cachedResult);
const { data, success } = await trafficSourceSchema.spa(safeParseJson(cachedResult));
if (success) return data;
}

Expand Down
16 changes: 8 additions & 8 deletions src/data/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { z } from "zod";
import { tokenSchema, namedTokenSchema, routeSchema } from "../lib/schemas";
import { newRoute } from "@/lib/utils";
import { newRoute, safeParseJson } from "@/lib/utils";
import { TToken, TNamedToken, TRoute } from "../lib/types";

export * from "./User";
Expand All @@ -12,28 +12,28 @@ export * from "./LandingPage";
export * from "./Offer";
export * from "./TrafficSource";

export async function parseRoute(jsonStr: string | null): Promise<TRoute> {
const { success, data } = await routeSchema.safeParseAsync(jsonStr);
export async function parseRoute(jsonStr: string): Promise<TRoute> {
const { success, data } = await routeSchema.spa(safeParseJson(jsonStr));
return success ? data : newRoute();
}

export async function parseRoutes(jsonStr: string | null): Promise<TRoute[]> {
const { success, data } = await z.array(routeSchema).safeParseAsync(jsonStr);
export async function parseRoutes(jsonStr: string): Promise<TRoute[]> {
const { success, data } = await z.array(routeSchema).spa(safeParseJson(jsonStr));
return success ? data : [];
}

export async function parseToken(jsonStr: string): Promise<TToken> {
const { success, data } = await tokenSchema.safeParseAsync(jsonStr);
const { success, data } = await tokenSchema.spa(safeParseJson(jsonStr));
return success ? data : makeBoilerplateToken();
}

export async function parseTokens(jsonStr: string): Promise<TToken[]> {
const { success, data } = await z.array(tokenSchema).safeParseAsync(jsonStr);
const { success, data } = await z.array(tokenSchema).spa(safeParseJson(jsonStr));
return success ? data : [];
}

export async function parseNamedTokens(jsonStr: string): Promise<TNamedToken[]> {
const { success, data } = await z.array(namedTokenSchema).safeParseAsync(jsonStr);
const { success, data } = await z.array(namedTokenSchema).spa(safeParseJson(jsonStr));
return success ? data : [];
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ export const pathSchema: toZod<TPath> = z.object({
export const routeSchema = z.object({
isActive: z.boolean(),
logicalRelation: z.nativeEnum(ELogicalRelation),
rules: z.array(ruleSchema),
paths: z.array(pathSchema),
rules: z.array(ruleSchema),
});

export const savedFlowSchema = z.object({
Expand Down
10 changes: 10 additions & 0 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@ import { TRoute, ELogicalRelation } from "./types";

export const initMakeRedisKey = (prefix: string) => (id: number | string) => `${prefix}:${id}`;

export function safeParseJson(jsonStr: string, resultIfError: unknown = {}): unknown {
let result: unknown;
try {
result = JSON.parse(jsonStr);
} catch (err) {
return resultIfError;
}
return result;
}

export function formatErr(err: unknown): string {
if (typeof err === "string") return err;
if (err instanceof Error) return err.message;
Expand Down

0 comments on commit 177b018

Please sign in to comment.