Help language development. Donate to The Perl Foundation

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

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

class Contact is Model {
  submethod BUILD {
    self.validate: 'fname', { length => { is => 7 } }
    self.validate: 'lname', { length => { in => 4..32 } }
  }
}

plan 9;

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

my Contact $contact;

$contact = Contact.build({fname => 'Gregory', lname => 'Donald'});
ok $contact.is-valid;

$contact = Contact.build;
ok $contact.is-invalid;
ok $contact.errors.fname[0] eq 'exactly 7 characters required';

$contact = Contact.build({});
ok $contact.is-invalid;
ok $contact.errors.fname[0] eq 'exactly 7 characters required';

$contact = Contact.build({email => '[email protected]', fname => 'x' x 8, lname => 'Donald'});
ok $contact.is-invalid;
ok $contact.errors.fname[0] eq 'exactly 7 characters required';

$contact = Contact.build({email => '[email protected]', fname => 'Gregory', lname => 'x' x 3});
ok $contact.is-invalid;
ok $contact.errors.lname[0] eq '4 to 32 characters required';

Contact.destroy-all;