shapeshift's default with respect to surroundings

s.object({
name: s.enum("A", "B"),
other: s.object({ name: s.string }).default(/** if name is "A", default is { name: "abc" }, if name is "B", default is { name: "cba" }
s.object({
name: s.enum("A", "B"),
other: s.object({ name: s.string }).default(/** if name is "A", default is { name: "abc" }, if name is "B", default is { name: "cba" }
Solution:
Once we implement when, You'll be able to do it like this ```ts s.object({ name: s.enum("A", "B"), other: s.object({ name: s.string }).when('name', {...
Jump to solution
15 Replies
Favna
Favna2y ago
s.object({
name: s.enum("A", "B"),
other: s.object({ name: s.string }).default(/** if name is "A", default is { name: "abc" }, if name is "B", default is { name: "cba" }
s.object({
name: s.enum("A", "B"),
other: s.object({ name: s.string }).default(/** if name is "A", default is { name: "abc" }, if name is "B", default is { name: "cba" }
With actual code block
DragonZV
DragonZV2y ago
oh wait yea sorry also can I make a recursive shape
Favna
Favna2y ago
@Vladdy might be able to answer quicker than me or @P<z, x>
Parbez
Parbez2y ago
I don't think this is possible now it will be possible after adding when
vladdy
vladdy2y ago
I don't fully understand the question
DragonZV
DragonZV2y ago
basically if the name is "A", then the default is { name: "abc" }, if its "B", then the default is { name: "bca" }
vladdy
vladdy2y ago
Oh, conditional defaults
Solution
Parbez
Parbez2y ago
Once we implement when, You'll be able to do it like this
s.object({
name: s.enum("A", "B"),
other: s.object({ name: s.string }).when('name', {
is: (v) => v==='A',
then: (s) => s.default({name: 'abc'}),
else: (s) => s.default({name: 'bca'})
})
s.object({
name: s.enum("A", "B"),
other: s.object({ name: s.string }).when('name', {
is: (v) => v==='A',
then: (s) => s.default({name: 'abc'}),
else: (s) => s.default({name: 'bca'})
})
DragonZV
DragonZV2y ago
and the recursive object thing, what I mean is
const v = s.object({
name: "abc",
option: v
});
const v = s.object({
name: "abc",
option: v
});
vladdy
vladdy2y ago
Iirc we do support recursivity
Parbez
Parbez2y ago
yeah
DragonZV
DragonZV2y ago
how do I do it tho, cuz js doesn't allow using the variable before declaration
vladdy
vladdy2y ago
It should work I don't remember exactly how to do it
Parbez
Parbez2y ago
true. In general, if anything isn't possible in js won't be possible in shapeshift too. But u can do it using lazy https://github.com/sapphiredev/shapeshift/blob/807666ef537c84d2e0f8bd9f4ce1a8060bfb3fb5/tests/validators/lazy.test.ts#L40
DragonZV
DragonZV2y ago
oki, thanks
Want results from more Discord servers?
Add your server