schema

donavan 2018-08-28T09:40:25.000100Z

Hi, I’m looking for a way to turn off coercion on a per Schema basis. Is this possible? To be specific: due to java.util.UUID.fromString() the UUID schema accepts and pads input that has less chars than a UUID. This is undesirable for us, we’d rather the validation fail.

tanzoniteblack 2018-08-28T13:58:08.000100Z

@donavan Can you give an example of a string that is being padded? When I run something like (java.util.UUID/fromString "df2-482d-ba74-fa6d491d78c6") I get an exception: IllegalArgumentException Invalid UUID string: df2-482d-ba74-fa6d491d78c6 java.util.UUID.fromString (UUID.java:215)

donavan 2018-08-28T14:02:02.000100Z

(java.util.UUID/fromString "9ccd5ac-aab7-11e8-98d0-529269fb1459") does the trick. First group is short one digit.

tanzoniteblack 2018-08-28T14:37:40.000100Z

(def no-padding-uuid (schema.core/conditional 
             (fn [uuid-input]
             (= uuid-input (str (java.util.UUID/fromString uuid-input))))
              java.util.UUID))

tanzoniteblack 2018-08-28T14:38:18.000100Z

If you do that, then yada (I saw your original message on #yada ) should coerce it only if the uuid is what you really want

tanzoniteblack 2018-08-28T14:38:56.000100Z

(schema-tools.coerce/coerce "09ccd5ac-aab7-11e8-98d0-529269fb1459" no-padding-uuid yada.parameters/query-coercions) ==>`#uuid "09ccd5ac-aab7-11e8-98d0-529269fb1459"` but: (schema-tools.coerce/coerce "9ccd5ac-aab7-11e8-98d0-529269fb1459" no-padding-uuid yada.parameters/query-coercions) throws an exception

tanzoniteblack 2018-08-28T14:39:42.000100Z

you could also do a string length check, or whatever exact conditions you want to enforce?

tanzoniteblack 2018-08-28T14:58:30.000100Z

^^^ @donavan

donavan 2018-08-28T15:00:17.000100Z

Thanks, I’m just in the middle of something else, will try ASAP 🙂

tanzoniteblack 2018-08-28T15:00:39.000100Z

no worries. Realized you probably hadn't seen that since I posted it a half hour later 🙂

tanzoniteblack 2018-08-28T15:01:15.000100Z

I haven't tried it via the actual yada parameter coercion, but since the coercer I used is what they use for parameters, I believe it will work