jhthorsen / json-validator

:cop: Validate data against a JSON schema
https://metacpan.org/release/JSON-Validator
56 stars 58 forks source link

Add support for default values #155

Closed jhthorsen closed 2 years ago

jhthorsen commented 5 years ago

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:

jhthorsen commented 5 years ago

This should be solved in in 3.08.

jhthorsen commented 4 years ago

I think "defaults" is buggy. I can't remember the issues, but going to re-open anyways.

jhthorsen commented 2 years ago

Can't remember what the issue was 😞