Skip to content

Commit

Permalink
fix: 🚑 Fix ZodSchemaCompat type incompatibility with zod
Browse files Browse the repository at this point in the history
  • Loading branch information
pklaschka committed Dec 7, 2024
1 parent f7bda2f commit 9636fbd
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
26 changes: 15 additions & 11 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ import { getLogger } from "@std/log";
export class EnvNotSetError extends Error {
/**
* Create a new {@link EnvNotSetError}.
*
*
* Use this when an environment variable is not set and the application tries to access it.
*
*
* @param envVariable name of the environment variable
* @param cause the cause of the error, if any
*/
Expand Down Expand Up @@ -244,7 +244,7 @@ function logger() {
* A type that is compatible with Zod schemas.
*/
export type ZodSchemaCompat = {
safeParse: (value?: string) => { error: Error | null | undefined };
safeParse: (value?: string) => { error?: Error };
isOptional: () => boolean;
};

Expand All @@ -271,9 +271,9 @@ export const REQUIRED: ZodSchemaCompat = {

/**
* A `ZodSchemaCompat` validator that represents an optional variable.
*
*
* Useful for projects where you don't need full-blown Zod schemas.
*
*
* @example
* ```typescript
* import { initVariable, OPTIONAL } from "@wuespace/envar/";
Expand All @@ -292,9 +292,9 @@ export const OPTIONAL: ZodSchemaCompat = {

/**
* A `ZodSchemaCompat` validator that represents a required, non-empty variable.
*
*
* Useful for projects where you don't need full-blown Zod schemas.
*
*
* @example
* ```typescript
* import { initVariable, REQUIRED_NON_EMPTY } from "@wuespace/envar/";
Expand All @@ -307,16 +307,18 @@ export const REQUIRED_NON_EMPTY: ZodSchemaCompat = {
safeParse: (val) => ({
error: typeof val === "string" && val.length > 0
? undefined
: new Error(`Expected value to be a non-empty string, but got "${val?.toString()}"`),
: new Error(
`Expected value to be a non-empty string, but got "${val?.toString()}"`,
),
}),
};

/**
* A `ZodSchemaCompat` validator that represents an optional, non-empty variable.
* Valid values are non-empty strings or undefined. Any other value is invalid.
*
*
* Useful for projects where you don't need full-blown Zod schemas.
*
*
* @example
* ```typescript
* import { initVariable, OPTIONAL_NON_EMPTY } from "@wuespace/envar/";
Expand All @@ -328,6 +330,8 @@ export const OPTIONAL_NON_EMPTY: ZodSchemaCompat = {
safeParse: (val) => ({
error: typeof val === "string" && val.length > 0 || val === undefined
? undefined
: new Error(`Expected value to be a non-empty string or unset, but got "${val?.toString()}"`),
: new Error(
`Expected value to be a non-empty string or unset, but got "${val?.toString()}"`,
),
}),
};
4 changes: 2 additions & 2 deletions mod_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const PARSE_ERROR = new Error("parse error");

const OPTIONAL_PASSING = {
isOptional: () => true,
safeParse: () => ({ error: null }),
safeParse: () => ({ error: undefined }),
};
const OPTIONAL_FAILING = {
isOptional: () => true,
Expand All @@ -55,7 +55,7 @@ const OPTIONAL_FAILING = {
const REQUIRED_PASSING = {
isOptional: () => false,
safeParse: (val: unknown) => ({
error: typeof val === "string" ? null : PARSE_ERROR,
error: typeof val === "string" ? undefined : PARSE_ERROR,
}),
};
const REQUIRED_FAILING = {
Expand Down

0 comments on commit 9636fbd

Please sign in to comment.