schema.cast reverses order of keys in objects
See original GitHub issueDescribe the bug Keys are being reversed when casting an object.
To Reproduce https://repl.it/@rheidari/yup-reversing-keys
Expected behavior Expect the keys defined in the schema to be the same order coming out when cast.
Platform: Node v8.16.0
Additional context
Noticed that there’s a .reverse()
being called in sortFields. When I remove that it acts as expected, however I’m sure you had a reason for it being there https://github.com/jquense/yup/blob/1426feceb6c5544c526711bedcf388afaf6115b9/src/util/sortFields.js#L31
Issue Analytics
- State:
- Created 4 years ago
- Reactions:3
- Comments:7 (1 by maintainers)
Top Results From Across the Web
node.js - Why MongooseJS is reversing the order of properties?
Yes, I do understand that properties order should not be considered in object. But If you're using a for..in loop, properties with numerical...
Read more >DataWeave Delight: Reversing order of keys in objects - MrHAKI
Finally we need to reduce the array of objects into a new object using the reduce function, where the ordering of keys will...
Read more >How to invert key value in JavaScript object ? - GeeksforGeeks
First Approach: In this example, we will demonstrate the conventional method of inverting key-value pairs. At first, a student object is created ...
Read more >17.7.0 API Reference - joi.dev
The schema can be a plain JavaScript object where every key is assigned a joi type, ... Each joi schema type supports its...
Read more >164 - Wrong order in Object properties interation - v8 - Monorail
I and many developers use this concept many years. For iteration as property in object is very convenient. And no matter what keys...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
You can try this in your validate function.
Thanks for the explanation @jquense. Is there a way I can best go about re-ordering the cast object so that the original order of fields is maintained as per the schema, or will I need to do that outside of yup? I tried implementing a
transform()
on each object to recreate the object ordered by theObject.keys(schema.fields)
but that didn’t seem to have any effect.In my case I have a deeply nested object that will need to retain the order defined in each yup schema as I’m going to be transforming it back into XML.