Skip to content

Is it possible to use zod to validate a comma-separated string #1869

Answered by JacobWeisenburger
grug asked this question in Q&A
Discussion options

You must be logged in to vote

Is this what you are looking for?

const schema = z.object( {
    foo: z.string()
        .transform( value => value.split( ',' ).map( Number ) )
        .pipe( z.number().array() ),
} )

console.log( schema.parse( { foo: '1,2,3,4' } ) )
// { foo: [ 1, 2, 3, 4 ] }

Replies: 2 comments 5 replies

Comment options

You must be logged in to vote
3 replies
@grug
Comment options

@JacobWeisenburger
Comment options

@grug
Comment options

Comment options

You must be logged in to vote
2 replies
@grug
Comment options

@staciax
Comment options

Answer selected by grug
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants