| 1 | package OMOP::CSV::Validator; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use utf8; | ||
| 6 | use JSON::XS; | ||
| 7 | use JSON::Validator; | ||
| 8 | use Text::CSV_XS; | ||
| 9 | use Scalar::Util qw(looks_like_number); | ||
| 10 | use Path::Tiny; | ||
| 11 | use Data::Dumper; | ||
| 12 | |||
| 13 | our $VERSION = '0.03'; | ||
| 14 | |||
| 15 | =head1 NAME | ||
| 16 | |||
| 17 | OMOP::CSV::Validator - Validates OMOP CDM CSV files against their expected data types | ||
| 18 | |||
| 19 | =head1 SYNOPSIS | ||
| 20 | |||
| 21 | use OMOP::CSV::Validator; | ||
| 22 | |||
| 23 | my $validator = OMOP::CSV::Validator->new(); | ||
| 24 | |||
| 25 | # Load schemas from DDL | ||
| 26 | my $schemas = $validator->load_schemas_from_ddl($ddl_text); | ||
| 27 | |||
| 28 | # Retrieve specific table schema for a CSV file | ||
| 29 | my $schema = $validator->get_schema_from_csv_filename($csv_file, $schemas); | ||
| 30 | |||
| 31 | # Validate CSV file | ||
| 32 | my $errors = $validator->validate_csv_file($csv_file, $schema); | ||
| 33 | if (@$errors) { | ||
| 34 | print "Validation errors found:\n"; | ||
| 35 | for my $err_info (@$errors) { | ||
| 36 | print "Row $err_info->{row}:\n"; | ||
| 37 | for my $e (@{ $err_info->{errors} }) { | ||
| 38 | print " $e\n"; | ||
| 39 | } | ||
| 40 | } | ||
| 41 | } else { | ||
| 42 | print "CSV is valid.\n"; | ||
| 43 | } | ||
| 44 | |||
| 45 | =head1 DESCRIPTION | ||
| 46 | |||
| 47 | OMOP::CSV::Validator is a CLI tool and Perl module designed to validate OMOP Common Data Model (CDM) CSV files. It auto-generates JSON schemas from PostgreSQL DDL files and then validates CSV rows against those schemas. | ||
| 48 | |||
| 49 | =head1 METHODS | ||
| 50 | |||
| 51 | =cut | ||
| 52 | |||
| 53 | ########################################################################## | ||
| 54 | # Constructor: new() | ||
| 55 | ########################################################################## | ||
| 56 | sub new { | ||
| 57 | my ( $class, %args ) = @_; | ||
| 58 | my $self = bless {}, $class; | ||
| 59 | return $self; | ||
| 60 | } | ||
| 61 | |||
| 62 | ########################################################################## | ||
| 63 | # load_schemas_from_ddl($ddl_text) | ||
| 64 | # | ||
| 65 | # Parses all CREATE TABLE definitions from a PostgreSQL OMOP DDL | ||
| 66 | # and returns a hashref of JSON schemas keyed by table name (lowercase). | ||
| 67 | ########################################################################## | ||
| 68 | sub load_schemas_from_ddl { | ||
| 69 | my ( $self, $ddl_text ) = @_; | ||
| 70 | return $self->_ddl_to_json_schemas($ddl_text); | ||
| 71 | } | ||
| 72 | |||
| 73 | ########################################################################## | ||
| 74 | # _ddl_to_json_schemas($ddl_text) - private | ||
| 75 | # | ||
| 76 | # Internal subroutine that iterates over all CREATE TABLE blocks. | ||
| 77 | ########################################################################## | ||
| 78 | sub _ddl_to_json_schemas { | ||
| 79 | my ( $self, $ddl_text ) = @_; | ||
| 80 | my %schemas; | ||
| 81 | while ( | ||
| 82 | $ddl_text =~ / | ||
| 83 | CREATE\s+TABLE\s+\S+\.(\w+)\s*\( # capture table name (after schema qualifier) | ||
| 84 | (.*?) # capture everything inside parentheses | ||
| 85 | \)\s*; # until the closing parenthesis and semicolon | ||
| 86 | /gisx | ||
| 87 | ) | ||
| 88 | { | ||
| 89 | my ( $table, $cols_block ) = ( lc $1, $2 ); | ||
| 90 | $schemas{$table} = $self->_build_schema( $table, $cols_block ); | ||
| 91 | } | ||
| 92 | return \%schemas; | ||
| 93 | } | ||
| 94 | |||
| 95 | ########################################################################## | ||
| 96 | # _build_schema($table_name, $cols_block) - private | ||
| 97 | # | ||
| 98 | # Builds a JSON schema for one table from the column definitions. | ||
| 99 | ########################################################################## | ||
| 100 | sub _build_schema { | ||
| 101 | my ( $self, $table_name, $cols_block ) = @_; | ||
| 102 | my $schema = { | ||
| 103 | '$schema' => 'http://json-schema.org/draft-07/schema#', | ||
| 104 | title => $table_name, | ||
| 105 | type => 'object', | ||
| 106 | properties => {}, | ||
| 107 | required => [], | ||
| 108 | additionalProperties => 0, | ||
| 109 | }; | ||
| 110 | |||
| 111 | for my $line ( grep /\S/, split /\n/, $cols_block ) { | ||
| 112 | $line =~ s/^\s+|\s+$//g; | ||
| 113 | $line =~ s/,$//; | ||
| 114 | next if $line =~ /^--/; # Skip comment lines | ||
| 115 | |||
| 116 | if ( $line =~ | ||
| 117 | /^(\w+)\s+([A-Za-z]+)(?:\((\d+(?:,\d+)?)\))?(?:\s+(NOT NULL))?/i ) | ||
| 118 | { | ||
| 119 | my ( $col, $type, $length, $notnull ) = | ||
| 120 | ( lc $1, lc $2, $3, defined $4 ); | ||
| 121 | my $prop = {}; | ||
| 122 | |||
| 123 | if ( $type =~ /int/ ) { | ||
| 124 | $prop->{type} = 'integer'; | ||
| 125 | $prop->{_coerce} = 1; | ||
| 126 | } | ||
| 127 | elsif ( $type =~ /numeric|real|double/ ) { | ||
| 128 | $prop->{type} = 'number'; | ||
| 129 | $prop->{_coerce} = 1; | ||
| 130 | |||
| 131 | } | ||
| 132 | elsif ( $type eq 'date' ) { | ||
| 133 | $prop->{type} = 'string'; | ||
| 134 | $prop->{format} = 'date'; | ||
| 135 | } | ||
| 136 | elsif ( $type =~ /timestamp/ ) { | ||
| 137 | $prop->{type} = 'string'; | ||
| 138 | $prop->{format} = 'date-time'; | ||
| 139 | } | ||
| 140 | elsif ( $type eq 'varchar' ) { | ||
| 141 | $prop->{type} = 'string'; | ||
| 142 | if ( defined $length ) { | ||
| 143 | |||
| 144 | # Capture only the first number if a comma is present (e.g., varchar(10,2)) | ||
| 145 | if ( $length =~ /^(\d+)/ ) { | ||
| 146 | $prop->{maxLength} = int($1); | ||
| 147 | } | ||
| 148 | } | ||
| 149 | } | ||
| 150 | else { | ||
| 151 | $prop->{type} = 'string'; | ||
| 152 | } | ||
| 153 | |||
| 154 | # If the column is not marked as NOT NULL, allow null values | ||
| 155 | unless ($notnull) { | ||
| 156 | $prop->{type} = [ $prop->{type}, 'null' ]; | ||
| 157 | } | ||
| 158 | |||
| 159 | $schema->{properties}{$col} = $prop; | ||
| 160 | push @{ $schema->{required} }, $col if $notnull; | ||
| 161 | } | ||
| 162 | } | ||
| 163 | return $schema; | ||
| 164 | } | ||
| 165 | |||
| 166 | ########################################################################## | ||
| 167 | # get_schema_from_csv_filename($csv_filename, $schemas) | ||
| 168 | # | ||
| 169 | # Derives the table name from the CSV file's basename (e.g. PERSON.csv → person) | ||
| 170 | # and returns the corresponding schema from the provided hashref. | ||
| 171 | ########################################################################## | ||
| 172 | sub get_schema_from_csv_filename { | ||
| 173 | my ( $self, $csv_filename, $schemas ) = @_; | ||
| 174 | ( my $table = lc $csv_filename ) =~ s{^.*/}{}; # remove any path | ||
| 175 | $table =~ s/\.csv$//i; # remove .csv extension | ||
| 176 | return $schemas->{$table}; | ||
| 177 | } | ||
| 178 | |||
| 179 | ########################################################################## | ||
| 180 | # validate_csv_file($csv_file, $schema, $sep) | ||
| 181 | # | ||
| 182 | # Reads the CSV file, coerces numeric fields, and validates each row against | ||
| 183 | # the provided JSON schema. Returns an arrayref of error info (each entry is a | ||
| 184 | # hashref with keys 'row' and 'errors'). | ||
| 185 | ########################################################################## | ||
| 186 | sub validate_csv_file { | ||
| 187 | my ( $self, $csv_file, $schema, $sep ) = @_; | ||
| 188 | $sep //= ','; | ||
| 189 | |||
| 190 | my $csv_handle = path($csv_file)->openr_utf8; | ||
| 191 | my $csv = | ||
| 192 | Text::CSV_XS->new( | ||
| 193 | { binary => 1, sep_char => $sep, auto_diag => 1, blank_is_undef => 1 } ) | ||
| 194 | or die "Cannot use CSV: " . Text::CSV_XS->error_diag(); | ||
| 195 | |||
| 196 | my $header = $csv->getline($csv_handle); | ||
| 197 | $csv->column_names(@$header); | ||
| 198 | |||
| 199 | my $records = $csv->getline_hr_all($csv_handle); | ||
| 200 | $csv_handle->close; | ||
| 201 | |||
| 202 | my @errors; | ||
| 203 | my $validator = JSON::Validator->new; | ||
| 204 | $validator->schema($schema); | ||
| 205 | |||
| 206 | for my $i ( 0 .. $#$records ) { | ||
| 207 | my $record = $records->[$i]; | ||
| 208 | |||
| 209 | # Coerce numeric fields according to the schema. | ||
| 210 | for my $col ( keys %{ $schema->{properties} } ) { | ||
| 211 | if ( exists $record->{$col} ) { | ||
| 212 | my $prop = $schema->{properties}->{$col}; | ||
| 213 | if ( defined $prop->{_coerce} && $prop->{_coerce} ) { | ||
| 214 | $record->{$col} = | ||
| 215 | $self->dotify_and_coerce_number( $record->{$col} ); | ||
| 216 | } | ||
| 217 | } | ||
| 218 | } | ||
| 219 | |||
| 220 | # Validate | ||
| 221 | my $errs = [ $validator->validate($record) ]; | ||
| 222 | if (@$errs) { | ||
| 223 | |||
| 224 | # row number excludes header → row index + 1 | ||
| 225 | push @errors, { row => $i + 1, errors => $errs }; | ||
| 226 | } | ||
| 227 | } | ||
| 228 | return \@errors; | ||
| 229 | } | ||
| 230 | |||
| 231 | ########################################################################## | ||
| 232 | # dotify_and_coerce_number($val) | ||
| 233 | # | ||
| 234 | # Converts a CSV string value to a number if it looks numeric. | ||
| 235 | # Returns undef if the value is empty or "\N". | ||
| 236 | ########################################################################## | ||
| 237 | sub dotify_and_coerce_number { | ||
| 238 | my ( $self, $val ) = @_; | ||
| 239 | return undef unless ( defined $val && $val ne '' && $val ne '\\N' ); | ||
| 240 | ( my $tr_val = $val ) =~ tr/,/./; | ||
| 241 | return looks_like_number($tr_val) ? 0 + $tr_val : $val; | ||
| 242 | } | ||
| 243 | |||
| 244 | =head1 AUTHOR | ||
| 245 | |||
| 246 | Written by Manuel Rueda, PhD. Info about CNAG can be found at L<https://www.cnag.eu>. | ||
| 247 | |||
| 248 | =head1 LICENSE | ||
| 249 | |||
| 250 | This module is free software; you may redistribute it and/or modify it under the same terms as Perl itself. | ||
| 251 | |||
| 252 | =cut | ||
| 253 | |||
| 254 | 1; |