Skip to content

Commit

Permalink
feat(validateQuote): add a string length check
Browse files Browse the repository at this point in the history
  • Loading branch information
sdanialraza committed Jan 17, 2024
1 parent e180573 commit fcb8e0a
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/util/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ export function validateQuote(quote: unknown) {
const { author, categories, submitter, text } = quote as Record<string, unknown>

if (typeof author === "string") {
if (author.length < 1) {
errors.push({
expected: "string.length <= 1",
message: "Expected the author to be at least 1 character",
received: `${author.length}`,
})
}

if (author.length > 30) {
errors.push({
expected: "string.length <= 30",
Expand All @@ -63,6 +71,14 @@ export function validateQuote(quote: unknown) {
if (Array.isArray(categories)) {
for (const category of categories) {
if (typeof category === "string") {
if (category.length < 1) {
errors.push({
expected: "string.length <= 1",
message: "Expected each category to be at least 1 character",
received: `${category.length}`,
})
}

if (category.length > 20) {
errors.push({
expected: "string.length <= 20",
Expand All @@ -88,6 +104,14 @@ export function validateQuote(quote: unknown) {
}

if (typeof submitter === "string") {
if (submitter.length < 1) {
errors.push({
expected: "string.length <= 1",
message: "Expected the submitter to be at least 1 character",
received: `${submitter.length}`,
})
}

if (submitter.length > 20) {
errors.push({
expected: "string.length <= 20",
Expand All @@ -104,6 +128,14 @@ export function validateQuote(quote: unknown) {
}

if (typeof text === "string") {
if (text.length < 1) {
errors.push({
expected: "string.length <= 1",
message: "Expected the text to be at least 1 character",
received: `${text.length}`,
})
}

if (text.length > 200) {
errors.push({
expected: "string.length <= 200",
Expand Down

0 comments on commit fcb8e0a

Please sign in to comment.