There should be an option for JSON::Validator to handle "default" values.
JSON::Validator already sort of mutate the incoming data if coercion is enabled, so we could also use $jv->coerce->{defaults} = 1. Doing this is a clean API imo, but it raises the question if if should be enabled on $jv->coerce(1).
Using such a flag fixes the main issue in #55, where we don't want the input to be "munged" without the user explicitly wanted that. Another suggestion was to add validate_and_set_default(), but I don't want another method.
There should be an option for JSON::Validator to handle "default" values.
JSON::Validator already sort of mutate the incoming data if coercion is enabled, so we could also use
$jv->coerce->{defaults} = 1
. Doing this is a clean API imo, but it raises the question if if should be enabled on$jv->coerce(1)
.Using such a flag fixes the main issue in #55, where we don't want the input to be "munged" without the user explicitly wanted that. Another suggestion was to add
validate_and_set_default()
, but I don't want another method.This implementation need to set the default value before doing the validation, unless the data already exists. Note exists, since undef and other false values are still values. (Even though I suggested otherwise in https://github.com/mojolicious/json-validator/issues/55#issuecomment-284138337)
References: