refactor: replace zod with shapeshift (#7547)

This commit is contained in:
Parbez
2022-04-09 15:07:16 +05:30
committed by GitHub
parent 3f3e4327c8
commit 3c0bbac82f
14 changed files with 105 additions and 116 deletions

View File

@@ -1,43 +1,46 @@
import type { APIEmbedField } from 'discord-api-types/v10';
import { z } from 'zod';
import { s } from '@sapphire/shapeshift';
export const fieldNamePredicate = z.string().min(1).max(256);
export const fieldNamePredicate = s.string.lengthGe(1).lengthLe(256);
export const fieldValuePredicate = z.string().min(1).max(1024);
export const fieldValuePredicate = s.string.lengthGe(1).lengthLe(1024);
export const fieldInlinePredicate = z.boolean().optional();
export const fieldInlinePredicate = s.boolean.optional;
export const embedFieldPredicate = z.object({
export const embedFieldPredicate = s.object({
name: fieldNamePredicate,
value: fieldValuePredicate,
inline: fieldInlinePredicate,
});
export const embedFieldsArrayPredicate = embedFieldPredicate.array();
export const embedFieldsArrayPredicate = embedFieldPredicate.array;
export const fieldLengthPredicate = z.number().lte(25);
export const fieldLengthPredicate = s.number.le(25);
export function validateFieldLength(amountAdding: number, fields?: APIEmbedField[]): void {
fieldLengthPredicate.parse((fields?.length ?? 0) + amountAdding);
}
export const authorNamePredicate = fieldNamePredicate.nullable();
export const authorNamePredicate = fieldNamePredicate.nullable;
export const urlPredicate = z.string().url().nullish();
export const imageURLPredicate = s.string.url({
allowedProtocols: ['http:', 'https:', 'attachment:'],
}).nullish;
export const RGBPredicate = z.number().int().gte(0).lte(255);
export const colorPredicate = z
.number()
.int()
.gte(0)
.lte(0xffffff)
.nullable()
.or(z.tuple([RGBPredicate, RGBPredicate, RGBPredicate]));
export const urlPredicate = s.string.url({
allowedProtocols: ['http:', 'https:'],
}).nullish;
export const descriptionPredicate = z.string().min(1).max(4096).nullable();
export const RGBPredicate = s.number.int.ge(0).le(255);
export const colorPredicate = s.number.int
.ge(0)
.le(0xffffff)
.or(s.tuple([RGBPredicate, RGBPredicate, RGBPredicate])).nullable;
export const footerTextPredicate = z.string().min(1).max(2048).nullable();
export const descriptionPredicate = s.string.lengthGe(1).lengthLe(4096).nullable;
export const timestampPredicate = z.union([z.number(), z.date()]).nullable();
export const footerTextPredicate = s.string.lengthGe(1).lengthLe(2048).nullable;
export const titlePredicate = fieldNamePredicate.nullable();
export const timestampPredicate = s.union(s.number, s.date).nullable;
export const titlePredicate = fieldNamePredicate.nullable;

View File

@@ -5,6 +5,7 @@ import {
descriptionPredicate,
embedFieldsArrayPredicate,
footerTextPredicate,
imageURLPredicate,
timestampPredicate,
titlePredicate,
urlPredicate,
@@ -69,12 +70,12 @@ export class EmbedBuilder extends UnsafeEmbedBuilder {
public override setImage(url: string | null): this {
// Data assertions
return super.setImage(urlPredicate.parse(url)!);
return super.setImage(imageURLPredicate.parse(url)!);
}
public override setThumbnail(url: string | null): this {
// Data assertions
return super.setThumbnail(urlPredicate.parse(url)!);
return super.setThumbnail(imageURLPredicate.parse(url)!);
}
public override setTimestamp(timestamp: number | Date | null = Date.now()): this {