Text::Xslate::Bridge::TypeDeclaration - A Type::Tiny based Type Validator in Xslate.
my $xslate = Text::Xslate->new( module => [ 'Text::Xslate::Bridge::TypeDeclaration' ], ); # @@ template.tx # <:- declare(drink => 'Enum["Cocoa", "Cappuchino", "Tea"]') -:> # May I have a cup of <: $drink :>. # Success! $xslate->render('template.tx', { drink => 'Cocoa' }); # Output: # May I have a cup of Cocoa. # A string 'Oil' is not a drink is $xslate->render('template.tx', { drink => 'Oil' }); # Output: # <pre class="type-declaration-mismatch"> # Declaration mismatch for `drink` # Value "Oil" did not pass type constraint "Enum["Cocoa", "Cappuchino", "Tea"]" # </pre> # May I have a cup of Oil.
Text::Xslate::Bridge::TypeDeclaration is a type validator module in Text::Xslate templates.
The type validation of this module is base on Type::Tiny.
Type::Tiny type constraints are compatible with Moo, Moose and Mouse.
declare interface was implemented with reference to Smart::Args.
declare
See Types::Standard.
These are imported by default Text::Xslate::Bridge::TypeDeclaration::Registry.
You can not use them unless you import Types::Standard with specifying registry by registry_class option.
registry_class
declare(name => 'Str')
declare(user_ids => 'ArrayRef[Int]')
declare(person_hash => 'Dict[name => Str, age => Int]')
These are defined by default Text::Xslate::Bridge::TypeDeclaration::Registry when a name is not found in registry.
declare(engine => 'Text::Xslate')
declare(visitor => 'Maybe[My::Model::UserAccount]')
Hashref is treated as Dict[... slurpy Any].
Dict[... slurpy Any]
This is a slurpy match. Less value is error. Extra values are ignored.
declare(account_summary => { name => 'Str', subscriber_count => 'Int', icon => 'My::Image' })
declare(sidebar => { profile => { name => 'Str', followers => 'Int' }, recent_entries => 'ArrayRef[My::Entry]' })
Text::Xslate->new( module => [ 'Text::Xslate::Bridge::TypeDeclaration' => [ # defaults method => 'declare', # method name to export validate => 1, # enable validation when truthy print => 1, # enable printing errors to the output buffer when truthy on_mismatch => 'die', # error handler ('die', 'warn' or 'none') registry_class => undef, # package name for specifying Type::Registry ] ] );
Perhaps you want to disable validation in production to prevent spoiling performance on a Plack application.
Text::Xslate->new( module => [ 'Text::Xslate::Bridge::TypeDeclaration' => [ validate => $ENV{PLACK_ENV} ne 'production', ], ], );
type-declaration-mismatch
Highlight by css
.type-declaration-mismatch { color: crimson; }
Lint with Test::WWW::Mechanize
# in subclass of Test::WWW::Mechanize sub _lint_content_ok { my ($self, $desc) = @_; if (my $mismatch = $self->scrape_text_by_attr('class', 'type-declaration-mismatch')) { $Test::Builder::Test->ok(0, $mismatch); }; return $self->SUPER::_lint_content_ok($desc); }
Text::Xslate, Text::Xslate::Bridge
Type::Tiny, Types::Standard, Type::Registry
Smart::Args, Test::WWW::Mechanize
Copyright (C) pokutuna.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
pokutuna <popopopopokutuna@gmail.com>
To install Text::Xslate::Bridge::TypeDeclaration, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Text::Xslate::Bridge::TypeDeclaration
CPAN shell
perl -MCPAN -e shell install Text::Xslate::Bridge::TypeDeclaration
For more information on module installation, please visit the detailed CPAN module installation guide.