| 1 | # ABSTRACT: Member names validation utility | ||
| 2 | package PONAPI::Names; | ||
| 3 | |||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | use parent qw< Exporter >; | ||
| 8 | our @EXPORT_OK = qw< check_name >; | ||
| 9 | |||
| 10 | my $qr_edge = qr/[a-zA-Z0-9\P{ASCII}]/; | ||
| 11 | my $qr_mid = qr/[a-zA-Z0-9\P{ASCII}_\ -]/; | ||
| 12 | |||
| 13 | sub check_name { | ||
| 14 | my $name = shift; | ||
| 15 | |||
| 16 | return if ref($name); | ||
| 17 | return if length($name) == 0; | ||
| 18 | |||
| 19 | return $name =~ /\A $qr_edge \z/x if length($name) == 1; | ||
| 20 | return $name =~ /\A $qr_edge $qr_edge \z/x if length($name) == 2; | ||
| 21 | return $name =~ /\A $qr_edge $qr_mid+ $qr_edge \z/x; | ||
| 22 | } | ||
| 23 | |||
| 24 | 1; | ||
| 25 | |||
| 26 | __END__ | ||
| 27 | |||
| 28 | =pod | ||
| 29 | |||
| 30 | =encoding UTF-8 | ||
| 31 | |||
| 32 | =head1 NAME | ||
| 33 | |||
| 34 | PONAPI::Names - Member names validation utility | ||
| 35 | |||
| 36 | =head1 VERSION | ||
| 37 | |||
| 38 | version 0.001001 | ||
| 39 | |||
| 40 | =head1 SYNOPSIS | ||
| 41 | |||
| 42 | use PONAPI::Names 'check_name'; | ||
| 43 | |||
| 44 | check_name('a'); # Valid | ||
| 45 | check_name('a-'); # Invalid | ||
| 46 | check_name('-a'); # Invalid | ||
| 47 | check_name('a-b'); # Valid | ||
| 48 | check_name('a b'); # Valid | ||
| 49 | |||
| 50 | =head1 DESCRIPTION | ||
| 51 | |||
| 52 | This module implements the L<member name restrictions|http://jsonapi.org/format/#document-member-names> | ||
| 53 | from the {json:api} specification; it can be used by repositories | ||
| 54 | to implement strict member names, if desired. | ||
| 55 | |||
| 56 | =head1 AUTHORS | ||
| 57 | |||
| 58 | =over 4 | ||
| 59 | |||
| 60 | =item * | ||
| 61 | |||
| 62 | Mickey Nasriachi <mickey@cpan.org> | ||
| 63 | |||
| 64 | =item * | ||
| 65 | |||
| 66 | Stevan Little <stevan@cpan.org> | ||
| 67 | |||
| 68 | =item * | ||
| 69 | |||
| 70 | Brian Fraser <hugmeir@cpan.org> | ||
| 71 | |||
| 72 | =back | ||
| 73 | |||
| 74 | =head1 COPYRIGHT AND LICENSE | ||
| 75 | |||
| 76 | This software is copyright (c) 2016 by Mickey Nasriachi, Stevan Little, Brian Fraser. | ||
| 77 | |||
| 78 | This is free software; you can redistribute it and/or modify it under | ||
| 79 | the same terms as the Perl 5 programming language system itself. | ||
| 80 | |||
| 81 | =cut |