oscklm
oscklm14mo ago

entering the literals of a v.union() when using run function in dashboard

When running functions through the dashboard, i can't seem to figure out how to manually type the value of a v.union(v.literal("easy"), v.literal("medium"), v.literal("hard")) when i enter the value i just want as a string "hard" i get an error. The function i try to run via the dashboard (the type field is a v.union aswell as the difficulty)
{
activityValues: {
details: {
description: "Bevæg dig udenfor og find 20 fugle og tag et billede og upload dit fund",
difficulty: "hard",
rewardPoints: 200,
thumbnail: "",
title: "Find 20 fugle i naturen",
},
type: "challenge",
},
videoId: "rx7frx81vdxvnx7g1h2wz1t4w16h18y8",
}
{
activityValues: {
details: {
description: "Bevæg dig udenfor og find 20 fugle og tag et billede og upload dit fund",
difficulty: "hard",
rewardPoints: 200,
thumbnail: "",
title: "Find 20 fugle i naturen",
},
type: "challenge",
},
videoId: "rx7frx81vdxvnx7g1h2wz1t4w16h18y8",
}
failure
ArgumentValidationError: Value does not match validator.
Path: .activityValues
Value: {details: {description: "Bevæg dig udenfor og find 20 fugle og tag et billede og upload dit fund", difficulty: "hard", rewardPoints: 200.0, thumbnail: "", title: "Find 20 fugle i naturen"}, type: "challenge"}
Validator: v.union(v.object({details: v.object({description: v.optional(v.string()), difficulty: v.union(v.literal("easy"), v.literal("medium"), v.literal("hard")), rewardPoints: v.float64(), thumbnail: v.optional(v.id("_storage")), title: v.string()}),
... rest of the error, too long to paste
failure
ArgumentValidationError: Value does not match validator.
Path: .activityValues
Value: {details: {description: "Bevæg dig udenfor og find 20 fugle og tag et billede og upload dit fund", difficulty: "hard", rewardPoints: 200.0, thumbnail: "", title: "Find 20 fugle i naturen"}, type: "challenge"}
Validator: v.union(v.object({details: v.object({description: v.optional(v.string()), difficulty: v.union(v.literal("easy"), v.literal("medium"), v.literal("hard")), rewardPoints: v.float64(), thumbnail: v.optional(v.id("_storage")), title: v.string()}),
... rest of the error, too long to paste
8 Replies
lee
lee14mo ago
the mismatch isn't coming from the "difficulty" union, which you are using correctly. It's from the thumbnail: v.optional(v.id("_storage")) which you are passing in as thumbnail: "". The empty string is not a valid storage id. I think you probably want to remove the thumbnail key from the input entirely.
oscklm
oscklmOP14mo ago
Ohh damn i completely missed that, i couldn't interpreate that from the error Thanks Lee! may i ask how u could tell that? I'd like to be able to better read the errors in the future Hmm actually still getting the error on web too after making a form for it: failure ArgumentValidationError: Value does not match validator. Path: .activity Value: {details: {description: "llqwkldkqwdmkqwdkl", difficulty: "easy", rewardPoints: 200.0, thumbnailId: "", title: "qwdqwkdk"}, type: "challenge"} Validator: v.union(v.object({details: v.object({description: v.optional(v.string()), difficulty: v.union(v.literal("easy"), v.literal("medium"), v.literal("hard")), rewardPoints: v.float64(), thumbnailId: v.optional(v.id("_storage")), title: v.string()}), type: v.literal("challenge")}), v.object({details: v.object({description: v.optional(v.string()), difficulty: v.union(v.literal("easy"), v.literal("medium"), v.literal("hard")), rewardPoints: v.float64(), thumbnailId: v.optional(v.id("_storage")), title: v.string()}), steps: v.array(v.object({text: v.string(), thumbnail: v.optional(v.id("_storage")), timeCode: v.optional(v.string())})), type: v.literal("tutorial")}), v.object({details: v.object({description: v.optional(v.string()), difficulty: v.union(v.literal("easy"), v.literal("medium"), v.literal("hard")), rewardPoints: v.float64(), thumbnailId: v.optional(v.id("_storage")), title: v.string()}), questions: v.array(v.object({answers: v.array(v.object({answer: v.string(), isCorrect: v.boolean()})), question: v.string()})), type: v.literal("quiz")}), v.null())
lee
lee14mo ago
I looked through each field and compared the value to the validator. So i noticed that thumbnail didn't match
oscklm
oscklmOP14mo ago
This is the schema for context:
export const activityDifficultyValidator = v.union(
v.literal('easy'),
v.literal('medium'),
v.literal('hard'),
)

export type ActivityDifficulty = Infer<typeof activityDifficultyValidator>

export const activityDetailsValidator = v.object({
title: v.string(),
difficulty: activityDifficultyValidator,
rewardPoints: v.number(),
description: v.optional(v.string()),
thumbnailId: v.optional(v.id('_storage')),
})

export const activityValidator = v.union(
v.object({
type: v.literal('challenge'),
details: activityDetailsValidator,
}),
v.object({
type: v.literal('tutorial'),
details: activityDetailsValidator,
steps: v.array(
v.object({
text: v.string(),
timeCode: v.optional(v.string()),
thumbnail: v.optional(v.id('_storage')),
}),
),
}),
v.object({
type: v.literal('quiz'),
details: activityDetailsValidator,
questions: v.array(
v.object({
question: v.string(),
answers: v.array(
v.object({
answer: v.string(),
isCorrect: v.boolean(),
}),
),
}),
),
}),
v.null(),
)
export const activityDifficultyValidator = v.union(
v.literal('easy'),
v.literal('medium'),
v.literal('hard'),
)

export type ActivityDifficulty = Infer<typeof activityDifficultyValidator>

export const activityDetailsValidator = v.object({
title: v.string(),
difficulty: activityDifficultyValidator,
rewardPoints: v.number(),
description: v.optional(v.string()),
thumbnailId: v.optional(v.id('_storage')),
})

export const activityValidator = v.union(
v.object({
type: v.literal('challenge'),
details: activityDetailsValidator,
}),
v.object({
type: v.literal('tutorial'),
details: activityDetailsValidator,
steps: v.array(
v.object({
text: v.string(),
timeCode: v.optional(v.string()),
thumbnail: v.optional(v.id('_storage')),
}),
),
}),
v.object({
type: v.literal('quiz'),
details: activityDetailsValidator,
questions: v.array(
v.object({
question: v.string(),
answers: v.array(
v.object({
answer: v.string(),
isCorrect: v.boolean(),
}),
),
}),
),
}),
v.null(),
)
The thumbnailId is optional (just changed the name to thumbnailId, to not add confusion from original post) Ahh nvm
lee
lee14mo ago
The error message says that thumbnailId is still the empty string
oscklm
oscklmOP14mo ago
Yeah i can tell now. And found the issue. I ended up adding thumbnailId: "" in the defaultValues of my react-hook-form Thanks again, enjoyr ur sunday
lee
lee14mo ago
sg glad you figured it out
oscklm
oscklmOP14mo ago
Yeah, sometimes it's worth actually reading through and understanding the errors before jumping to conclusion somethings else is the root cause haha. Something i def try to get better at. I could have easily spent more time than needed on finding the solution had i not asked here haha

Did you find this page helpful?