Skip to content

Commit

Permalink
instanceof keyword didn't work. changed to use schema._def.typeName
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobWeisenburger committed Feb 21, 2024
1 parent 12aa7fd commit f3ccca3
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zod_utilz",
"version": "0.8.1-beta",
"version": "0.8.0",
"author": "JacobWeisenburger",
"description": "Framework agnostic utilities for Zod",
"license": "MIT",
Expand Down
13 changes: 7 additions & 6 deletions src/coerce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,13 @@ export function coerce<Schema extends AllowedZodTypes> ( schema: Schema ) {
}

function getTransformer<Schema extends AllowedZodTypes> ( schema: Schema ) {
if ( schema instanceof z.ZodAny ) return ( value: any ) => value
if ( schema instanceof z.ZodString ) return toString
if ( schema instanceof z.ZodNumber ) return toNumber
if ( schema instanceof z.ZodBoolean ) return toBoolean
if ( schema instanceof z.ZodBigInt ) return toBigInt
if ( schema instanceof z.ZodArray ) return toArray( schema )
if ( schema._def.typeName === 'ZodAny' ) return ( value: any ) => value
if ( schema._def.typeName === 'ZodString' ) return toString
if ( schema._def.typeName === 'ZodNumber' ) return toNumber
if ( schema._def.typeName === 'ZodBoolean' ) return toBoolean

if ( schema._def.typeName === 'ZodBigInt' ) return toBigInt
if ( schema._def.typeName === 'ZodArray' ) return toArray( schema as z.ZodArray<any> )

throw new Error( `${ schema!.constructor.name } is not supported by zu.coerce` )
}
Expand Down

0 comments on commit f3ccca3

Please sign in to comment.