Skip to content

Object key/value order changes after parsing #1852

Discussion options

You must be logged in to vote

This way keeps the type safety. Does this work for you?

function returnDataInSameOrderAsPassed<Schema extends z.ZodObject<z.ZodRawShape>> (
    schema: Schema
) {
    return z.custom( value => schema.safeParse( value ).success ) as Schema
    // return z.custom<z.infer<Schema>>( value => schema.safeParse( value ).success )
    // this could also work ^^^^^^
}

const objSchema = z.object( {
    one: z.number(),
    two: z.number(),
    three: z.number(),
} )

console.log( objSchema.parse( {
    three: 3,
    two: 2,
    one: 1,
} ) )
// { one: 1, two: 2, three: 3 }

const sameOrderAsPassedSchema = returnDataInSameOrderAsPassed( objSchema )
type SameOrderAsPassedSchema = z.infer<typeof same…

Replies: 4 comments 8 replies

Comment options

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

@JacobWeisenburger
Comment options

Comment options

You must be logged in to vote
5 replies
@bconnorwhite
Comment options

@JacobWeisenburger
Comment options

@bconnorwhite
Comment options

@andrewrjohn
Comment options

@andrewrjohn
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@king-of-poppk
Comment options

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

This discussion was converted from issue #1850 on January 10, 2023 21:06.