mirror of
https://github.com/Sendouc/sendou.ink.git
synced 2026-09-07 19:55:46 -05:00
zod -> valibot (#3364)
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { z } from "zod";
|
||||
import * as v from "valibot";
|
||||
import type {
|
||||
BaseWeaponStats,
|
||||
MainWeaponParams,
|
||||
@@ -1043,17 +1043,17 @@ function resolveSpecialWeaponId(weapon: MainWeapon) {
|
||||
return specialWeaponObj.Id as SpecialWeaponId;
|
||||
}
|
||||
|
||||
const overwriteSchema = z.object({
|
||||
High: z.number().optional(),
|
||||
Mid: z.number().optional(),
|
||||
Low: z.number().optional(),
|
||||
const overwriteSchema = v.object({
|
||||
High: v.optional(v.number()),
|
||||
Mid: v.optional(v.number()),
|
||||
Low: v.optional(v.number()),
|
||||
});
|
||||
|
||||
function resolveOverwrites(params: any) {
|
||||
const result: MainWeaponParams["overwrites"] = {};
|
||||
|
||||
for (const [key, value] of Object.entries(params)) {
|
||||
const parsed = overwriteSchema.safeParse(value);
|
||||
const parsed = v.safeParse(overwriteSchema, value);
|
||||
|
||||
resolveOverwritesWithArbitraryKeys(result, value);
|
||||
|
||||
@@ -1066,14 +1066,14 @@ function resolveOverwrites(params: any) {
|
||||
const abilityKey = key.split("_").at(-1);
|
||||
invariant(abilityKey, `Could not find ability key for '${key}'`);
|
||||
|
||||
if (!parsed.data.High && !parsed.data.Mid && !parsed.data.Low) {
|
||||
if (!parsed.output.High && !parsed.output.Mid && !parsed.output.Low) {
|
||||
continue;
|
||||
}
|
||||
|
||||
result[abilityKey] = {
|
||||
High: parsed.data.High,
|
||||
Mid: parsed.data.Mid,
|
||||
Low: parsed.data.Low,
|
||||
High: parsed.output.High,
|
||||
Mid: parsed.output.Mid,
|
||||
Low: parsed.output.Low,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// for testing use the command `pnpm exec vite-node ./scripts/create-league-divisions.ts 6 'https://gist.githubusercontent.com/sendou-ink/38aa4d5d8426035ce178c09598ae627f/raw/17be9bb53a9f017c2097d0624f365d1c5a029f01/league.csv'`
|
||||
|
||||
import { z } from "zod";
|
||||
import * as v from "valibot";
|
||||
import { db } from "~/db/sql";
|
||||
import { ADMIN_ID } from "~/features/admin/admin-constants";
|
||||
import * as CalendarRepository from "~/features/calendar/CalendarRepository.server";
|
||||
@@ -20,7 +20,10 @@ invariant(
|
||||
|
||||
const csvUrl = process.argv[3]?.trim();
|
||||
|
||||
invariant(z.string().url().parse(csvUrl), "csv url is required (argument 2)");
|
||||
invariant(
|
||||
v.parse(v.pipe(v.string(), v.url()), csvUrl),
|
||||
"csv url is required (argument 2)",
|
||||
);
|
||||
|
||||
async function main() {
|
||||
const tournament = await tournamentFromDB({
|
||||
@@ -122,10 +125,10 @@ async function loadCsv() {
|
||||
return response.text();
|
||||
}
|
||||
|
||||
const csvSchema = z.array(
|
||||
z.object({
|
||||
"Team id": z.coerce.number(),
|
||||
Div: z.string(),
|
||||
const csvSchema = v.array(
|
||||
v.object({
|
||||
"Team id": v.pipe(v.unknown(), v.transform(Number), v.number()),
|
||||
Div: v.string(),
|
||||
}),
|
||||
);
|
||||
|
||||
@@ -145,7 +148,7 @@ function parseCsv(csv: string) {
|
||||
);
|
||||
});
|
||||
|
||||
const validated = csvSchema.parse(rows);
|
||||
const validated = v.parse(csvSchema, rows);
|
||||
|
||||
return validated.map((row) => ({
|
||||
id: row["Team id"],
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import * as v from "valibot";
|
||||
import * as BadgeRepository from "~/features/badges/BadgeRepository.server";
|
||||
import * as BuildRepository from "~/features/builds/BuildRepository.server";
|
||||
import * as XRankPlacementRepository from "~/features/top-search/XRankPlacementRepository.server";
|
||||
@@ -72,7 +73,7 @@ async function processJson(args: {
|
||||
logger.info(`reading in ${url}...`);
|
||||
|
||||
const json = await fetch(url).then((res) => res.json());
|
||||
const validated = xRankSchema.parse(json);
|
||||
const validated = v.parse(xRankSchema, json);
|
||||
|
||||
const array =
|
||||
validated.data.node.xRankingAr ??
|
||||
|
||||
@@ -1,76 +1,76 @@
|
||||
import { z } from "zod";
|
||||
import * as v from "valibot";
|
||||
|
||||
const placements = z.object({
|
||||
edges: z.array(
|
||||
z.object({
|
||||
node: z.object({
|
||||
id: z.string(),
|
||||
name: z.string(),
|
||||
rank: z.number(),
|
||||
rankDiff: z.union([z.string(), z.null()]),
|
||||
xPower: z.number(),
|
||||
weapon: z.object({
|
||||
name: z.string(),
|
||||
image: z.object({ url: z.string() }),
|
||||
id: z.string(),
|
||||
image3d: z.object({ url: z.string() }),
|
||||
image2d: z.object({ url: z.string() }),
|
||||
image3dThumbnail: z.object({ url: z.string() }),
|
||||
image2dThumbnail: z.object({ url: z.string() }),
|
||||
subWeapon: z.object({
|
||||
name: z.string(),
|
||||
image: z.object({ url: z.string() }),
|
||||
id: z.string(),
|
||||
const placements = v.object({
|
||||
edges: v.array(
|
||||
v.object({
|
||||
node: v.object({
|
||||
id: v.string(),
|
||||
name: v.string(),
|
||||
rank: v.number(),
|
||||
rankDiff: v.union([v.string(), v.null()]),
|
||||
xPower: v.number(),
|
||||
weapon: v.object({
|
||||
name: v.string(),
|
||||
image: v.object({ url: v.string() }),
|
||||
id: v.string(),
|
||||
image3d: v.object({ url: v.string() }),
|
||||
image2d: v.object({ url: v.string() }),
|
||||
image3dThumbnail: v.object({ url: v.string() }),
|
||||
image2dThumbnail: v.object({ url: v.string() }),
|
||||
subWeapon: v.object({
|
||||
name: v.string(),
|
||||
image: v.object({ url: v.string() }),
|
||||
id: v.string(),
|
||||
}),
|
||||
specialWeapon: z.object({
|
||||
name: z.string(),
|
||||
image: z.object({ url: z.string() }),
|
||||
id: z.string(),
|
||||
specialWeapon: v.object({
|
||||
name: v.string(),
|
||||
image: v.object({ url: v.string() }),
|
||||
id: v.string(),
|
||||
}),
|
||||
}),
|
||||
weaponTop: z.boolean(),
|
||||
__isPlayer: z.string(),
|
||||
byname: z.string(),
|
||||
nameId: z.string(),
|
||||
nameplate: z.object({
|
||||
badges: z.array(
|
||||
z.union([
|
||||
z.object({
|
||||
image: z.object({ url: z.string() }),
|
||||
id: z.string(),
|
||||
weaponTop: v.boolean(),
|
||||
__isPlayer: v.string(),
|
||||
byname: v.string(),
|
||||
nameId: v.string(),
|
||||
nameplate: v.object({
|
||||
badges: v.array(
|
||||
v.union([
|
||||
v.object({
|
||||
image: v.object({ url: v.string() }),
|
||||
id: v.string(),
|
||||
}),
|
||||
z.null(),
|
||||
v.null(),
|
||||
]),
|
||||
),
|
||||
background: z.object({
|
||||
textColor: z.object({
|
||||
a: z.number(),
|
||||
b: z.number(),
|
||||
g: z.number(),
|
||||
r: z.number(),
|
||||
background: v.object({
|
||||
textColor: v.object({
|
||||
a: v.number(),
|
||||
b: v.number(),
|
||||
g: v.number(),
|
||||
r: v.number(),
|
||||
}),
|
||||
image: z.object({ url: z.string() }),
|
||||
id: z.string(),
|
||||
image: v.object({ url: v.string() }),
|
||||
id: v.string(),
|
||||
}),
|
||||
}),
|
||||
__typename: z.string(),
|
||||
__typename: v.string(),
|
||||
}),
|
||||
cursor: z.string(),
|
||||
cursor: v.string(),
|
||||
}),
|
||||
),
|
||||
pageInfo: z.object({ endCursor: z.string(), hasNextPage: z.boolean() }),
|
||||
pageInfo: v.object({ endCursor: v.string(), hasNextPage: v.boolean() }),
|
||||
});
|
||||
|
||||
// e.g. https://splatoon3.ink/data/xrank/xrank.detail.a-2.clamblitz.json
|
||||
export const xRankSchema = z.object({
|
||||
data: z.object({
|
||||
node: z.object({
|
||||
__typename: z.string(),
|
||||
xRankingAr: placements.optional(),
|
||||
xRankingCl: placements.optional(),
|
||||
xRankingLf: placements.optional(),
|
||||
xRankingGl: placements.optional(),
|
||||
id: z.string(),
|
||||
export const xRankSchema = v.object({
|
||||
data: v.object({
|
||||
node: v.object({
|
||||
__typename: v.string(),
|
||||
xRankingAr: v.optional(placements),
|
||||
xRankingCl: v.optional(placements),
|
||||
xRankingLf: v.optional(placements),
|
||||
xRankingGl: v.optional(placements),
|
||||
id: v.string(),
|
||||
}),
|
||||
}),
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user