diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/MooseX/Meta/TypeConstraint/Doctype.pm | 36 | ||||
-rw-r--r-- | lib/MooseX/Validation/Doctypes.pm | 36 | ||||
-rw-r--r-- | lib/MooseX/Validation/Doctypes/Errors.pm | 36 |
3 files changed, 99 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; |