Skip to content

Instantly share code, notes, and snippets.

@preaction
Created March 20, 2020 17:20
Show Gist options
  • Save preaction/0e7378caf78b61dbbe38f0194ca81214 to your computer and use it in GitHub Desktop.
Save preaction/0e7378caf78b61dbbe38f0194ca81214 to your computer and use it in GitHub Desktop.
patch to fix datetime validation of empty string
diff --git a/lib/Mojolicious/Plugin/Yancy.pm b/lib/Mojolicious/Plugin/Yancy.pm
index 7ed3e54..6ae923e 100644
--- a/lib/Mojolicious/Plugin/Yancy.pm
+++ b/lib/Mojolicious/Plugin/Yancy.pm
@@ -892,6 +892,10 @@ sub _helper_validate {
for my $prop_name ( keys %{ $schema->{properties} } ) {
my $prop = $schema->{properties}{ $prop_name };
+ # These blocks fix problems with validation only. If the
+ # problem is the database understanding the value, it must be
+ # fixed in the backend class.
+
# Pre-filter booleans
if ( is_type( $prop->{type}, 'boolean' ) && defined $item->{ $prop_name } ) {
my $value = $item->{ $prop_name };
@@ -902,6 +906,14 @@ sub _helper_validate {
}
$item->{ $prop_name } = $value;
}
+ # An empty date-time, date, or time must become undef: The empty
+ # string will never pass the format check, but properties that
+ # are allowed to be null can be validated.
+ if ( is_type( $prop->{type}, 'string' ) && $prop->{format} && $prop->{format} =~ /^(?:date-time|date|time)$/ ) {
+ if ( !$item->{ $prop_name } ) {
+ $item->{ $prop_name } = undef;
+ }
+ }
# Always add dummy passwords to pass required checks
if ( $prop->{format} && $prop->{format} eq 'password' && !$item->{ $prop_name } ) {
# Add to a new copy of the item so we don't actually change
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment