FU
view release on metacpan or search on metacpan
FU/Validate.pm view on Meta::CPAN
=item unknown => $option
Implies C<< type => 'hash' >>, this option specifies what to do with keys in
the input data that have not been defined in the I<keys> option. Possible
values are I<remove> to remove unknown keys from the output data (this is the
default), I<reject> to return an error if there are unknown keys in the input,
or I<pass> to pass through any unknown keys to the output data. Values for
passed-through keys are only validated when the I<values> option is set,
otherwise they are passed through as-is. This option has no effect when the
I<keys> option is never set, in that case all values are always passed through.
In the case of I<reject>, the error object will look like:
{ validation => 'unknown',
# List of unknown keys present in the input
keys => ['unknown1', .. ],
# List of known keys (which may or may not be present
# in the input - that is checked at a later stage)
expected => ['known1', .. ]
}
=item missing => $option
For values inside a hash I<keys> schema, this option specifies what to do when
the key is not present in the input data. Possible values are I<create> to
insert the key with a default value (if the I<default> option is set, otherwise
undef), I<reject> to return an error if the option is missing or I<ignore> to
leave the key out of the returned data.
The default is I<create>, but if no I<default> option is set for this key then
that is effectively the same as I<reject>. Values created through I<create> are
still validated through I<values> if that has been set.
In the case of I<reject>, the error object will look like:
{ validation => 'missing',
key => 'field'
}
=item elems => $schema
Implies C<< type => 'array' >>, this defines the schema that is applied to
every element in the array. The schema definition may be a bare hashref or a
validator returned by C<compile()>.
Failure is reported in a similar fashion to I<keys>:
{ validation => 'elems',
errors => [
{ index => 1, validation => 'required' }
]
}
=item accept_scalar => 0/1
Implies C<< type => 'array' >>, this option will also permit the input to be a
scalar. In that case, the input is interpreted and returned as an array with
only one element. This option exists to make it easy to validate multi-value
form inputs. For example, consider C<query_decode()> in L<FU::Util>: a
parameter in a query string is decoded into an array if it is listed multiple
times, a scalar if it only occcurs once. So we could either end up with:
{ a => 1, b => 1 }
# OR:
{ a => [1, 3], b => 1 }
With the I<accept_scalar> option, we can accept both forms for C<a> and
normalize into an array. The following schema definition can validate the above
examples:
{ type => 'hash',
keys => {
a => { type => 'array', accept_scalar => 1 },
b => { }
}
}
=item accept_array => false/'first'/'last'
Implies C<< type => 'scalar' >>. Similar to I<accept_scalar> but normalizes in
the other direction: when the input is an array, only the first or last item is
extracted and the other elements are ignored. If the input is an empty array,
the value is taken to be C<undef>.
=item sort => $option
Implies C<< type => 'array' >>, sort the array after validating its elements.
C<$option> determines how the array is sorted, possible values are I<str> for
string comparison, I<num> for numeric comparison, or a subroutine reference for
custom comparison function. The subroutine must be similar to the one given to
Perl's C<sort()> function, except it should compare C<$_[0]> and C<$_[1]>
instead of C<$a> and C<$b>.
=item unique => $option
Implies C<< type => 'array' >>, require elements to be unique. That is, don't
allow duplicate elements. There are several ways to specify what uniqueness
means in this context:
If C<$option> is a subroutine reference, then the subroutine is given an
element as first argument, and it should return a string that is used to check
for uniqueness. For example, if array elements are hashes, and you want to
check for uniqueness of a hash key named I<id>, you can specify this as
C<< unique => sub { $_[0]{id} } >>.
Otherwise, if C<$option> is true and the I<sort> option is set, then the
comparison function used for sorting is also used as uniqueness check. Two
elements are the same if the comparison function returns C<0>.
If C<$option> is true and I<sort> is not set, then the elements will be
interpreted as strings, similar to setting C<< unique => sub { $_[0] } >>.
All of that may sound complicated, but it's quite easy to use. Here's a few
examples:
# This describes an array of hashes with keys 'id' and 'name'.
{ elems => {
keys => {
id => { uint => 1 },
name => {}
( run in 1.138 second using v1.01-cache-2.11-cpan-39bf76dae61 )