@@ -280,10 +280,10 @@ export interface StructOptions {
280280 *
281281 * @since 2.0.0
282282 */
283- export function Struct < Fields extends Schema . Struct . Fields > (
283+ export const Struct = < Fields extends Schema . Struct . Fields > (
284284 fields : Fields ,
285285 options : StructOptions = { }
286- ) : Struct < Fields > {
286+ ) : Struct < Fields > => {
287287 const { flatFields, flatInUnion, index = 0 , tagField } = options
288288
289289 // flatInUnion defaults to true when index is specified
@@ -325,9 +325,10 @@ export function Struct<Fields extends Schema.Struct.Fields>(
325325 encode : ( encodedStruct ) => {
326326 // encodedStruct is the result of Schema.Struct(fields), which has already transformed all fields
327327
328- // Filter out the tag field if detected (it's metadata, not data)
329- const fieldEntries = Object . entries ( encodedStruct ) . filter ( ( [ key ] ) => key !== detectedTagField )
330- const fieldValues = fieldEntries . map ( ( [ _ , value ] ) => value ) as ReadonlyArray < Data . Data >
328+ // Use Object.keys(fields) to preserve schema definition order
329+ // rather than Object.entries(encodedStruct) which would use runtime object order
330+ const orderedKeys = Object . keys ( fields ) . filter ( ( key ) => key !== detectedTagField )
331+ const fieldValues = orderedKeys . map ( ( key ) => encodedStruct [ key as keyof typeof encodedStruct ] ) as ReadonlyArray < Data . Data >
331332
332333 // Check if any field values are Constrs with flatFields:true
333334 // If so, spread their fields into this Struct's field array
@@ -793,24 +794,24 @@ export const Tuple = <Elements extends Schema.TupleType.Elements>(element: [...E
793794 * @since 2.0.0
794795 * @category constructors
795796 */
796- export function Variant < const Variants extends Record < PropertyKey , Schema . Struct . Fields > > (
797+ export const Variant = < const Variants extends Record < PropertyKey , Schema . Struct . Fields > > (
797798 variants : Variants
798799) : Union <
799800 ReadonlyArray <
800801 {
801802 [ K in keyof Variants ] : Struct < { readonly [ P in K ] : Struct < Variants [ K ] > } >
802803 } [ keyof Variants ]
803804 >
804- > {
805+ > => {
805806 return Union (
806- ...Object . entries ( variants ) . map ( ( [ name , fields ] , index ) =>
807+ ...( Object . entries ( variants ) . map ( ( [ name , fields ] , index ) =>
807808 Struct (
808809 {
809810 [ name ] : Struct ( fields , { flatFields : true } )
810811 } as any ,
811812 { flatInUnion : true , index }
812813 )
813- ) as any
814+ ) as any )
814815 )
815816}
816817
0 commit comments