top | item 24433207

(no title)

bmillare | 5 years ago

Found the answer to s/keys and :opt "The :opt keys serve as documentation and may be used by the generator."

discuss

order

didibus|5 years ago

The important aspect to keep in mind is that it makes the key optional, not the value of the key.

Thus if you have a map without the key, the value won't be validated. But if the key is present, then it will validate its value.

If you want to make the value of the key optional, in the spec for the value of the key, you need to add `nil?` as a valid value.

jwr|5 years ago

> Thus if you have a map without the key, the value won't be validated. But if the key is present, then it will validate its value.

Exactly. Which is something I did not expect. I expected '(s/valid? ::my-spec x)' to tell me if x is valid according to ::my-spec, checking only those keys that ::my-spec lists in :req and :opt (if present).

For maps, you might as well think of s/valid? as ignoring the first parameter. It validates anything it can.