=encoding utf-8 =head1 NAME =begin original Data::Validator - Rule based validator on type constraint system =end original Data::Validator - ルールに基づいた型制約システム =head1 VERSION =begin original This document describes Data::Validator version 0.06. =end original このドキュメントは Data::Validatorバージョン 0.06について述べています. =head1 SYNOPSIS =begin original use 5.10.0; use Data::Validator; # for functions sub get { state $rule = Data::Validator->new( uri => { isa => 'Str', xor => [qw(schema host path_query)] }, schema => { isa => 'Str', default => 'http' }, host => { isa => 'Str' }, path_query => { isa => 'Str', default => '/' }, method => { isa => 'Str', default => 'GET' }, ); my $args = $rule->validate(@_); # ... } get( uri => 'http://example.com/' ); # for methods sub method { state $rule = Data::Validator->new( foo => 'Str', )->with('Method'); my($self, $args) = $rule->validate(@_); # ... } Foo->method( foo => 'bar' ); # using sequenced parameters sub seq { state $rule = Data::Validator->new( foo => 'Str', )->with('Sequenced'); my $args = $rule->validate(@_); # ... } seq( 'bar' ); # seq() will get { foo => 'bar' } seq({ foo => 'bar' }); # named style are available! # using Method and Sequenced together sub seq_method { state $rule = Data::Validator->new( foo => 'Str', )->with( 'Method', 'Sequenced'); my($self, $args) = $rule->validate(@_); # ... } Foo->seq_method( 'bar' ); # seq() will get { foo => 'bar' } =end original use 5.10.0; use Data::Validator; # 関数の場合 sub get { state $rule = Data::Validator->new( uri => { isa => 'Str', xor => [qw(schema host path_query)] }, schema => { isa => 'Str', default => 'http' }, host => { isa => 'Str' }, path_query => { isa => 'Str', default => '/' }, method => { isa => 'Str', default => 'GET' }, ); my $args = $rule->validate(@_); # ... } get( uri => 'http://example.com/' ); # メソッドの場合 sub method { state $rule = Data::Validator->new( foo => 'Str', )->with('Method'); my($self, $args) = $rule->validate(@_); # ... } Foo->method( foo => 'bar' ); # 引数をリストを使って渡す場合 sub seq { state $rule = Data::Validator->new( foo => 'Str', )->with('Sequenced'); my $args = $rule->validate(@_); # ... } seq( 'bar' ); # seq()は { foo => 'bar' }として受け取ることになります seq({ foo => 'bar' }); # 名前付きの方式も利用できます! # メソッドとリスト渡しを同時に使用する場合 sub seq_method { state $rule = Data::Validator->new( foo => 'Str', )->with( 'Method', 'Sequenced'); my($self, $args) = $rule->validate(@_); # ... } Foo->seq_method( 'bar' ); # seq() will get { foo => 'bar' } =head1 DESCRIPTION =begin original This is yet another validation library, based on C but less smart. This is under development. B<< Any API will change without notice >>. =head2 Concepts =over =item Natural as Perl code I love C because it is really stylish, but it does not seem Perl-ish. Thus, I have designed C in more Perl-ish way with full of C functionality. =item Basics on type constraint system Moose's type constraint system is awesome, and so is Mouse's. In fact, Mouse's type constraints are much faster than Moose's so that you need not hesitate to check types. Thus, I have made C on Mouse's type constraint system. =item Pure Perl Although I do not hesitate to depend on XS modules, some people think that XS modules are hard to install. Thus, I have written C in pure Perl and selected dependent modules which work in pure Perl. =item Performance Validators should be as fast as possible because they matter only for illegal inputs. Otherwise, one would want something like I option. This is much faster than C, which has an XS backend, though. =back =end original このモジュールは Cに基づくバリデーションライブラリです. ただし少しばかり機能が劣っています. このモジュールは開発段階です. B<> =head2 Concepts =over =item Perlらしいコード 私は洗練された Cが大好きですが, Perlらしくないと思います. そこで私は Cの全機能を, より Perlらしく使える Cを設計しました. =item 型制約システムの基本 Mooseの型制約システムは見事なものです. Mouseも同様です. 実のところ Mouseの型制約システムは Mooseのものより高速であるため, 型チェックを 行うことを躊躇する必要はありません. そこで Cは Mouse'sの型制約システムが利用するようにしました. =item Pure Perl 私は XSモジュールの依存についてを遠慮しませんが, XSモジュールのインストールが 困難と考える人もいるでしょう. そこで Cを Pure Perlとしてコーディングし, Pure Purlとして 動作する場合の依存モジュールを選び出しました. =item 性能 バリデータは不正な入力を検出するだけなので, 可能な限り高速で高速であるべきです. そうでなければ, Iオプションのようなものが欲しくなってしまうことでしょう. このモジュールは XSバックエンドを持つ Cよりも高速です. =back =head1 INTERFACE =begin original =head2 C<< Data::Validator->new( $arg_name => $rule [, ...]) :Validator >> Creates a validation rule. You should cache the rules for performance. Attributes for I<$rule> are as follows: =over =item C<< isa => $type : Str|Object >> The type of the rule, which can be a Mouse type constraint name, a class name, or a type constraint object of either Mouse or Moose (i.e. it's duck-typed). =item C<< does => $role : Str|Object >> The type of the rule, which can be a Mouse type constraint name, a role name, or a type constraint object of either Mouse or Moose (i.e. it's duck-typed). Note that you cannot use it with the C attribute. =item C<< coerce => $should_coercion : Bool >> If false, the rule does not try to coerce when the validation fails. Default to true. =item C<< default=> $value : Any | CodeRef >> The default value for the argument. If it is a CODE reference, it is called in scalar context as C<< $default->($validator, $rule, $args) >> and its return value is used as a default value. Because arguments are validated in the order of definitions, C callbacks can rely on the previously-filled values: my $v = Data::Validator->new( foo => { default => 99 }, bar => { default => sub { my($validator, $this_rule, $args) = @_; return $args->{foo} + 1; } }, ); $v->validate(); # bar is 100 $v->validate(foo => 42); # bar is 43 Unlike Moose/Mouse's C, any references are allowed, but note that they are statically allocated. =item C<< optional => $value : Bool >> If true, users can omit the argument. Default to false. =item C<< xor => $exclusives : ArrayRef >> Exclusive arguments, which users cannot pass together. =item C<< documentation => $doc : Str >> Descriptions of the argument. This is not yet used anywhere. =back =head2 C<< $validator->find_rule($name :Str) >> Finds the rule named I<$name>. Provided for error handling. =head2 C<< $validator->with(@extentions) :Validator >> Applies I<@extentions> to I<$validator> and returns itself. See L for details. =head2 C<< $validator->validate(@args) :HashRef >> Validates I<@args> and returns a restricted HASH reference. Restricted hashes are hashes which do not allow to access non-existing keys, so you must check a key C in the hash before fetching its values. =end original =head2 C<< Data::Validator->new( $arg_name => $rule [, ...]) :Validator >> バリデーションルールの生成. パフォーマンスを気にするのであればルールはキャッシュすべきです. I<$rule>の属性は以下に示すとおりです. =over =item C<< isa => $type : Str|Object >> ルールの型. Mouseの型制約名, クラス名, Mouseか Mooseいずれかの型制約オブジェクト (すなわちダックタイピング)を指定できます. =item C<< does => $role : Str|Object >> ルールの型. Mouseの型制約名, ロール名, Mouseか Mooseいずれかの型制約オブジェクト (すなわちダックタイピング)を指定できます. 注意 C属性と同時に使用することはできません. =item C<< coerce => $should_coercion : Bool >> この属性に偽を指定した場合, バリデーションが失敗した場合に型変換を行いません. デフォルトは真です. =item C<< default=> $value : Any | CodeRef >> 引数のデフォルト値. コードリファレンスを指定した場合, C<< $default->($validator, $rule, $args) >>を スカラコンテキストで呼び出し, その戻り値をデフォルト値として使用します. なぜなら引数は定義された順番でバリデートされるので, Cコールバックは 前で定義される値に依存します. my $v = Data::Validator->new( foo => { default => 99 }, bar => { default => sub { my($validator, $this_rule, $args) = @_; return $args->{foo} + 1; } }, ); $v->validate(); # bar is 100 $v->validate(foo => 42); # bar is 43 任意のリファレンスを許可する Moose/Mouseの Cと異なります. それらは静的に割り当てられます. =item C<< optional => $value : Bool >> 真が設定された場合, ユーザはその引数を省略することができます. デフォルトは偽です. =item C<< xor => $exclusives : ArrayRef >> 同時に引き渡すことのできない, 排他的な引数を指定します. =item C<< documentation => $doc : Str >> 引数の説明. どこでも使われていません. =back =head2 C<< $validator->find_rule($name :Str) >> I<$name>と名付けられたルールを探します. エラー処理のために提供されます. =head2 C<< $validator->with(@extentions) :Validator >> I<@extentions>を I<$validator>に適用し, 自分自身を返します. Lの詳細を参照してください. =head2 C<< $validator->validate(@args) :HashRef >> I<@args>をバリデートし, 制限されたハッシュリファレンスを返します. 制限されたハッシュは存在していないキーにアクセスすることが許可されていないので, 値を使う前に必ず Cでキーの存在を確認する必要があります. =head1 EXTENTIONS =begin original There are extentions which changes behaviours of C. =head2 Method Takes the first argument as an invocant (i.e. class or object instance), and returns it as the first value: my($invocant, $args) = $rule->validate(@_); =head2 Sequenced Deals with arguments in sequenced style, where users should pass arguments by the order of argument rules, instead of by-name. Note that if the last argument is a HASH reference, it is regarded as named-style arguments. =head2 AllowExtra Regards unknown arguments as extra arguments, and returns them as a list of name-value pairs: my($args, %extra) = $rule->validate(@_); =head2 NoThrow Does not throw errors. Instead, it provides validators with the C attribute: my $args = $v->validate(@_); # it never throws errors if($v->has_errors) { my $errors = $v->clear_errors; foreach my $e(@{$errors}) { # $e has 'type', 'message' and 'name' print $e->{message}, "\n"; } } =head2 Croak Does not report stack backtraces on errors, i.e. uses C instead of C to throw errors. =end original これらの拡張は Cの挙動を変更します. =head2 Method 第一引数にインボカントを渡し(すなわちクラスかインスタンス), 第一要素としてそれを返します. my($invocant, $args) = $rule->validate(@_); =head2 Sequenced ユーザに名前ではなく順序づいた引数を渡して欲しい場合に, 引数をリスト渡しスタイルとして扱います. 注記 最後の引数がハッシュリファレンスであれば, それは名前付き引数と して扱います. =head2 AllowExtra 不明な引数を別の引数として扱い, 名前と値のペアのリストとしてそれらを 返します. my($args, %extra) = $rule->validate(@_); =head2 NoThrow 例外を報告しません. 代わりに, バリデータに C属性を提供しています. my $args = $v->validate(@_); # エラーが通知されることはありません if($v->has_errors) { my $errors = $v->clear_errors; foreach my $e(@{$errors}) { # $e has 'type', 'message' and 'name' print $e->{message}, "\n"; } } =head2 Croak 例外発生時 Cの代わりに Cを使い, エラー時にスタックのバックトレースをレポートしません. =head1 DEPENDENCIES Perl 5.8.1 or later. =head1 BUGS =begin original All complex software has bugs lurking in it, and this module is no exception. If you find a bug please either email me, or add the bug to cpan-RT. =end original 複雑なソフトウェアにはバグが潜んでおり, このモジュールも例外ではありません. バグを見つけた場合は, メールをしていただくか, CPAN-RTにバグを追加してください. =head1 SEE ALSO L L L L =head1 AUTHOR Fuji, Goro (gfx) Egfuji@cpan.orgE =head1 LICENSE AND COPYRIGHT Copyright (c) 2010, Fuji Goro (gfx). All rights reserved. This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =cut