summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStevan Little <stevan.little@iinteractive.com>2012-10-10 09:34:08 -0400
committerStevan Little <stevan.little@iinteractive.com>2012-10-10 09:34:08 -0400
commit36610f8548eeabdde065fa40fd8f3dd7ffc7fc56 (patch)
treea966dced1a091b2c36ee3d1f5d3a5a1a05418e67
parenta5be6ff67d942fd419646fad12c4f39a851f4257 (diff)
downloadmoosex-validation-doctypes-36610f8548eeabdde065fa40fd8f3dd7ffc7fc56.tar.gz
moosex-validation-doctypes-36610f8548eeabdde065fa40fd8f3dd7ffc7fc56.zip
adding more complex example in the synopsis to illustrate how much more this module does, and adding a test to test that example
-rw-r--r--lib/MooseX/Meta/TypeConstraint/Doctype.pm36
-rw-r--r--lib/MooseX/Validation/Doctypes.pm36
-rw-r--r--lib/MooseX/Validation/Doctypes/Errors.pm36
-rw-r--r--t/more-basic.t117
4 files changed, 216 insertions, 9 deletions
diff --git a/lib/MooseX/Meta/TypeConstraint/Doctype.pm b/lib/MooseX/Meta/TypeConstraint/Doctype.pm
index 03b5712..f18da4c 100644
--- a/lib/MooseX/Meta/TypeConstraint/Doctype.pm
+++ b/lib/MooseX/Meta/TypeConstraint/Doctype.pm
@@ -13,15 +13,45 @@ use MooseX::Validation::Doctypes::Errors;
use MooseX::Validation::Doctypes;
+ doctype 'Location' => {
+ id => 'Str',
+ city => 'Str',
+ state => 'Str',
+ country => 'Str',
+ zipcode => 'Int',
+ };
+
doctype 'Person' => {
id => 'Str',
- name => 'Str',
- title => 'Str',
+ name => {
+ # ... nested data structures
+ first_name => 'Str',
+ last_name => 'Str',
+ },
+ title => 'Str',
+ # ... complex Moose types
+ friends => 'ArrayRef[Person]',
+ # ... using doctypes same as regular types
+ address => 'Maybe[Location]',
};
use JSON;
- my $data = decode_json('{"id": "1234-A", "name": "Bob", "title": "CIO"}');
+ # note the lack of Location,
+ # which is fine because it
+ # was Maybe[Location]
+
+ my $data = decode_json(q[
+ {
+ "id": "1234-A",
+ "name": {
+ "first_name" : "Bob",
+ "last_name" : "Smith",
+ },
+ "title": "CIO",
+ "friends" : [],
+ }
+ ]);
use Moose::Util::TypeConstraints;
diff --git a/lib/MooseX/Validation/Doctypes.pm b/lib/MooseX/Validation/Doctypes.pm
index c29e125..1c1ab2a 100644
--- a/lib/MooseX/Validation/Doctypes.pm
+++ b/lib/MooseX/Validation/Doctypes.pm
@@ -16,15 +16,45 @@ use Sub::Exporter -setup => {
use MooseX::Validation::Doctypes;
+ doctype 'Location' => {
+ id => 'Str',
+ city => 'Str',
+ state => 'Str',
+ country => 'Str',
+ zipcode => 'Int',
+ };
+
doctype 'Person' => {
id => 'Str',
- name => 'Str',
- title => 'Str',
+ name => {
+ # ... nested data structures
+ first_name => 'Str',
+ last_name => 'Str',
+ },
+ title => 'Str',
+ # ... complex Moose types
+ friends => 'ArrayRef[Person]',
+ # ... using doctypes same as regular types
+ address => 'Maybe[Location]',
};
use JSON;
- my $data = decode_json('{"id": "1234-A", "name": "Bob", "title": "CIO"}');
+ # note the lack of Location,
+ # which is fine because it
+ # was Maybe[Location]
+
+ my $data = decode_json(q[
+ {
+ "id": "1234-A",
+ "name": {
+ "first_name" : "Bob",
+ "last_name" : "Smith",
+ },
+ "title": "CIO",
+ "friends" : [],
+ }
+ ]);
use Moose::Util::TypeConstraints;
diff --git a/lib/MooseX/Validation/Doctypes/Errors.pm b/lib/MooseX/Validation/Doctypes/Errors.pm
index 7ca640e..023493c 100644
--- a/lib/MooseX/Validation/Doctypes/Errors.pm
+++ b/lib/MooseX/Validation/Doctypes/Errors.pm
@@ -6,15 +6,45 @@ use Moose;
use MooseX::Validation::Doctypes;
+ doctype 'Location' => {
+ id => 'Str',
+ city => 'Str',
+ state => 'Str',
+ country => 'Str',
+ zipcode => 'Int',
+ };
+
doctype 'Person' => {
id => 'Str',
- name => 'Str',
- title => 'Str',
+ name => {
+ # ... nested data structures
+ first_name => 'Str',
+ last_name => 'Str',
+ },
+ title => 'Str',
+ # ... complex Moose types
+ friends => 'ArrayRef[Person]',
+ # ... using doctypes same as regular types
+ address => 'Maybe[Location]',
};
use JSON;
- my $data = decode_json('{"id": "1234-A", "name": "Bob", "title": "CIO"}');
+ # note the lack of Location,
+ # which is fine because it
+ # was Maybe[Location]
+
+ my $data = decode_json(q[
+ {
+ "id": "1234-A",
+ "name": {
+ "first_name" : "Bob",
+ "last_name" : "Smith",
+ },
+ "title": "CIO",
+ "friends" : [],
+ }
+ ]);
use Moose::Util::TypeConstraints;
diff --git a/t/more-basic.t b/t/more-basic.t
new file mode 100644
index 0000000..ceb904a
--- /dev/null
+++ b/t/more-basic.t
@@ -0,0 +1,117 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use Test::More;
+
+use Moose::Util::TypeConstraints 'find_type_constraint';
+
+use MooseX::Validation::Doctypes;
+
+doctype 'Location' => {
+ id => 'Str',
+ city => 'Str',
+ state => 'Str',
+ country => 'Str',
+ zipcode => 'Int',
+};
+
+doctype 'Person' => {
+ id => 'Str',
+ name => {
+ first_name => 'Str',
+ last_name => 'Str',
+ },
+ title => 'Str',
+ friends => 'ArrayRef[Person]',
+ address => 'Maybe[Location]'
+};
+
+
+{
+ my $person = find_type_constraint('Person');
+ isa_ok($person, 'Moose::Meta::TypeConstraint');
+ isa_ok($person, 'MooseX::Meta::TypeConstraint::Doctype');
+
+ is_deeply(
+ $person->doctype,
+ {
+ id => 'Str',
+ name => {
+ first_name => 'Str',
+ last_name => 'Str',
+ },
+ title => 'Str',
+ friends => 'ArrayRef[Person]',
+ address => 'Maybe[Location]'
+ },
+ "got the right doctype"
+ );
+
+ {
+ my $errors = $person->validate({
+ id => '17382-QA',
+ name => {
+ first_name => 'Bob',
+ last_name => 'Smith',
+ },
+ title => 'CIO',
+ friends => [],
+ address => {
+ id => 'My House',
+ city => 'Anytown',
+ state => 'IL',
+ country => 'USA',
+ zipcode => '12345'
+ }
+ });
+
+ is($errors, undef, "no errors");
+ }
+
+ {
+ my $errors = $person->validate({
+ id => '17382-QA',
+ name => {
+ first_name => 'Bob',
+ last_name => 'Smith',
+ },
+ title => 'CIO',
+ friends => [],
+ favorite_food => 'ice cream',
+ });
+ isa_ok($errors, 'MooseX::Validation::Doctypes::Errors');
+ is_deeply(
+ $errors->extra_data,
+ { favorite_food => 'ice cream' },
+ "got the right extra data"
+ );
+ is($errors->errors, undef, "no errors");
+ }
+
+ {
+ my $errors = $person->validate({
+ id => '17382-QA',
+ name => {
+ first_name => 'Bob',
+ last_name => 'Smith',
+ },
+ friends => [],
+ address => {
+ id => 'My House',
+ city => 'Anytown',
+ state => 'IL',
+ country => 'USA',
+ zipcode => '12345'
+ }
+ });
+ isa_ok($errors, 'MooseX::Validation::Doctypes::Errors');
+ is($errors->extra_data, undef, "no extra data");
+ is_deeply(
+ $errors->errors,
+ { title => "invalid value undef for 'title'" },
+ "got the right errors"
+ );
+ }
+}
+
+done_testing;