Help language development. Donate to The Perl Foundation

ORM::ActiveRecord cpan:GDONALD last updated on 2019-10-24

t/002-validate-format.t6
use v6.d;
use lib 'lib';
use ORM::ActiveRecord::Model;
use Test;

plan 7;

class Contact is Model {
  submethod BUILD {
    self.validate: 'email', { :presence, format => { with => /:i ^<[\w]>+ '@' <[\w]>+ '.' <[\w]>+$/ } }
  }
}

%*ENV<DISABLE-SQL-LOG> = True;

my @errors;
my Contact $contact;

$contact = Contact.create({});
ok $contact.is-invalid;
@errors = ['must be present', 'is invalid'];
ok @errors.grep: $contact.errors.email[0];
ok @errors.grep: $contact.errors.email[1];

$contact = Contact.create({email => 'foo'});
ok $contact.is-invalid;
ok $contact.errors.email[0] eq 'is invalid';

$contact = Contact.create({email => '[email protected]', fname => 'Gregory', lname => 'Donald'});
ok $contact.is-valid;
nok $contact.errors.email;

Contact.destroy-all;