is there a mustNotBe

currently I am using this as a workaround but I'd rather not implement the (was actual) part myself I would like maybe a mustNotBe or maybe a problem variant that doesn't overwrite the was stuff in the error string
const formSchema = type({
name: type('string.trim').narrow((name, ctx) => {
if (!name.endsWith('.project')) return true;
return ctx.reject({
problem: `cannot end with .project (was "${name}")`
});
}),
id: type('string | undefined').pipe((v) => v ?? nanoid()),
parentId: type('string')
});
const formSchema = type({
name: type('string.trim').narrow((name, ctx) => {
if (!name.endsWith('.project')) return true;
return ctx.reject({
problem: `cannot end with .project (was "${name}")`
});
}),
id: type('string | undefined').pipe((v) => v ?? nanoid()),
parentId: type('string')
});
Thanks for any help 🙂
9 Replies
ssalbdivad
ssalbdivad•2w ago
Hmm, I think the best way to make it composable would be to try and find a way to fit into must be ___. In a lot of cases, it wasn't the first thing I thought of but I found a reasonable way to make it work for various messages. In this case, maybe something like ctx.mustBe("a string not ending in .project") or similar
Raqueebuddin Aziz
Raqueebuddin AzizOP•2w ago
Thats what I thought as well but "a string" isnt a nice message for like showing form errors for example sicne most people wouldn't know what a string is
ssalbdivad
ssalbdivad•2w ago
"non-project suffixed"
Raqueebuddin Aziz
Raqueebuddin AzizOP•2w ago
And error summaries in ark already so good that they can be show verbatim on a form so I'd rather not implement custom formatters just for this case
ssalbdivad
ssalbdivad•2w ago
Yeah, the problem is the way it composes though that's why generally it's valuable to use must be ___ Because if some errors are must not be it adds a ton of complexity to the logic for union messages So you might be stuck between a rock and hard place in terms of a creative solution for the positive version or having to respecify actual
Raqueebuddin Aziz
Raqueebuddin AzizOP•2w ago
Got it, thanks for the insights
ssalbdivad
ssalbdivad•2w ago
Sorry I don't have a better answer! Hopefully you find something acceptable
Raqueebuddin Aziz
Raqueebuddin AzizOP•2w ago
Yeah I think interpolating actual isnt that bad, just kinda annoying
ssalbdivad
ssalbdivad•2w ago
not ending in .project or ending in something other than .project I don't know lol maybe there's something good out there

Did you find this page helpful?