feat: initial commit introducing NonEmptyString schema
This commit is contained in:
18
src/non-empty-string.js
Normal file
18
src/non-empty-string.js
Normal file
@@ -0,0 +1,18 @@
|
||||
import * as z from "zod";
|
||||
|
||||
const NAME = "NonEmptyString";
|
||||
|
||||
/**
|
||||
* @returns {z.core.$ZodBranded<z.ZodString, typeof NAME, "out">}
|
||||
*/
|
||||
function t() {
|
||||
return z.string().trim().nonempty().brand(NAME).meta({
|
||||
description:
|
||||
"A string that contains at least some characters after being trimmed of leading and trailing whitespace",
|
||||
title: NAME,
|
||||
});
|
||||
}
|
||||
|
||||
export const NonEmptyString = t();
|
||||
/** @typedef {z.output<typeof NonEmptyString>} NonEmptyString */
|
||||
/** @typedef {z.input<typeof NonEmptyString>} NonEmptyStringInput */
|
||||
65
src/non-empty-string.spec.js
Normal file
65
src/non-empty-string.spec.js
Normal file
@@ -0,0 +1,65 @@
|
||||
import { faker } from "@faker-js/faker";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { NonEmptyString } from "./non-empty-string.js";
|
||||
|
||||
describe("NonEmptyString", () => {
|
||||
const WHITESPACE_CHARACTERS = {
|
||||
NEWLINE: "\n",
|
||||
SPACE: " ",
|
||||
TAB: "\t",
|
||||
};
|
||||
|
||||
it("rejects true", () =>
|
||||
// @ts-expect-error because only strings are valid inputs
|
||||
expect(NonEmptyString.safeDecode(true).success).toBe(false));
|
||||
|
||||
it("rejects false", () =>
|
||||
// @ts-expect-error because only strings are valid inputs
|
||||
expect(NonEmptyString.safeDecode(false).success).toBe(false));
|
||||
|
||||
it("rejects integers", { repeats: 10 }, () =>
|
||||
// @ts-expect-error because only strings are valid inputs
|
||||
expect(NonEmptyString.safeDecode(faker.number.int()).success).toBe(false),
|
||||
);
|
||||
|
||||
it("rejects bigints", { repeats: 10 }, () =>
|
||||
// @ts-expect-error because only strings are valid inputs
|
||||
expect(NonEmptyString.safeDecode(faker.number.bigInt()).success).toBe(
|
||||
false,
|
||||
),
|
||||
);
|
||||
|
||||
it("rejects floats/numbers", { repeats: 10 }, () =>
|
||||
// @ts-expect-error because only strings are valid inputs
|
||||
expect(NonEmptyString.safeDecode(faker.number.float()).success).toBe(false),
|
||||
);
|
||||
|
||||
it("rejects arrays", () =>
|
||||
// @ts-expect-error because only strings are valid inputs
|
||||
expect(NonEmptyString.safeDecode([]).success).toBe(false));
|
||||
|
||||
it("rejects plain objects", () =>
|
||||
// @ts-expect-error because only strings are valid inputs
|
||||
expect(NonEmptyString.safeDecode({}).success).toBe(false));
|
||||
|
||||
it("rejects empty strings", () =>
|
||||
expect(NonEmptyString.safeDecode("").success).toBe(false));
|
||||
|
||||
it("rejects strings containing only whitespace characters", () =>
|
||||
expect(
|
||||
NonEmptyString.safeDecode(
|
||||
faker.helpers
|
||||
.multiple(() => faker.helpers.enumValue(WHITESPACE_CHARACTERS), {
|
||||
count: { min: 1, max: 100 },
|
||||
})
|
||||
.join(""),
|
||||
).success,
|
||||
).toBe(false));
|
||||
|
||||
it("accepts strings with at least 1 non-whitespsace character", () =>
|
||||
expect(
|
||||
NonEmptyString.safeDecode(
|
||||
faker.string.alphanumeric({ length: { min: 1, max: 255 } }),
|
||||
).success,
|
||||
).toBe(true));
|
||||
});
|
||||
Reference in New Issue
Block a user