Skip to content

Postgres validation extension

License

Notifications You must be signed in to change notification settings

bitsnap/pg_valid

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pg_valid

Complex postgres validation extension.

Motivation

There's too much validation going on, usually on the backend and frontend side, but sometimes even in the database schema itself. This extension was created to standardize domain validation and introduce basic reflection capabilities, which makes the database schema the single source of truth. Posix regular expressions are far from anything I'd call fast, so porting the most common checks to C had improved performance a lot.

SELECT pgvalid_create_validated_domain("name" => 'email',
                                       not_blank => true,
                                       html_escaped => true,
                                       is_email => true,
                                       min_length => 3
);

SELECT pgvalid_create_validated_domain("name" => 'cleartext_password',
                                       not_blank => true,
                                       html_escaped => true,
                                       min_length => 5,

);

SELECT pgvalid_create_validated_domain("name" => 'full_name',
                                       not_blank => true,
                                       html_escaped => true,
                                       min_length => 5,
                                       is_alnum => true
);

CREATE TABLE public.users (
  id SERIAL NOT NULL,
  email email NOT NULL,
  password cleartext_password NOT NULL,
  full_name full_name NOT NULL
);

SELECT * FROM registered_domains WHERE "name" = 'full_name';
    name     | schema | not_blank | html_escaped | is_num | is_alpha | is_alnum | ...
-------------+--------+-----------+--------------+--------+----------+----------+
 full_name   | public | t         | t            | f      | f        | f        |

Installation

Install libicu first for Unicode support; PostgreSQL ICU support is not required, although ICU usage is advisable.

make 
sudo make install
sudo make installcheck
CREATE EXTENSION pg_valid;

pg_valid features

0. Validation primitives

To replace common SQL and regex based validation with a set of simple and efficient C functions.

1. Validated Domains management

A set of convenience functions to seamlessly create and manage validation domains.

Testing

C Functions, without the extension binding

mkdir build
cd build
cmake ..
make
./pg_valid_test

Postgres extension

make
sudo make install
sudo systemctl restart postgresql
sudo PGUSER=postgres make installcheck

CTest is omitted for simplicity.

License

Licensed under the terms of MIT License.