-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
A way to cast the result of a array to Set and object to Map #1914
Comments
Some further thoughts: The Joi.boolean().transform({ pre: Boolean }); |
My main concerns are that people will expect async support and that this basically breaks the ability to describe a schema. I assume your main use case is to apply transformation for parts of an input inside a larger validation. Otherwise, you can just call whatever you want on the result of the validation function. |
People already expect async support from Joi, so I don't really see how this changes things. Anyway, this is somewhat redundant if we address your second "describe" concern, which I hadn't thought about. In that case, I would go back to const schema = Joi.object().keys({
list: Joi.array().items(Joi.number()).unique().cast('set')
}); This will require Joi to implement logic for any such cast, probably beginning with And yes, this is really about multiple such casts. I made a |
OTOH do we really want to describe casts ? Should it mean something for the data being validated ? That seems like internal stuff. I'd wonder how should a cast fail though. |
Describe the problem you are trying to fix (provide as much context as possible)
I have a configuration file, where an array is included containing unique elements.
JSON:
Schema:
Once this has been validated with joi, I want to convert this array into a
Set
for fastO(1)
complexity lookups.Which API (or modification of the current API) do you suggest to solve that problem ?
While I can do the casting manually in a post-processing stage, I think an
any.cast()
option, which converts the rule value post-validation, could provide a great deal of value. Not only for this case, but others as well (eg. object toMap
conversion). There is already precedent for such an output modifier with theany.raw()
rule.A simple and powerful way to do this, would be to just pass a method:
any.cast(fn)
Outputs the value returned from calling
fn(value)
instead of the casted or raw value.The downside, is that it requires handling errors in the casting function, and that it might be abused to apply other stateful changes to the output. Eg.
Joi.any().optional().cast(() => new Date())
to create a rule that adds the current date to an object. If that is the implementation,any.transform()
might actually be a better name for it.Are you ready to work on a pull request if your suggestion is accepted ?
Yes
The text was updated successfully, but these errors were encountered: