summaryrefslogtreecommitdiff
path: root/pinyweb/cgi-bin/checkconstraint.cgi
blob: 8a3656976fb92b0ebb5fb70bebb0e2dae94e6606 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/perl

use strict;
use warnings;

use CGI;
use JSON qw( encode_json );
use Moose::Util::TypeConstraints qw( find_type_constraint );

use Piny;

my $q = CGI->new( );

if ( $q->request_method( ) eq "OPTIONS" ) {
  print "Status: 200 OK\nAccess-Control-Allow-Headers: *\nAccess-Control-Allow-Methods: GET, OPTIONS, POST\nAccess-Control-Allow-Origin: *\nAccess-Control-Max-Age: 3600\nContent-Type: text/plain\n\n";
} else {

  my $type = scalar( $q->param('t') );
  my $value = scalar( $q->param('v') );

  if ( not defined $type or not defined $value ) {
    print "Status: 200 OK\nContent-type: application/json\n\n";
    print encode_json( { "ok" => JSON::false, "msg" => "Unable to validate due to client-side error, please reload this page. Required parameter missing." } );
  } else {

    my $constraint = find_type_constraint( $type );

    if ( not defined $constraint ) {
      print "Status: 200 OK\nContent-type: application/json\n\n";
      print encode_json( { "ok" => JSON::false, "msg" => "Unable to validate due to client-side error, please reload this page. Invalid constraint name." } );
    } else {

      print "Status: 200 OK\nContent-type: application/json\n\n";

      my $error = $constraint->validate( $value );

      if ( defined $error ) {
        print encode_json( { "ok" => JSON::false, "msg" => $error } );
      } else {
        print encode_json( { "ok" => JSON::true } );
      };

    };

  };

};