Skip to content

Commit 9636fbd

Browse files
committed
fix: 🚑 Fix ZodSchemaCompat type incompatibility with zod
1 parent f7bda2f commit 9636fbd

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

mod.ts

+15-11
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ import { getLogger } from "@std/log";
3030
export class EnvNotSetError extends Error {
3131
/**
3232
* Create a new {@link EnvNotSetError}.
33-
*
33+
*
3434
* Use this when an environment variable is not set and the application tries to access it.
35-
*
35+
*
3636
* @param envVariable name of the environment variable
3737
* @param cause the cause of the error, if any
3838
*/
@@ -244,7 +244,7 @@ function logger() {
244244
* A type that is compatible with Zod schemas.
245245
*/
246246
export type ZodSchemaCompat = {
247-
safeParse: (value?: string) => { error: Error | null | undefined };
247+
safeParse: (value?: string) => { error?: Error };
248248
isOptional: () => boolean;
249249
};
250250

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

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

293293
/**
294294
* A `ZodSchemaCompat` validator that represents a required, non-empty variable.
295-
*
295+
*
296296
* Useful for projects where you don't need full-blown Zod schemas.
297-
*
297+
*
298298
* @example
299299
* ```typescript
300300
* import { initVariable, REQUIRED_NON_EMPTY } from "@wuespace/envar/";
@@ -307,16 +307,18 @@ export const REQUIRED_NON_EMPTY: ZodSchemaCompat = {
307307
safeParse: (val) => ({
308308
error: typeof val === "string" && val.length > 0
309309
? undefined
310-
: new Error(`Expected value to be a non-empty string, but got "${val?.toString()}"`),
310+
: new Error(
311+
`Expected value to be a non-empty string, but got "${val?.toString()}"`,
312+
),
311313
}),
312314
};
313315

314316
/**
315317
* A `ZodSchemaCompat` validator that represents an optional, non-empty variable.
316318
* Valid values are non-empty strings or undefined. Any other value is invalid.
317-
*
319+
*
318320
* Useful for projects where you don't need full-blown Zod schemas.
319-
*
321+
*
320322
* @example
321323
* ```typescript
322324
* import { initVariable, OPTIONAL_NON_EMPTY } from "@wuespace/envar/";
@@ -328,6 +330,8 @@ export const OPTIONAL_NON_EMPTY: ZodSchemaCompat = {
328330
safeParse: (val) => ({
329331
error: typeof val === "string" && val.length > 0 || val === undefined
330332
? undefined
331-
: new Error(`Expected value to be a non-empty string or unset, but got "${val?.toString()}"`),
333+
: new Error(
334+
`Expected value to be a non-empty string or unset, but got "${val?.toString()}"`,
335+
),
332336
}),
333337
};

mod_test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ const PARSE_ERROR = new Error("parse error");
4646

4747
const OPTIONAL_PASSING = {
4848
isOptional: () => true,
49-
safeParse: () => ({ error: null }),
49+
safeParse: () => ({ error: undefined }),
5050
};
5151
const OPTIONAL_FAILING = {
5252
isOptional: () => true,
@@ -55,7 +55,7 @@ const OPTIONAL_FAILING = {
5555
const REQUIRED_PASSING = {
5656
isOptional: () => false,
5757
safeParse: (val: unknown) => ({
58-
error: typeof val === "string" ? null : PARSE_ERROR,
58+
error: typeof val === "string" ? undefined : PARSE_ERROR,
5959
}),
6060
};
6161
const REQUIRED_FAILING = {

0 commit comments

Comments
 (0)