A schema for an array that includes at least one item of a certain type #2937
Replies: 7 comments 6 replies
-
Is this what you are looking for? const schema = z.string().array().min( 1 )
console.log( schema.parse( [ 'foo' ] ) ) // [ 'foo' ]
const result = schema.safeParse( [] )
!result.success && console.log( result.error.issues )
// [
// {
// code: 'too_small',
// minimum: 1,
// type: 'array',
// inclusive: true,
// exact: false,
// message: 'Array must contain at least 1 element(s)',
// path: []
// }
// ] If you found my answer satisfactory, please consider supporting me. Even a small amount is greatly appreciated. Thanks friend! 🙏 |
Beta Was this translation helpful? Give feedback.
-
I'm assuming you changed the title because I didn't give you the answer you were looking for. Could you please provide some data that would pass and some that would fail the schema you are wanting to make? I would love to help, but I need a little more info to be able to help. |
Beta Was this translation helpful? Give feedback.
-
Hi, thank you for your answer. I need it to be an array that can contain many types (or many schemas)
|
Beta Was this translation helpful? Give feedback.
-
Is this what you are looking for? const bookSchema = z.object( { type: z.literal( 'book' ) } )
const movieSchema = z.object( { type: z.literal( 'movie' ) } )
const comicSchema = z.object( { type: z.literal( 'comic' ) } )
const schema = z.object( {
books: bookSchema.array().min( 1 ),
movies: movieSchema.array().min( 1 ),
comics: comicSchema.array(),
} )
const validData = {
books: [ { type: 'book' } ],
movies: [ { type: 'movie' } ],
comics: [ { type: 'comic' } ]
}
console.log( schema.safeParse( validData ).success )
const invalidData = {
books: [ { type: 'book' } ],
movies: [],
comics: [ { type: 'comic' } ]
}
console.log( schema.safeParse( invalidData ).success ) If you found my answer satisfactory, please consider supporting me. Even a small amount is greatly appreciated. Thanks friend! 🙏 |
Beta Was this translation helpful? Give feedback.
-
No, because I have an array of products, not object.
Tomorrow I may have a new kind of product which adheres to a new schema
(e.g. toySchema) and I would want to validate whether a product list
contains one (or more) items which are toys and one or more items which are
books.
According to your suggestion, I would have to keep adding fields to the
base schema whenever I have a new specific product schema.
…On Mon, 6 Nov 2023 at 18:05 Jacob Weisenburger ***@***.***> wrote:
Is this what you are looking for?
const bookSchema = z.object( { type: z.literal( 'book' ) } )const movieSchema = z.object( { type: z.literal( 'movie' ) } )const comicSchema = z.object( { type: z.literal( 'comic' ) } )
const schema = z.object( {
books: bookSchema.array().min( 1 ),
movies: movieSchema.array().min( 1 ),
comics: comicSchema.array(),} )
const validData = {
books: [ { type: 'book' } ],
movies: [ { type: 'movie' } ],
comics: [ { type: 'comic' } ]}console.log( schema.safeParse( validData ).success )
const invalidData = {
books: [ { type: 'book' } ],
movies: [],
comics: [ { type: 'comic' } ]}console.log( schema.safeParse( invalidData ).success )
If you found my answer satisfactory, please consider supporting me. Even a
small amount is greatly appreciated. Thanks friend! 🙏
https://github.com/sponsors/JacobWeisenburger
—
Reply to this email directly, view it on GitHub
<#2937 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/BC2BBMVG7OQPCXHO4ZREI2TYDEDDNAVCNFSM6AAAAAA67WYKKOVHI2DSMVQWIX3LMV43SRDJONRXK43TNFXW4Q3PNVWWK3TUHM3TIOBZGQYTS>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
Yes, that’s why I submitted that as an issue first, as a feature request.
…On Mon, 6 Nov 2023 at 18:43 Jacob Weisenburger ***@***.***> wrote:
as far as I can see, there isn't a built in way to do what you are asking,
so you will likely need to do something with refine or transform.
—
Reply to this email directly, view it on GitHub
<#2937 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/BC2BBMUEWI66JOQPSQR5OP3YDEHUXAVCNFSM6AAAAAA67WYKKOVHI2DSMVQWIX3LMV43SRDJONRXK43TNFXW4Q3PNVWWK3TUHM3TIOBZHA3DI>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
It’s bigger than just me…
But I also don’t think it such a weird structuring.
Example:
You have shopping carts which are arrays of products (it makes no sense to
define them as objects). Then, for example, you want to define a
discountEntitledCartScema as a cart that contains both a book and a movie.
How would you do it differently?
…On Mon, 6 Nov 2023 at 20:36 Jacob Weisenburger ***@***.***> wrote:
Honestly, I have never seen anyone try to do what you are doing. So if it
were me, I would figure out a way to not combine different types of data
into an array. Arrays are easiest when they only have 1 type of data. Is
that something you are able to change? or is the project bigger than just
you?
—
Reply to this email directly, view it on GitHub
<#2937 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/BC2BBMX2VTZAFHPZJZ2DNHDYDEUZNAVCNFSM6AAAAAA67WYKKOVHI2DSMVQWIX3LMV43SRDJONRXK43TNFXW4Q3PNVWWK3TUHM3TIOJQHEZDI>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
I imagine something like:
Seems like a very common use case (I need it right now for my project).
Is there a way to do such thing without refine?
Beta Was this translation helpful? Give feedback.
All reactions