Skip to content

Commit

Permalink
updated z.custom example again :D
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobWeisenburger committed Mar 5, 2023
1 parent 1749657 commit dabe63d
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions README.md
Expand Up @@ -1760,15 +1760,16 @@ This returns a `ZodEffects` instance. `ZodEffects` is a wrapper class that conta
You can create a Zod schema for any TypeScript type by using `z.custom()`. This is useful for creating schemas for types that are not supported by Zod out of the box, such as template string literals.

```ts
const px = z.custom<`${ number }px`>( x => {
const isString = z.string().safeParse( x )
if ( isString.success )
return /^\d+px$/.test( isString.data )
} )
console.log( px.safeParse( '100px' ).success ) // true
console.log( px.safeParse( '100vw' ).success ) // false
console.log( px.safeParse( '' ).success ) // false
const px = z.custom<`${ number }px`>( x =>
z.string().regex( /^\d+px$/ ).safeParse( x ).success
)
type Px = z.infer<typeof px>
// type Px = `${number}px`

console.log( px.safeParse( '42px' ).success ) // true
console.log( px.safeParse( '42vw' ).success ) // false
console.log( px.safeParse( 42 ).success ) // false
console.log( px.safeParse( 42n ).success ) // false
console.log( px.safeParse( null ).success ) // false
```

Expand Down

0 comments on commit dabe63d

Please sign in to comment.