joi icon indicating copy to clipboard operation
joi copied to clipboard

basic validate is failed ...

Open mydearxym opened this issue 4 years ago • 5 comments

given schema:

schema = %{
    checked: [:string],
     indent: [:string],
}

Joi.validate(%{checked: true, indent: 0}, schema)
# => {:ok, %{checked: "true", indent: "0"}}

image

mydearxym avatar Feb 14 '21 05:02 mydearxym

That's not a bug, Joi.validate/2 doesn't just do the validation. It also does conversions

scottming avatar Feb 14 '21 06:02 scottming

why conversion ? what's the point ?

image

mydearxym avatar Feb 14 '21 11:02 mydearxym

When I have time, I will implement a global or local setting to control whether or not to convert this field. But I haven't figured out where to put this setting. Do you have any suggestions?

scottming avatar Mar 22 '21 02:03 scottming

Do you have any suggestions?

I think the default behavior should be a strict one (no coercion done by default). individually each field could accept a keyword coerce: true to allow coercion for that field. Also, the Joi.validate can accept coerce: true as an option to try to coerce every field that hasn't explicitly defined a coercion option.

y86 avatar Jul 28 '21 21:07 y86

Thank you, very good suggestion, I am going to implement this conversion option in the next version.

@y86

scottming avatar Jul 30 '21 01:07 scottming