Yup: What exactly does noSortEdges do?

Created on 11 Dec 2019  路  2Comments  路  Source: jquense/yup

I recently had two fields--"a" and "b"-- if a user entered a value into either "a" or "b" then both fields should be made mandatory. I was unable to get this working until I added both of the values to the "noSortEdges" array param as shown below:

object.shape(fields: object, noSortEdges?: Array<[string, string]>): Schema

I can find zero documentation on what this parameters actually _does_, it did seem to fix my cyclical dependency issue, though I have absolutely no idea why.

Anyone?

Thanks!

-CJ

Most helpful comment

It's a ugly escape hatch for dealing with cyclical dependencies. Generally fields are topographically sorted so they can be resolved in the correct order, i.e. field A resolves before field B if field B depends on it.

In some cases tho the order doesn't matter, because the dependency is purely for the sake of validation, which all happen after all casting is done. In those cases it's _fine_ if there are cycles between fields, and you can manually opt out of the ordering, if you are sure that it's not needed. The reason it's not documented is that its a bad API and really easy to shoot one's self in the foot with it. I've had dreams of adding a real API but it's a bit tricky b/c it would require determining if a dependency is validation only, at schema construction, something that isn't really possible.

All 2 comments

It's a ugly escape hatch for dealing with cyclical dependencies. Generally fields are topographically sorted so they can be resolved in the correct order, i.e. field A resolves before field B if field B depends on it.

In some cases tho the order doesn't matter, because the dependency is purely for the sake of validation, which all happen after all casting is done. In those cases it's _fine_ if there are cycles between fields, and you can manually opt out of the ordering, if you are sure that it's not needed. The reason it's not documented is that its a bad API and really easy to shoot one's self in the foot with it. I've had dreams of adding a real API but it's a bit tricky b/c it would require determining if a dependency is validation only, at schema construction, something that isn't really possible.

Got it--thank you!

Was this page helpful?
0 / 5 - 0 ratings