New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Customize Error Paths #102
Comments
Would you be opposed to returning an object from a custom type something like |
Returning an object is interesting. I’d be down for that if you wanted to make a PR. I’d generally try to avoid these kinds of setups, but if two values are co-dependent then there’s no other way really. Good solution! |
@ianstormtaylor thanks for the quick response
Yes i can definitely do a PR
I'm open to other ideas, originally i was thinking about returning an array like |
@davidchase what's the data shape you're trying to assert? It's a bit hard to tell from the code sample above what "correct" data can look like. |
so for example we have const data = {
123: {
state: 'PA',
county: null
},
456: {
state: 'CA',
county: null
},
789: {
state: 'PA',
county: null
}
} Updated way of having dynamic keys with either state or county filled in const State = st.object({ state: 'string', county: 'string|null' })
const County = st.object({ state: 'string|null', county: 'string' })
const StateOrCounty = st.union([State, County])
const EitherStateOrCounty = st.dict(['string', StateOrCounty]) Now i need to validate duplicates based on the original code i had in the OP so in the above case i would want to return and object with does this help? finding duplicates still yields a an empty array path which is why i purpose an object with |
Interesting! So what if you structured it as such: const State = struct.object({
state: 'string',
county: 'string|null'
})
const County = struct.object({
state: 'string|null',
county: 'string'
})
const StateOrCounty = struct.union([
State,
County
])
const StateOrCountyDict = stuct.dict([
'string',
StateOrCounty
])
const StateOrCountyDictWithoutDuplicates = struct.union([
StateOrCountyDictionary,
'unique_array'
]) Where I'm down for that! |
Although, also consider that you may consider the entire array invalid, instead of considering individual paths invalid. Although I'm down for the PR either way. |
Can you explain why its a union? Essentially i need to first validate to see if my object with dynamic keys has either state or county filled in... both fields are allowed. Followed by checking to see if any dynamic key object has duplicate values. In the above scenario So i would have thought that would be an & (intersection) vs an | (union), no? I also like that technically i can just compose like
It seems like if i build custom types in superstruct my
Would you want to keep string and boolean and now have a return object? or maybe just get rid of string and have the message and path be optional in the object? |
started a PR to continue the discussion :) |
@davidchase ah you're right, I meant |
Done in |
Sure thanks for adding to your library |
Hi 👋 really digging this library for validating objects
My only question is about customizing paths when creating types via superstruct
Currently the above code checks if the dynamic keys have either state or county filled in and then if either state or county is a duplicate
Returning custom messages works great but for whatever reason the
paths
array is always empty which brings me to my question of customizing pathsbecause i have all of the information i need to create a path but no obvious way of doing, unless I am missing something ?
The text was updated successfully, but these errors were encountered: