question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

schema.cast reverses order of keys in objects

See original GitHub issue

Describe 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:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
TacianoBackcommented, Feb 24, 2021

You can try this in your validate function.

const shape = {
	name: yup.string().max(100),
	email: yup.string().max(60).email(),
}

const newShape = Object.entries(shape)
	.reverse()
	.reduce((prev, [key, value]) => ({ ...prev, [key]: value }), {})

const schema = yup.object().shape(newShape)
1reaction
rheidaricommented, Nov 27, 2019

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 the Object.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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found