How to transform array with shapeshift using default values?

I have an array which I'm trying to validate with shapeshift:
const bans = JSON.parse(data);
const defaultReason = `Imported by ${interaction.user.username} from ${link}`;

const banListWithReason = s.array(
s.object({
id: s.string,
reason: s.string.default(defaultReason),
}),
);
const processedListWithReason = banListWithReason.run(bans);

const banList = s.array(s.string);
const processedList = banList.run(bans);

const validatedList: BanEntityWithReason[] = [];

if (processedListWithReason.isOk()) {
validatedList.concat(processedListWithReason.value);
return this.initiateBans(interaction, validatedList, defaultReason);
}
if (processedList.isOk()) {
processedList.value.forEach((ban) => {
validatedList.push({ id: ban, reason: defaultReason });
});
return this.initiateBans(interaction, validatedList, defaultReason);
}
const bans = JSON.parse(data);
const defaultReason = `Imported by ${interaction.user.username} from ${link}`;

const banListWithReason = s.array(
s.object({
id: s.string,
reason: s.string.default(defaultReason),
}),
);
const processedListWithReason = banListWithReason.run(bans);

const banList = s.array(s.string);
const processedList = banList.run(bans);

const validatedList: BanEntityWithReason[] = [];

if (processedListWithReason.isOk()) {
validatedList.concat(processedListWithReason.value);
return this.initiateBans(interaction, validatedList, defaultReason);
}
if (processedList.isOk()) {
processedList.value.forEach((ban) => {
validatedList.push({ id: ban, reason: defaultReason });
});
return this.initiateBans(interaction, validatedList, defaultReason);
}
So is there a way I can use banListWithReason to transform the string array into object array?
Solution:
Solution: ```ts const banList = s .array(s.string)...
Jump to solution
3 Replies
MRDGH2821
MRDGH28212y ago
for more context: bans is an array of either string or this:
type banEntityWithReason = {
id: string,
reason: string
}
type banEntityWithReason = {
id: string,
reason: string
}
If it is a string[] then I have to transform it into banEntityWithReason[] with defaultReason supplied to it
Favna
Favna2y ago
I dont think shapeshift can do this for you but if it can then it'll be in the transform function. I havent used that one though so you'll have to FAAFO If not then you'll have to transform it yourself, shouldn't be too hard.
Solution
MRDGH2821
MRDGH28212y ago
Solution:
const banList = s
.array(s.string)
.transform((value) => value.map((v) => ({ id: v, reason: defaultReason })));
const banList = s
.array(s.string)
.transform((value) => value.map((v) => ({ id: v, reason: defaultReason })));
This does the transformation in the format I want.
Want results from more Discord servers?
Add your server