Fixed up errors for validation; validate moar; style errors

This commit is contained in:
swag 2023-05-28 00:42:07 -04:00
parent b1583bbb95
commit 65bb0ef5a0
18 changed files with 101 additions and 44 deletions

View File

@ -58,11 +58,9 @@ Run the tests locally (against development environment):
## TODOs ## TODOs
1. Create mod takes null input?? 1. Report 404s better (thread by id and remark by id)
1. Check status for items not found, should be 404 but getting 5xx
1. "All new posts flagged" mode (require approval for new posts) 1. "All new posts flagged" mode (require approval for new posts)
1. Tests for mod-only user? 1. Tests for mod-only user?
1. Check input validation
## Crazy future ideas ## Crazy future ideas

View File

@ -7,7 +7,7 @@
--true-gray: #999999; --true-gray: #999999;
--highlight-green: green; --highlight-green: green;
--highlight-red: red; --highlight-red: red;
--transparent: #FFFFFFDD; --transparent: #FFFFFFBB;
box-sizing: border-box; box-sizing: border-box;
font-size: calc(8px + 1vmin); font-size: calc(8px + 1vmin);

View File

@ -76,6 +76,7 @@
.form-field { .form-field {
display: flex; display: flex;
flex-flow: column; flex-flow: column;
gap: 0.25em;
} }
.form-field > textarea { .form-field > textarea {
@ -97,13 +98,18 @@
background-color: var(--dark-blue); background-color: var(--dark-blue);
} }
.field-with-info { .field-with-error {
border: dashed var(--highlight-red);
padding: 0.5em 0.25em;
}
.stash-with-info {
border: dashed var(--highlight-green) 0.5em; border: dashed var(--highlight-green) 0.5em;
padding: 1em; padding: 1em;
background-color: var(--transparent); background-color: var(--transparent);
} }
.field-with-error { .stash-with-error {
border: dashed var(--highlight-red) 0.5em; border: dashed var(--highlight-red) 0.5em;
padding: 1em; padding: 1em;
background-color: var(--transparent); background-color: var(--transparent);

View File

@ -37,8 +37,8 @@ sub login($self) {
$v = $self->validation if $self->req->method eq 'POST'; $v = $self->validation if $self->req->method eq 'POST';
if ($v && $v->has_data) { if ($v && $v->has_data) {
$v->required('email' ); $v->required('email' )->size(6, 320);
$v->required('password'); $v->required('password')->size(12, undef);
if ($v->has_error) { if ($v->has_error) {
$self->stash(status => 400) $self->stash(status => 400)
@ -154,9 +154,9 @@ sub create($self) {
$v = $self->validation if $self->req->method eq 'POST'; $v = $self->validation if $self->req->method eq 'POST';
if ($v && $v->has_data) { if ($v && $v->has_data) {
$v->required('name' ); $v->required('name' )->size(1, 64);
$v->required('email' ); $v->required('email' )->size(6, 320);
$v->required('password'); $v->required('password')->size(12, undef);
if ($v->has_error) { if ($v->has_error) {
$self->stash(status => 400) $self->stash(status => 400)
@ -182,8 +182,8 @@ sub admin_reset($self) {
$v = $self->validation if $self->req->method eq 'POST'; $v = $self->validation if $self->req->method eq 'POST';
if ($v && $v->has_data) { if ($v && $v->has_data) {
$v->required('email' ); $v->required('email' )->size(6, 320);
$v->required('password'); $v->required('password')->size(12, undef);
if ($v->has_error) { if ($v->has_error) {
$self->stash(status => 400) $self->stash(status => 400)
@ -208,7 +208,7 @@ sub mod_reset($self) {
$v = $self->validation if $self->req->method eq 'POST'; $v = $self->validation if $self->req->method eq 'POST';
if ($v && $v->has_data) { if ($v && $v->has_data) {
$v->required('password'); $v->required('password')->size(12, undef);
if ($v->has_error) { if ($v->has_error) {
$self->stash(status => 400) $self->stash(status => 400)
@ -235,7 +235,7 @@ sub lock_acct($self) {
$v = $self->validation if $self->req->method eq 'POST'; $v = $self->validation if $self->req->method eq 'POST';
if ($v && $v->has_data) { if ($v && $v->has_data) {
$v->required('email'); $v->required('email')->size(6, 320);
if ($v->has_error) { if ($v->has_error) {
$self->stash(status => 400) $self->stash(status => 400)
@ -257,7 +257,7 @@ sub unlock_acct($self) {
$v = $self->validation if $self->req->method eq 'POST'; $v = $self->validation if $self->req->method eq 'POST';
if ($v && $v->has_data) { if ($v && $v->has_data) {
$v->required('email'); $v->required('email')->size(6, 320);
if ($v->has_error) { if ($v->has_error) {
$self->stash(status => 400) $self->stash(status => 400)
@ -279,7 +279,7 @@ sub promote($self) {
$v = $self->validation if $self->req->method eq 'POST'; $v = $self->validation if $self->req->method eq 'POST';
if ($v && $v->has_data) { if ($v && $v->has_data) {
$v->required('email'); $v->required('email')->size(6, 320);
if ($v->has_error) { if ($v->has_error) {
$self->stash(status => 404) $self->stash(status => 404)
@ -301,7 +301,7 @@ sub demote($self) {
$v = $self->validation if $self->req->method eq 'POST'; $v = $self->validation if $self->req->method eq 'POST';
if ($v && $v->has_data) { if ($v && $v->has_data) {
$v->required('email'); $v->required('email')->size(6, 320);
if ($v->has_error) { if ($v->has_error) {
$self->stash(status => 404) $self->stash(status => 404)

View File

@ -11,7 +11,7 @@ my %valid_login = (
my %invalid_login = ( my %invalid_login = (
email => 'fuck@example.com', email => 'fuck@example.com',
password => 'ah fuck' password => 'ah fuck goddamn'
); );
subtest Login => sub { subtest Login => sub {

View File

@ -40,7 +40,7 @@ subtest 'Post new remark', sub {
->text_like(h2 => qr/Thread #1/); ->text_like(h2 => qr/Thread #1/);
$t->post_ok('/remark/post/1', form => \%invalid_remark)->status_is(400) $t->post_ok('/remark/post/1', form => \%invalid_remark)->status_is(400)
->text_like(p => qr/Invalid text/); ->text_like(p => qr/Must be between/);
}; };
subtest 'Flagging remark', sub { subtest 'Flagging remark', sub {
@ -49,7 +49,7 @@ subtest 'Flagging remark', sub {
->text_like(h2 => qr/Remark #1/); ->text_like(h2 => qr/Remark #1/);
$t->get_ok('/remark/flag/1')->status_is(200) $t->get_ok('/remark/flag/1')->status_is(200)
->element_exists('p[class="field-with-info"]') ->element_exists('p[class="stash-with-info"]')
->text_like(p => qr/Remark #1 has been flagged/); ->text_like(p => qr/Remark #1 has been flagged/);
}; };

View File

@ -66,10 +66,10 @@ subtest 'Post new thread', sub {
->text_like(h2 => qr/New Thread/); ->text_like(h2 => qr/New Thread/);
$t->post_ok('/thread/post', form => \%invalid_title)->status_is(400) $t->post_ok('/thread/post', form => \%invalid_title)->status_is(400)
->text_like(p => qr/Invalid title/); ->text_like(p => qr/Must be between/);
$t->post_ok('/thread/post', form => \%invalid_post)->status_is(400) $t->post_ok('/thread/post', form => \%invalid_post)->status_is(400)
->text_like(p => qr/Invalid text/); ->text_like(p => qr/Must be between/);
$t->post_ok('/thread/post', form => \%valid_params)->status_is(200) $t->post_ok('/thread/post', form => \%valid_params)->status_is(200)
->text_like(h2 => qr/Thread #\d+/); ->text_like(h2 => qr/Thread #\d+/);
@ -85,7 +85,7 @@ subtest 'Bumping thread', sub {
->text_like(h2 => qr/Thread #1/); ->text_like(h2 => qr/Thread #1/);
$t->get_ok('/thread/bump/1')->status_is(200) $t->get_ok('/thread/bump/1')->status_is(200)
->element_exists('p[class="field-with-info"]') ->element_exists('p[class="stash-with-info"]')
->text_like(p => qr/Thread #1 has been bumped/); ->text_like(p => qr/Thread #1 has been bumped/);
}; };
@ -99,7 +99,7 @@ subtest 'Flagging thread', sub {
->text_like(h2 => qr/Thread #1/); ->text_like(h2 => qr/Thread #1/);
$t->get_ok('/thread/flag/1')->status_is(200) $t->get_ok('/thread/flag/1')->status_is(200)
->element_exists('p[class="field-with-info"]') ->element_exists('p[class="stash-with-info"]')
->text_like(p => qr/Thread #1 has been flagged/); ->text_like(p => qr/Thread #1 has been flagged/);
}; };

View File

@ -44,14 +44,14 @@
<% } =%> <% } =%>
</header> </header>
<% if (flash 'error') { =%> <% if (flash 'error') { =%>
<p class="field-with-error" id="error"><%= flash 'error' %></p> <p class="stash-with-error" id="error"><%= flash 'error' %></p>
<% } elsif (stash 'error') { =%> <% } elsif (stash 'error') { =%>
<p class="field-with-error" id="error"><%= stash 'error' %></p> <p class="stash-with-error" id="error"><%= stash 'error' %></p>
<% } =%> <% } =%>
<% if (flash 'info') { =%> <% if (flash 'info') { =%>
<p class="field-with-info" id="info"><%= flash 'info' %></p> <p class="stash-with-info" id="info"><%= flash 'info' %></p>
<% } elsif (stash 'info') { =%> <% } elsif (stash 'info') { =%>
<p class="field-with-info" id="info"><%= stash 'info' %></p> <p class="stash-with-info" id="info"><%= stash 'info' %></p>
<% } =%> <% } =%>
<%= content =%> <%= content =%>
<footer class="site-footer"> <footer class="site-footer">

View File

@ -3,10 +3,18 @@
<h2 class="page-title"><%= title %></h2> <h2 class="page-title"><%= title %></h2>
<form method="post" class="form-body"> <form method="post" class="form-body">
<div class="form-field"> <div class="form-field">
<% if (my $error = validation->error('email')) { =%>
<p class="field-with-error">Must be between <%= $error->[2] %>
and <%= $error->[3] %> characters.</p>
<% } =%>
<%= label_for email => 'Email' %> <%= label_for email => 'Email' %>
<%= email_field email => (id => 'email') %> <%= email_field email => (id => 'email') %>
</div> </div>
<div class="form-field"> <div class="form-field">
<% if (my $error = validation->error('password')) { =%>
<p class="field-with-error">Must be between <%= $error->[2] %>
and <%= $error->[3] %> characters.</p>
<% } =%>
<%= label_for password => 'Password' %> <%= label_for password => 'Password' %>
<%= password_field password => (id => 'password') %> <%= password_field password => (id => 'password') %>
</div> </div>

View File

@ -3,14 +3,26 @@
<h2 class="page-title"><%= title %></h2> <h2 class="page-title"><%= title %></h2>
<form method="post" class="form-body"> <form method="post" class="form-body">
<div class="form-field"> <div class="form-field">
<% if (my $error = validation->error('name')) { =%>
<p class="field-with-error">Must be between <%= $error->[2] %>
and <%= $error->[3] %> characters.</p>
<% } =%>
<%= label_for name => 'Name' %> <%= label_for name => 'Name' %>
<%= text_field name => (id => 'name') %> <%= text_field name => (id => 'name') %>
</div> </div>
<div class="form-field"> <div class="form-field">
<% if (my $error = validation->error('email')) { =%>
<p class="field-with-error">Must be between <%= $error->[2] %>
and <%= $error->[3] %> characters.</p>
<% } =%>
<%= label_for email => 'Email' %> <%= label_for email => 'Email' %>
<%= email_field email => (id => 'email') %> <%= email_field email => (id => 'email') %>
</div> </div>
<div class="form-field"> <div class="form-field">
<% if (my $error = validation->error('password')) { =%>
<p class="field-with-error">Must be between <%= $error->[2] %>
and <%= $error->[3] %> characters.</p>
<% } =%>
<%= label_for password => 'Password' %> <%= label_for password => 'Password' %>
<%= password_field password => (id => 'password') %> <%= password_field password => (id => 'password') %>
</div> </div>

View File

@ -3,6 +3,10 @@
<h2 class="page-title"><%= title %></h2> <h2 class="page-title"><%= title %></h2>
<form method="post" class="form-body"> <form method="post" class="form-body">
<div class="form-field"> <div class="form-field">
<% if (my $error = validation->error('email')) { =%>
<p class="field-with-error">Must be between <%= $error->[2] %>
and <%= $error->[3] %> characters.</p>
<% } =%>
<%= label_for email => 'Email' %> <%= label_for email => 'Email' %>
<%= email_field email => (id => 'email') %> <%= email_field email => (id => 'email') %>
</div> </div>

View File

@ -3,6 +3,10 @@
<h2 class="page-title"><%= title %></h2> <h2 class="page-title"><%= title %></h2>
<form method="post" class="form-body"> <form method="post" class="form-body">
<div class="form-field"> <div class="form-field">
<% if (my $error = validation->error('email')) { =%>
<p class="field-with-error">Must be between <%= $error->[2] %>
and <%= $error->[3] %> characters.</p>
<% } =%>
<%= label_for email => 'Email' %> <%= label_for email => 'Email' %>
<%= email_field email => (id => 'email') %> <%= email_field email => (id => 'email') %>
</div> </div>

View File

@ -3,10 +3,18 @@
<h2 class="page-title"><%= title %></h2> <h2 class="page-title"><%= title %></h2>
<form method="post" class="form-body"> <form method="post" class="form-body">
<div class="form-field"> <div class="form-field">
<% if (my $error = validation->error('email')) { =%>
<p class="field-with-error">Must be between <%= $error->[2] %>
and <%= $error->[3] %> characters.</p>
<% } =%>
<%= label_for email => 'Email' %> <%= label_for email => 'Email' %>
<%= email_field email => (id => 'email') %> <%= email_field email => (id => 'email') %>
</div> </div>
<div class="form-field"> <div class="form-field">
<% if (my $error = validation->error('password')) { =%>
<p class="field-with-error">Must be between <%= $error->[2] %>
and <%= $error->[3] %> characters.</p>
<% } =%>
<%= label_for password => 'Password' %> <%= label_for password => 'Password' %>
<%= password_field password => (id => 'password') %> <%= password_field password => (id => 'password') %>
</div> </div>

View File

@ -3,6 +3,10 @@
<h2 class="page-title"><%= title %></h2> <h2 class="page-title"><%= title %></h2>
<form method="post" class="form-body"> <form method="post" class="form-body">
<div class="form-field"> <div class="form-field">
<% if (my $error = validation->error('password')) { =%>
<p class="field-with-error">Must be between <%= $error->[2] %>
and <%= $error->[3] %> characters.</p>
<% } =%>
<%= label_for password => 'Password' %> <%= label_for password => 'Password' %>
<%= password_field password => (id => 'password') %> <%= password_field password => (id => 'password') %>
</div> </div>

View File

@ -3,6 +3,10 @@
<h2 class="page-title"><%= title %></h2> <h2 class="page-title"><%= title %></h2>
<form method="post" class="form-body"> <form method="post" class="form-body">
<div class="form-field"> <div class="form-field">
<% if (my $error = validation->error('email')) { =%>
<p class="field-with-error">Must be between <%= $error->[2] %>
and <%= $error->[3] %> characters.</p>
<% } =%>
<%= label_for email => 'Email' %> <%= label_for email => 'Email' %>
<%= email_field email => (id => 'email') %> <%= email_field email => (id => 'email') %>
</div> </div>

View File

@ -3,6 +3,10 @@
<h2 class="page-title"><%= title %></h2> <h2 class="page-title"><%= title %></h2>
<form method="post" class="form-body"> <form method="post" class="form-body">
<div class="form-field"> <div class="form-field">
<% if (my $error = validation->error('email')) { =%>
<p class="field-with-error">Must be between <%= $error->[2] %>
and <%= $error->[3] %> characters.</p>
<% } =%>
<%= label_for email => 'Email' %> <%= label_for email => 'Email' %>
<%= email_field email => (id => 'email') %> <%= email_field email => (id => 'email') %>
</div> </div>

View File

@ -3,6 +3,10 @@
<h2 class="page-title"><%= title %></h2> <h2 class="page-title"><%= title %></h2>
<form method="post" class="form-body"> <form method="post" class="form-body">
<div class="form-field"> <div class="form-field">
<% if (my $error = validation->error('author')) { =%>
<p class="field-with-error">Must be between <%= $error->[2] %>
and <%= $error->[3] %> characters.</p>
<% } =%>
<%= label_for author => 'Author' %> <%= label_for author => 'Author' %>
<%= text_field author => session->{'author'}, ( <%= text_field author => session->{'author'}, (
id => 'author', id => 'author',
@ -10,11 +14,12 @@
minlength => 1, minlength => 1,
required => undef required => undef
) %> ) %>
<% if (my $error = validation->error('author')) { =%>
<p class="field-with-error">Invalid author: 1 to 63 characters please.</p>
<% } =%>
</div> </div>
<div class="form-field"> <div class="form-field">
<% if (my $error = validation->error('body')) { =%>
<p class="field-with-error">Must be between <%= $error->[2] %>
and <%= $error->[3] %> characters.</p>
<% } =%>
<%= label_for body => 'Text' %> <%= label_for body => 'Text' %>
<%= text_area body => ( <%= text_area body => (
id => 'body', id => 'body',
@ -24,9 +29,6 @@
rows => 6, rows => 6,
autofocus => undef autofocus => undef
) %> ) %>
<% if (my $error = validation->error('body')) { =%>
<p class="field-with-error">Invalid text: Up to 4,000 characters only.</p>
<% } =%>
</div> </div>
<button type="submit" class="form-button">Post</button> <button type="submit" class="form-button">Post</button>
</form> </form>

View File

@ -3,6 +3,10 @@
<h2 class="page-title"><%= title %></h2> <h2 class="page-title"><%= title %></h2>
<form method="post" class="form-body"> <form method="post" class="form-body">
<div class="form-field"> <div class="form-field">
<% if (my $error = validation->error('author')) { =%>
<p class="field-with-error">Must be between <%= $error->[2] %>
and <%= $error->[3] %> characters.</p>
<% } =%>
<%= label_for author => 'Author' %> <%= label_for author => 'Author' %>
<%= text_field author => session->{'author'}, ( <%= text_field author => session->{'author'}, (
id => 'author', id => 'author',
@ -10,11 +14,12 @@
minlength => 1, minlength => 1,
required => undef required => undef
) %> ) %>
<% if (my $error = validation->error('author')) { =%>
<p class="field-with-error">Invalid author: 1 to 63 characters please.</p>
<% } =%>
</div> </div>
<div class="form-field"> <div class="form-field">
<% if (my $error = validation->error('title')) { =%>
<p class="field-with-error">Must be between <%= $error->[2] %>
and <%= $error->[3] %> characters.</p>
<% } =%>
<%= label_for title => 'Title' %> <%= label_for title => 'Title' %>
<%= text_field title => ( <%= text_field title => (
id => 'title', id => 'title',
@ -23,11 +28,12 @@
autofocus => undef, autofocus => undef,
required => undef required => undef
) %> ) %>
<% if (my $error = validation->error('title')) { =%>
<p class="field-with-error">Invalid title: 1 to 127 characters please.</p>
<% } =%>
</div> </div>
<div class="form-field"> <div class="form-field">
<% if (my $error = validation->error('body')) { =%>
<p class="field-with-error">Must be between <%= $error->[2] %>
and <%= $error->[3] %> characters.</p>
<% } =%>
<%= label_for body => 'Text' %> <%= label_for body => 'Text' %>
<%= text_area body => ( <%= text_area body => (
id => 'body', id => 'body',
@ -36,9 +42,6 @@
required => undef, required => undef,
rows => 6 rows => 6
) %> ) %>
<% if (my $error = validation->error('body')) { =%>
<p class="field-with-error">Invalid text: Up to 4,000 characters only.</p>
<% } =%>
</div> </div>
<button type="submit" class="form-button">Post</button> <button type="submit" class="form-button">Post</button>
</form> </form>